cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
GSLFunctionsWrappers.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2013-2022 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef CepGen_Utils_GSLFunctionsWrappers_h
20#define CepGen_Utils_GSLFunctionsWrappers_h
21
22#include <gsl/gsl_math.h>
23#include <gsl/gsl_monte.h>
24
25#include <memory>
26
28
29namespace cepgen {
30 namespace utils {
32 class GSLFunctionWrapper : public gsl_function {
33 public:
35 static std::unique_ptr<gsl_function> build(const Function1D& func, void* obj) {
36 return std::unique_ptr<gsl_function>(new utils::GSLFunctionWrapper(func, ParametersList(), obj));
37 }
39 static std::unique_ptr<gsl_function> build(const Function1D& func,
40 const ParametersList& params = ParametersList()) {
41 return std::unique_ptr<gsl_function>(new utils::GSLFunctionWrapper(func, params, nullptr));
42 }
43
44 private:
45 explicit GSLFunctionWrapper(const Function1D& func, const ParametersList& plist, void* obj = nullptr)
46 : func_(func), params_(plist), obj_(obj) {
47 function = &GSLFunctionWrapper::eval;
48 params = this;
49 }
51 static double eval(double x, void* params) {
52 auto* wrp = static_cast<GSLFunctionWrapper*>(params);
53 if (wrp->obj_)
54 return wrp->func_(x, wrp->obj_);
55 if (!wrp->params_.empty())
56 return wrp->func_(x, wrp->params_);
57 return wrp->func_(x);
58 }
59 const Function1D func_;
60 const ParametersList& params_;
61 void* obj_{nullptr};
62 };
63
66 template <typename F>
67 class GSLMonteFunctionWrapper : public gsl_monte_function {
68 public:
69 explicit GSLMonteFunctionWrapper(const F& func, size_t ndim) : func_(func) {
70 f = &GSLMonteFunctionWrapper::eval;
71 dim = ndim;
72 params = this;
73 }
75 static std::unique_ptr<gsl_monte_function> build(const F& func, size_t ndim) {
76 return std::unique_ptr<gsl_monte_function>(new utils::GSLMonteFunctionWrapper<decltype(func)>(func, ndim));
77 }
78
79 private:
81 static double eval(double* x, size_t ndim, void* params) {
82 return static_cast<GSLMonteFunctionWrapper*>(params)->func_(x, ndim, params);
83 }
85 const F& func_;
86 };
87 } // namespace utils
88} // namespace cepgen
89
90#endif
Wrapper to a 1-dimensional function with optional parameters.
GSL wrapper to define a functor as a GSL-digestible functional.
static std::unique_ptr< gsl_function > build(const Function1D &func, const ParametersList &params=ParametersList())
Utility to build a gsl_function pointer from a functional.
static std::unique_ptr< gsl_function > build(const Function1D &func, void *obj)
Utility to build a gsl_function pointer from a functional.
GSL wrapper to define a functor as an integrable functional.
static std::unique_ptr< gsl_monte_function > build(const F &func, size_t ndim)
Utility to build a gsl_monte_function pointer from a functional and phase space size.
GSLMonteFunctionWrapper(const F &func, size_t ndim)
Common namespace for this Monte Carlo generator.