Version: SMASH-1.5
smash::Integrator2dCuhre 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; });

Definition at line 406 of file integrate.h.

Collaboration diagram for smash::Integrator2dCuhre:
[legend]

Public Member Functions

 Integrator2dCuhre (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

◆ Integrator2dCuhre()

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

421  : maxeval_(num_calls), epsrel_(epsrel), epsabs_(epsabs) {}
int maxeval_
The (approximate) maximum number of integrand evaluations allowed.
Definition: integrate.h:495
double epsabs_
Requested absolute accuracy.
Definition: integrate.h:499
double epsrel_
Requested relative accuracy.
Definition: integrate.h:497

Member Function Documentation

◆ operator()()

template<typename F >
Result smash::Integrator2dCuhre::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 440 of file integrate.h.

440  {
441  Result result = {0., 0.};
442 
443  if (max1 < min1 || max2 < min2) {
444  bool tolerable = (max1 - min1 > -1.e-16) && (max2 - min2 > -1.e-16);
445  if (tolerable) {
446  return result;
447  }
448  std::stringstream err;
449  err << "Integrator2dCuhre got wrong integration limits: [" << min1 << ", "
450  << max1 << "], [" << min2 << ", " << max2 << "]";
451  throw std::invalid_argument(err.str());
452  }
453 
454  Integrand2d<F> f_with_limits = {min1, max1 - min1, min2, max2 - min2, fun};
455 
456  const integrand_t cuhre_fun{[](const int * /* ndim */, const cubareal xx[],
457  const int * /* ncomp */, cubareal ff[],
458  void *userdata) -> int {
459  auto i = static_cast<Integrand2d<F> *>(userdata);
460  /* We have to transform the integrand to the unit cube.
461  * This is what Cuba expects. */
462  ff[0] = (i->f)(i->min1 + i->diff1 * xx[0], i->min2 + i->diff2 * xx[1]) *
463  i->diff1 * i->diff2;
464  return 0;
465  }};
466 
467  const int ndim = 2;
468  const int ncomp = 1;
469  void *userdata = &f_with_limits;
470  const int nvec = 1;
471  const int flags = 0; // Use the defaults.
472  const int mineval = 0;
473  const int maxeval = maxeval_;
474  const int key = -1; // Use the default.
475  const char *statefile = nullptr;
476  void *spin = nullptr;
477 
478  Cuhre(ndim, ncomp, cuhre_fun, userdata, nvec, epsrel_, epsabs_, flags,
479  mineval, maxeval, key, statefile, spin, &nregions_, &neval_, &fail_,
480  &result.first, &result.second, &prob_);
481 
482  if (fail_) {
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());
487  }
488  result.check_error("Cuba integration ", epsrel_, epsabs_);
489 
490  return result;
491  }
int maxeval_
The (approximate) maximum number of integrand evaluations allowed.
Definition: integrate.h:495
int neval_
Actual number of integrand evaluations needed.
Definition: integrate.h:503
int fail_
An error flag.
Definition: integrate.h:511
double prob_
The chi^2 probability that the error is not a reliable estimate of the true integration error...
Definition: integrate.h:516
double epsabs_
Requested absolute accuracy.
Definition: integrate.h:499
int nregions_
Actual number of subregions needed.
Definition: integrate.h:501
double epsrel_
Requested relative accuracy.
Definition: integrate.h:497
Here is the call graph for this function:

Member Data Documentation

◆ maxeval_

int smash::Integrator2dCuhre::maxeval_
private

The (approximate) maximum number of integrand evaluations allowed.

Definition at line 495 of file integrate.h.

◆ epsrel_

double smash::Integrator2dCuhre::epsrel_
private

Requested relative accuracy.

Definition at line 497 of file integrate.h.

◆ epsabs_

double smash::Integrator2dCuhre::epsabs_
private

Requested absolute accuracy.

Definition at line 499 of file integrate.h.

◆ nregions_

int smash::Integrator2dCuhre::nregions_
private

Actual number of subregions needed.

Definition at line 501 of file integrate.h.

◆ neval_

int smash::Integrator2dCuhre::neval_
private

Actual number of integrand evaluations needed.

Definition at line 503 of file integrate.h.

◆ fail_

int smash::Integrator2dCuhre::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 511 of file integrate.h.

◆ prob_

double smash::Integrator2dCuhre::prob_
private

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

Definition at line 516 of file integrate.h.


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