Version: SMASH-1.6
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

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:497
double epsabs_
Requested absolute accuracy.
Definition: integrate.h:501
double epsrel_
Requested relative accuracy.
Definition: integrate.h:499

Member Function Documentation

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

Here is the call graph for this function:

Member Data Documentation

int smash::Integrator2dCuhre::maxeval_
private

The (approximate) maximum number of integrand evaluations allowed.

Definition at line 497 of file integrate.h.

double smash::Integrator2dCuhre::epsrel_
private

Requested relative accuracy.

Definition at line 499 of file integrate.h.

double smash::Integrator2dCuhre::epsabs_
private

Requested absolute accuracy.

Definition at line 501 of file integrate.h.

int smash::Integrator2dCuhre::nregions_
private

Actual number of subregions needed.

Definition at line 503 of file integrate.h.

int smash::Integrator2dCuhre::neval_
private

Actual number of integrand evaluations needed.

Definition at line 505 of file integrate.h.

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 513 of file integrate.h.

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 518 of file integrate.h.


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