Version: SMASH-3.1
smash::Integrator2d Class Reference

#include <integrate.h>

A C++ interface for numerical integration in two dimensions with the Cuba Cuhre integration function.

The algorithm is deterministic and well-suited for low dimensions, where it can reach good accuracy.

Example:

const auto result = integrate(0.1, 0.9, 0., 0.5,
[](double x, double y) { return x * y; });
Integrator2d(int num_calls=1e6, double epsrel=5e-4, double epsabs=1e-9)
Construct an integration functor.
Definition: integrate.h:232
static Integrator integrate
Definition: decaytype.cc:143

Definition at line 219 of file integrate.h.

Public Member Functions

 Integrator2d (int num_calls=1e6, double epsrel=5e-4, double epsabs=1e-9)
 Construct an integration functor. 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

int maxeval_
 The (approximate) maximum number of integrand evaluations allowed. More...
 
double epsrel_
 Requested relative accuracy. More...
 
double epsabs_
 Requested absolute accuracy. More...
 
int nregions_
 Actual number of subregions needed. More...
 
int neval_
 Actual number of integrand evaluations needed. More...
 
int fail_
 An error flag. More...
 
double prob_
 The chi^2 probability that the error is not a reliable estimate of the true integration error. More...
 

Constructor & Destructor Documentation

◆ Integrator2d()

smash::Integrator2d::Integrator2d ( int  num_calls = 1e6,
double  epsrel = 5e-4,
double  epsabs = 1e-9 
)
inlineexplicit

Construct an integration functor.

Parameters
[in]num_callsThe maximum number of calls to the integrand function (defaults to 1E6 if omitted), i.e. how often the integrand can be sampled in the integration. Larger numbers lead to a more precise result, but possibly to increased runtime.
[in]epsrelThe desired relative accuracy (1E-3 by default).
[in]epsabsThe desired absolute accuracy (1E-3 by default).

Definition at line 232 of file integrate.h.

234  : maxeval_(num_calls), epsrel_(epsrel), epsabs_(epsabs) {}
double epsabs_
Requested absolute accuracy.
Definition: integrate.h:314
int maxeval_
The (approximate) maximum number of integrand evaluations allowed.
Definition: integrate.h:310
double epsrel_
Requested relative accuracy.
Definition: integrate.h:312

Member Function Documentation

◆ operator()()

template<typename F >
Result smash::Integrator2d::operator() ( double  min1,
double  max1,
double  min2,
double  max2,
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 253 of file integrate.h.

253  {
254  Result result = {0., 0.};
255 
256  if (max1 < min1 || max2 < min2) {
257  /* Tolerance chosen large enough for floating-point arithmetic error in
258  * chained decays, consider increasing if insufficient. */
259  bool tolerable = (max1 - min1 > -1.e-15) && (max2 - min2 > -1.e-15);
260  if (tolerable) {
261  return result;
262  }
263  std::stringstream err;
264  err << "Integrator2d got wrong integration limits: [" << min1 << ", "
265  << max1 << "], [" << min2 << ", " << max2 << "]";
266  throw std::invalid_argument(err.str());
267  }
268 
269  Integrand2d<F> f_with_limits = {min1, max1 - min1, min2, max2 - min2, fun};
270 
271  const integrand_t cuhre_fun{[](const int * /* ndim */, const cubareal xx[],
272  const int * /* ncomp */, cubareal ff[],
273  void *userdata) -> int {
274  auto i = static_cast<Integrand2d<F> *>(userdata);
275  /* We have to transform the integrand to the unit cube.
276  * This is what Cuba expects. */
277  ff[0] = (i->f)(i->min1 + i->diff1 * xx[0], i->min2 + i->diff2 * xx[1]) *
278  i->diff1 * i->diff2;
279  return 0;
280  }};
281 
282  const int ndim = 2;
283  const int ncomp = 1;
284  void *userdata = &f_with_limits;
285  const int nvec = 1;
286  const int flags = 0; // Use the defaults.
287  const int mineval = 0;
288  const int maxeval = maxeval_;
289  const int key = -1; // Use the default.
290  const char *statefile = nullptr;
291  void *spin = nullptr;
292 
293  Cuhre(ndim, ncomp, cuhre_fun, userdata, nvec, epsrel_, epsabs_, flags,
294  mineval, maxeval, key, statefile, spin, &nregions_, &neval_, &fail_,
295  &result.first, &result.second, &prob_);
296 
297  if (fail_) {
298  std::stringstream err;
299  err << "After " << neval_ << " evaluations "
300  << "Cuhre integration from Cuba reports error code " << fail_;
301  throw std::runtime_error(err.str());
302  }
303  result.check_error("Cuba integration ", epsrel_, epsabs_);
304 
305  return result;
306  }
double prob_
The chi^2 probability that the error is not a reliable estimate of the true integration error.
Definition: integrate.h:331
int fail_
An error flag.
Definition: integrate.h:326
int neval_
Actual number of integrand evaluations needed.
Definition: integrate.h:318
int nregions_
Actual number of subregions needed.
Definition: integrate.h:316

Member Data Documentation

◆ maxeval_

int smash::Integrator2d::maxeval_
private

The (approximate) maximum number of integrand evaluations allowed.

Definition at line 310 of file integrate.h.

◆ epsrel_

double smash::Integrator2d::epsrel_
private

Requested relative accuracy.

Definition at line 312 of file integrate.h.

◆ epsabs_

double smash::Integrator2d::epsabs_
private

Requested absolute accuracy.

Definition at line 314 of file integrate.h.

◆ nregions_

int smash::Integrator2d::nregions_
private

Actual number of subregions needed.

Definition at line 316 of file integrate.h.

◆ neval_

int smash::Integrator2d::neval_
private

Actual number of integrand evaluations needed.

Definition at line 318 of file integrate.h.

◆ fail_

int smash::Integrator2d::fail_
private

An error flag.

0 if the desired accuracy was reached, -1 if the dimension is out of range, larger than 0 if the accuracy goal was not met within the maximum number of evaluations.

Definition at line 326 of file integrate.h.

◆ prob_

double smash::Integrator2d::prob_
private

The chi^2 probability that the error is not a reliable estimate of the true integration error.

Definition at line 331 of file integrate.h.


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