18#ifndef OPAL_GSL_INTEGRATION_HH
19#define OPAL_GSL_INTEGRATION_HH
74 std::pair<double, double> adaptive_integrate(
75 const std::function<
double(
double)>& func,
double a,
double b,
double tol,
76 size_t max_depth,
size_t depth) {
77 if (depth > max_depth) {
82 auto simpson = [&func](
double a,
double b) ->
double {
83 double h = (b - a) / 2.0;
85 double fm = func(a + h);
87 return (h / 3.0) * (fa + 4.0 * fm + fb);
90 double c = (a + b) / 2.0;
91 double sab = simpson(a, b);
92 double sac = simpson(a, c);
93 double scb = simpson(c, b);
94 double error = std::abs(sab - sac - scb) / 15.0;
97 return {sac + scb, error};
99 auto left = adaptive_integrate(func, a, c, tol / 2.0, max_depth, depth + 1);
100 auto right = adaptive_integrate(func, c, b, tol / 2.0, max_depth, depth + 1);
101 return {left.first + right.first, left.second + right.second};
122 const gsl_function* f,
double a,
double b,
double epsabs,
double epsrel,
size_t ,
125 const size_t max_depth = 20;
126 double tolerance = std::max(epsabs, epsrel * std::abs(b - a));
128 std::function<double(
double)> func = [f](
double x) {
132 auto res = adaptive_integrate(func, a, b, tolerance, max_depth, 0);
134 *abserr = res.second;
constexpr int GSL_INTEG_GAUSS61
gsl_integration_workspace * gsl_integration_workspace_alloc(size_t n)
Allocate integration workspace with limit .
int gsl_integration_qag(const gsl_function *f, double a, double b, double epsabs, double epsrel, size_t, int, gsl_integration_workspace *, double *result, double *abserr)
Adaptive integration on using Simpson refinement.
constexpr int GSL_INTEG_GAUSS51
constexpr int GSL_INTEG_GAUSS31
constexpr int GSL_INTEG_GAUSS15
Integration rule identifiers (accepted but not used in this implementation).
constexpr int GSL_INTEG_GAUSS21
void gsl_integration_workspace_free(gsl_integration_workspace *w)
Free a workspace allocated by gsl_integration_workspace_alloc.
constexpr int GSL_INTEG_GAUSS41
Function wrapper used by integration routines.
double(* function)(double x, void *params)
Workspace for adaptive integration routines.
std::vector< size_t > order
std::vector< double > rlist
std::vector< double > blist
gsl_integration_workspace()
std::vector< double > alist
std::vector< double > elist