10 #ifndef SRC_INCLUDE_INTEGRATE_H_ 11 #define SRC_INCLUDE_INTEGRATE_H_ 14 #include <gsl/gsl_integration.h> 15 #include <gsl/gsl_monte_plain.h> 16 #include <gsl/gsl_monte_vegas.h> 42 void operator()(gsl_integration_cquad_workspace *ptr)
const {
46 gsl_integration_cquad_workspace_free(ptr);
54 class Result :
public std::pair<double, double> {
56 using Base = std::pair<double, double>;
63 operator double()
const {
return Base::first; }
66 double value()
const {
return Base::first; }
69 double error()
const {
return Base::second; }
80 double relative_tolerance = 5e-4,
81 double absolute_tolerance = 1e-9)
const {
82 const double allowed_error =
83 std::max(absolute_tolerance,
value() * relative_tolerance);
84 if (
error() > allowed_error) {
85 std::stringstream error_msg;
86 error_msg << integration_name <<
" resulted in I = " <<
value() <<
" ± " 88 <<
", but the required precision is either absolute error < " 89 << absolute_tolerance <<
" or relative error < " 90 << relative_tolerance << std::endl;
91 throw std::runtime_error(error_msg.str());
123 :
workspace_(gsl_integration_cquad_workspace_alloc(workspace_size)) {}
138 template <
typename F>
141 const gsl_function gslfun{
144 [](
double x,
void *type_erased) ->
double {
145 auto &&f = *
static_cast<F *
>(type_erased);
151 const int error_code = gsl_integration_cquad(
153 &result.first, &result.second,
156 std::stringstream err;
157 err <<
"GSL 1D deterministic integration: " << gsl_strerror(error_code);
158 throw std::runtime_error(err.str());
167 std::unique_ptr<gsl_integration_cquad_workspace, GslWorkspaceDeleter>
205 :
state_(gsl_monte_plain_alloc(1)),
206 rng_(gsl_rng_alloc(gsl_rng_mt19937)),
208 gsl_monte_plain_init(
state_);
211 gsl_rng_set(
rng_, seed);
216 gsl_monte_plain_free(
state_);
234 template <
typename F>
238 const double lower[1] = {min};
239 const double upper[1] = {max};
244 const gsl_monte_function monte_fun{
246 [](
double *x,
size_t ,
void *params) ->
double {
247 auto &&f = *
static_cast<F *
>(params);
252 const int error_code =
256 std::stringstream err;
257 err <<
"GSL 1D Monte-Carlo integration: " << gsl_strerror(error_code);
258 throw std::runtime_error(err.str());
261 result.
check_error(
"GSL 1D Monte-Carlo integration");
305 :
state_(gsl_monte_plain_alloc(2)),
306 rng_(gsl_rng_alloc(gsl_rng_mt19937)),
308 gsl_monte_plain_init(
state_);
311 gsl_rng_set(
rng_, seed);
316 gsl_monte_plain_free(
state_);
336 template <
typename F>
341 const double lower[2] = {min1, min2};
342 const double upper[2] = {max1, max2};
344 if (max1 <= min1 || max2 <= min2)
347 const gsl_monte_function monte_fun{
349 [](
double *x,
size_t ,
void *params) ->
double {
350 auto &&f = *
static_cast<F *
>(params);
351 return f(x[0], x[1]);
378 template <
typename F>
420 double epsabs = 1e-9)
439 template <
typename F>
443 if (max1 < min1 || max2 < min2) {
444 bool tolerable = (max1 - min1 > -1.e-16) && (max2 - min2 > -1.e-16);
448 std::stringstream err;
449 err <<
"Integrator2dCuhre got wrong integration limits: [" << min1 <<
", " 450 << max1 <<
"], [" << min2 <<
", " << max2 <<
"]";
451 throw std::invalid_argument(err.str());
454 Integrand2d<F> f_with_limits = {min1, max1 - min1, min2, max2 - min2, fun};
456 const integrand_t cuhre_fun{[](
const int * ,
const cubareal xx[],
457 const int * , cubareal ff[],
458 void *userdata) ->
int {
462 ff[0] = (i->f)(i->min1 + i->diff1 * xx[0], i->min2 + i->diff2 * xx[1]) *
469 void *userdata = &f_with_limits;
472 const int mineval = 0;
475 const char *statefile =
nullptr;
476 void *spin =
nullptr;
478 Cuhre(ndim, ncomp, cuhre_fun, userdata, nvec,
epsrel_,
epsabs_, flags,
480 &result.first, &result.second, &
prob_);
483 std::stringstream err;
484 err <<
"After " <<
neval_ <<
" evaluations " 485 <<
"Cuhre integration from Cuba reports error code " <<
fail_;
486 throw std::runtime_error(err.str());
521 #endif // SRC_INCLUDE_INTEGRATE_H_ int maxeval_
The (approximate) maximum number of integrand evaluations allowed.
double min2
the lower bound of the second integrated variable
double value() const
Access the first entry in the pair as the value.
gsl_monte_plain_state * state_
internal state of the Monte-Carlo integrator
This is a wrapper for the integrand, so we can pass the limits as well for renormalizing to the unit ...
Result operator()(double a, double b, F &&fun)
The function call operator implements the integration functionality.
constexpr GslWorkspaceDeleter()=default
The class has no members, so this is a noop.
A deleter type for std::unique_ptr to be used with gsl_integration_workspace pointers.
~Integrator1dMonte()
Destructor: Clean up internal state and RNG.
Guard type that safely disables floating point traps for the scope in which it is placed...
const std::size_t number_of_calls_
number of calls to the integrand
A C++ interface for numerical integration in two dimensions with the GSL Monte-Carlo integration func...
Integrator1dMonte(size_t num_calls=1E6)
Construct an integration functor.
std::pair< double, double > Base
The data type to store the value and the error of the integration.
std::unique_ptr< gsl_integration_cquad_workspace, GslWorkspaceDeleter > workspace_
Holds the workspace pointer.
double min1
the lower bound of the first integrated variable
int neval_
Actual number of integrand evaluations needed.
double error() const
Access the second entry in the pair as the absolute error.
A C++ interface for numerical integration in two dimensions with the Cuba Cuhre integration function...
void check_error(const std::string &integration_name, double relative_tolerance=5e-4, double absolute_tolerance=1e-9) const
Check whether the error is small and alert if it is not.
A C++ interface for numerical integration in one dimension with the GSL Monte-Carlo integration funct...
The result type returned from integrations, containing the value and an error.
double diff2
the integration range of the second integrated variable
T uniform_int(T min, T max)
Integrator2d(size_t num_calls=1E6)
Construct an integration functor.
const double accuracy_relative_
Parameter to the GSL integration function: desired relative error limit.
Integrator2dCuhre(int num_calls=1e6, double epsrel=5e-4, double epsabs=1e-9)
Construct an integration functor.
gsl_monte_plain_state * state_
internal state of the Monte-Carlo integrator
double diff1
the integration range of the first integrated variable
gsl_rng * rng_
random number generator
Result operator()(double min1, double max1, double min2, double max2, F &&fun)
The function call operator implements the integration functionality.
const double accuracy_absolute_
Parameter to the GSL integration function: desired absolute error limit.
double prob_
The chi^2 probability that the error is not a reliable estimate of the true integration error...
F f
the integrated function
void operator()(gsl_integration_cquad_workspace *ptr) const
Frees the gsl_integration_cquad_workspace resource if it is non-zero.
double epsabs_
Requested absolute accuracy.
gsl_rng * rng_
random number generator
Result operator()(double min, double max, F &&fun)
The function call operator implements the integration functionality.
const std::size_t number_of_calls_
number of calls to the integrand
A C++ interface for numerical integration in one dimension with the GSL CQUAD integration functions...
Result operator()(double min1, double max1, double min2, double max2, F fun)
The function call operator implements the integration functionality.
int nregions_
Actual number of subregions needed.
~Integrator2d()
Destructor: Clean up internal state and RNG.
Integrator(size_t workspace_size=1000)
Construct an integration functor with the given workspace_size.
double epsrel_
Requested relative accuracy.