Version: SMASH-3.1
smash::Integrator Class Reference

#include <integrate.h>

A C++ interface for numerical integration in one dimension with the GSL CQUAD integration functions.

Example:

const auto result = integrate(0.1, 0.9, [](double x) { return x * x; });
Integrator(size_t workspace_size=1000)
Construct an integration functor with the given workspace_size.
Definition: integrate.h:122
static Integrator integrate
Definition: decaytype.cc:143

Definition at line 106 of file integrate.h.

Public Member Functions

 Integrator (size_t workspace_size=1000)
 Construct an integration functor with the given workspace_size. More...
 
template<typename F >
Result operator() (double a, double b, F &&fun)
 The function call operator implements the integration functionality. More...
 
void set_precision (double absolute, double relative)
 Set precision for the absolute and relative error of the integration. More...
 

Private Attributes

std::unique_ptr< gsl_integration_cquad_workspace, GslWorkspaceDeleterworkspace_
 Holds the workspace pointer. More...
 
double accuracy_absolute_ = 1.0e-9
 Parameter to the GSL integration function: desired absolute error limit. More...
 
double accuracy_relative_ = 5.0e-4
 Parameter to the GSL integration function: desired relative error limit. More...
 

Constructor & Destructor Documentation

◆ Integrator()

smash::Integrator::Integrator ( size_t  workspace_size = 1000)
inlineexplicit

Construct an integration functor with the given workspace_size.

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.
Parameters
workspace_sizeThe internal workspace is allocated such that it can hold the given number of double precision intervals, their integration results, and error estimates. It also determines the maximum number of subintervals the integration algorithm will use.

Definition at line 122 of file integrate.h.

123  : workspace_(gsl_integration_cquad_workspace_alloc(workspace_size)) {}
std::unique_ptr< gsl_integration_cquad_workspace, GslWorkspaceDeleter > workspace_
Holds the workspace pointer.
Definition: integrate.h:176

Member Function Documentation

◆ operator()()

template<typename F >
Result smash::Integrator::operator() ( double  a,
double  b,
F &&  fun 
)
inline

The function call operator implements the integration functionality.

Parameters
[in]aThe lower limit of the integral.
[in]bThe upper limit of the integral.
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 a single double argument. 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 139 of file integrate.h.

139  {
140  Result result;
141  const gsl_function gslfun{
142  /* important! The lambda cannot use captures, otherwise the
143  * conversion to a function pointer type is impossible. */
144  [](double x, void *type_erased) -> double {
145  auto &&f = *static_cast<F *>(type_erased);
146  return f(x);
147  },
148  &fun};
149  // We disable float traps when calling GSL code we cannot control.
150  DisableFloatTraps guard;
151  const int error_code = gsl_integration_cquad(
152  &gslfun, a, b, accuracy_absolute_, accuracy_relative_, workspace_.get(),
153  &result.first, &result.second,
154  nullptr /* Don't store the number of evaluations */);
155  if (error_code) {
156  std::stringstream err;
157  err << "GSL 1D deterministic integration: " << gsl_strerror(error_code);
158  throw std::runtime_error(err.str());
159  }
160  result.check_error("GSL 1D deterministic integration", accuracy_relative_,
162  return result;
163  }
double accuracy_relative_
Parameter to the GSL integration function: desired relative error limit.
Definition: integrate.h:182
double accuracy_absolute_
Parameter to the GSL integration function: desired absolute error limit.
Definition: integrate.h:179

◆ set_precision()

void smash::Integrator::set_precision ( double  absolute,
double  relative 
)
inline

Set precision for the absolute and relative error of the integration.

Definition at line 168 of file integrate.h.

168  {
169  accuracy_absolute_ = absolute;
170  accuracy_relative_ = relative;
171  }

Member Data Documentation

◆ workspace_

std::unique_ptr<gsl_integration_cquad_workspace, GslWorkspaceDeleter> smash::Integrator::workspace_
private

Holds the workspace pointer.

Definition at line 176 of file integrate.h.

◆ accuracy_absolute_

double smash::Integrator::accuracy_absolute_ = 1.0e-9
private

Parameter to the GSL integration function: desired absolute error limit.

Definition at line 179 of file integrate.h.

◆ accuracy_relative_

double smash::Integrator::accuracy_relative_ = 5.0e-4
private

Parameter to the GSL integration function: desired relative error limit.

Definition at line 182 of file integrate.h.


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