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

Definition at line 106 of file integrate.h.

Collaboration diagram for smash::Integrator:
[legend]

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...
 

Private Attributes

std::unique_ptr< gsl_integration_cquad_workspace, GslWorkspaceDeleterworkspace_
 Holds the workspace pointer. More...
 
const double accuracy_absolute_ = 1.0e-9
 Parameter to the GSL integration function: desired absolute error limit. More...
 
const 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:168

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  }
std::unique_ptr< gsl_integration_cquad_workspace, GslWorkspaceDeleter > workspace_
Holds the workspace pointer.
Definition: integrate.h:168
const double accuracy_relative_
Parameter to the GSL integration function: desired relative error limit.
Definition: integrate.h:174
const double accuracy_absolute_
Parameter to the GSL integration function: desired absolute error limit.
Definition: integrate.h:171
Here is the call graph for this function:

Member Data Documentation

◆ workspace_

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

Holds the workspace pointer.

Definition at line 168 of file integrate.h.

◆ accuracy_absolute_

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

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

Definition at line 171 of file integrate.h.

◆ accuracy_relative_

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

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

Definition at line 174 of file integrate.h.


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