Version: SMASH-1.5
smash::Integrator2d Class Reference

#include <integrate.h>

A C++ interface for numerical integration in two dimensions with the GSL Monte-Carlo integration functions.

Example:

Integrator integrate;
const auto result = integrate(0.1, 0.9, 0., 0.5,
[](double x, double y) { return x * y; });

Definition at line 288 of file integrate.h.

Collaboration diagram for smash::Integrator2d:
[legend]

Public Member Functions

 Integrator2d (size_t num_calls=1E6)
 Construct an integration functor. More...
 
 ~Integrator2d ()
 Destructor: Clean up internal state and RNG. More...
 
template<typename F >
Result operator() (double min1, double max1, double min2, double max2, F &&fun)
 The function call operator implements the integration functionality. More...
 

Private Attributes

gsl_monte_plain_state * state_
 internal state of the Monte-Carlo integrator More...
 
gsl_rng * rng_
 random number generator More...
 
const std::size_t number_of_calls_
 number of calls to the integrand More...
 

Constructor & Destructor Documentation

◆ Integrator2d()

smash::Integrator2d::Integrator2d ( size_t  num_calls = 1E6)
inlineexplicit

Construct an integration functor.

Parameters
[in]num_callsThe desired number of calls to the integrand function (defaults to 1E6 if omitted), i.e. how often the integrand is sampled in the Monte-Carlo integration. Larger numbers lead to a more precise result, but also to increased runtime.
Note
Since the workspace is allocated in the constructor and deallocated on destruction, you should not recreate Integrator objects unless required. Thus, if you want to calculate multiple integrals with the same workspace_size, keep the Integrator object around.

Definition at line 304 of file integrate.h.

305  : state_(gsl_monte_plain_alloc(2)),
306  rng_(gsl_rng_alloc(gsl_rng_mt19937)),
307  number_of_calls_(num_calls) {
308  gsl_monte_plain_init(state_);
309  // initialize the GSL RNG with a random seed
310  const uint32_t seed = random::uniform_int(0ul, ULONG_MAX);
311  gsl_rng_set(rng_, seed);
312  }
const std::size_t number_of_calls_
number of calls to the integrand
Definition: integrate.h:369
T uniform_int(T min, T max)
Definition: random.h:97
gsl_monte_plain_state * state_
internal state of the Monte-Carlo integrator
Definition: integrate.h:363
gsl_rng * rng_
random number generator
Definition: integrate.h:366
Here is the call graph for this function:

◆ ~Integrator2d()

smash::Integrator2d::~Integrator2d ( )
inline

Destructor: Clean up internal state and RNG.

Definition at line 315 of file integrate.h.

315  {
316  gsl_monte_plain_free(state_);
317  gsl_rng_free(rng_);
318  }
gsl_monte_plain_state * state_
internal state of the Monte-Carlo integrator
Definition: integrate.h:363
gsl_rng * rng_
random number generator
Definition: integrate.h:366

Member Function Documentation

◆ operator()()

template<typename F >
Result smash::Integrator2d::operator() ( double  min1,
double  max1,
double  min2,
double  max2,
F &&  fun 
)
inline

The function call operator implements the integration functionality.

Parameters
[in]min1The lower limit in the first dimension.
[in]max1The upper limit in the first dimension.
[in]min2The lower limit in the second dimension.
[in]max2The upper limit in the second dimension.
Template Parameters
FType of the integrand function.
Parameters
[in]funThe callable to integrate over. This callable may be a function pointer, lambda, or a functor object. In any case, the callable must return a double and take two double arguments. If you want to pass additional data to the callable you can e.g. use lambda captures.
Returns
Pair of integral value and absolute error estimate.

Definition at line 337 of file integrate.h.

338  {
339  Result result = {0, 0};
340 
341  const double lower[2] = {min1, min2};
342  const double upper[2] = {max1, max2};
343 
344  if (max1 <= min1 || max2 <= min2)
345  return result;
346 
347  const gsl_monte_function monte_fun{
348  // trick: pass integrand function as 'params'
349  [](double *x, size_t /*dim*/, void *params) -> double {
350  auto &&f = *static_cast<F *>(params);
351  return f(x[0], x[1]);
352  },
353  2, &fun};
354 
355  gsl_monte_plain_integrate(&monte_fun, lower, upper, 2, number_of_calls_,
356  rng_, state_, &result.first, &result.second);
357 
358  return result;
359  }
const std::size_t number_of_calls_
number of calls to the integrand
Definition: integrate.h:369
gsl_monte_plain_state * state_
internal state of the Monte-Carlo integrator
Definition: integrate.h:363
gsl_rng * rng_
random number generator
Definition: integrate.h:366

Member Data Documentation

◆ state_

gsl_monte_plain_state* smash::Integrator2d::state_
private

internal state of the Monte-Carlo integrator

Definition at line 363 of file integrate.h.

◆ rng_

gsl_rng* smash::Integrator2d::rng_
private

random number generator

Definition at line 366 of file integrate.h.

◆ number_of_calls_

const std::size_t smash::Integrator2d::number_of_calls_
private

number of calls to the integrand

Definition at line 369 of file integrate.h.


The documentation for this class was generated from the following file: