Version: SMASH-1.5
smash::Integrator1dMonte Class Reference

#include <integrate.h>

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

Example:

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

Definition at line 188 of file integrate.h.

Collaboration diagram for smash::Integrator1dMonte:
[legend]

Public Member Functions

 Integrator1dMonte (size_t num_calls=1E6)
 Construct an integration functor. More...
 
 ~Integrator1dMonte ()
 Destructor: Clean up internal state and RNG. More...
 
template<typename F >
Result operator() (double min, double max, 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

◆ Integrator1dMonte()

smash::Integrator1dMonte::Integrator1dMonte ( 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 204 of file integrate.h.

205  : state_(gsl_monte_plain_alloc(1)),
206  rng_(gsl_rng_alloc(gsl_rng_mt19937)),
207  number_of_calls_(num_calls) {
208  gsl_monte_plain_init(state_);
209  // initialize the GSL RNG with a random seed
210  const uint32_t seed = random::uniform_int(0ul, ULONG_MAX);
211  gsl_rng_set(rng_, seed);
212  }
gsl_monte_plain_state * state_
internal state of the Monte-Carlo integrator
Definition: integrate.h:268
T uniform_int(T min, T max)
Definition: random.h:97
gsl_rng * rng_
random number generator
Definition: integrate.h:271
const std::size_t number_of_calls_
number of calls to the integrand
Definition: integrate.h:274
Here is the call graph for this function:

◆ ~Integrator1dMonte()

smash::Integrator1dMonte::~Integrator1dMonte ( )
inline

Destructor: Clean up internal state and RNG.

Definition at line 215 of file integrate.h.

215  {
216  gsl_monte_plain_free(state_);
217  gsl_rng_free(rng_);
218  }
gsl_monte_plain_state * state_
internal state of the Monte-Carlo integrator
Definition: integrate.h:268
gsl_rng * rng_
random number generator
Definition: integrate.h:271

Member Function Documentation

◆ operator()()

template<typename F >
Result smash::Integrator1dMonte::operator() ( double  min,
double  max,
F &&  fun 
)
inline

The function call operator implements the integration functionality.

Parameters
[in]minThe lower limit of the integration.
[in]maxThe upper limit of the integration.
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 235 of file integrate.h.

235  {
236  Result result = {0, 0};
237 
238  const double lower[1] = {min};
239  const double upper[1] = {max};
240 
241  if (max <= min)
242  return result;
243 
244  const gsl_monte_function monte_fun{
245  // trick: pass integrand function as 'params'
246  [](double *x, size_t /*dim*/, void *params) -> double {
247  auto &&f = *static_cast<F *>(params);
248  return f(x[0]);
249  },
250  1, &fun};
251 
252  const int error_code =
253  gsl_monte_plain_integrate(&monte_fun, lower, upper, 1, number_of_calls_,
254  rng_, state_, &result.first, &result.second);
255  if (error_code) {
256  std::stringstream err;
257  err << "GSL 1D Monte-Carlo integration: " << gsl_strerror(error_code);
258  throw std::runtime_error(err.str());
259  }
260 
261  result.check_error("GSL 1D Monte-Carlo integration");
262 
263  return result;
264  }
gsl_monte_plain_state * state_
internal state of the Monte-Carlo integrator
Definition: integrate.h:268
gsl_rng * rng_
random number generator
Definition: integrate.h:271
const std::size_t number_of_calls_
number of calls to the integrand
Definition: integrate.h:274
Here is the call graph for this function:

Member Data Documentation

◆ state_

gsl_monte_plain_state* smash::Integrator1dMonte::state_
private

internal state of the Monte-Carlo integrator

Definition at line 268 of file integrate.h.

◆ rng_

gsl_rng* smash::Integrator1dMonte::rng_
private

random number generator

Definition at line 271 of file integrate.h.

◆ number_of_calls_

const std::size_t smash::Integrator1dMonte::number_of_calls_
private

number of calls to the integrand

Definition at line 274 of file integrate.h.


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