qqbarthreshold is hosted by Hepforge, IPPP Durham
QQbar_threshold
integrate.hpp
Go to the documentation of this file.
1 
31 #pragma once
32 /* */
33 
34 #include "gsl/gsl_integration.h"
35 #include <new>
36 
37 namespace QQbar_threshold{
38 
40 
46  template<class Integrand>
47  inline
48  double integrate(Integrand I, double x_min, double x_max);
49 
50  namespace detail{
55  template<class Functor>
56  inline
57  double call_from_gsl(double t, void * p){
58  Functor * f = static_cast<Functor *>(p);
59  return (*f)(t);
60  }
61  }
62 
64 
67  template<class Integrand>
68  class integrator{
69  static constexpr size_t workspace_size = 200;
70  Integrand i;
71  gsl_integration_cquad_workspace * space;
72  double abserr;
73  size_t nevals;
74  public:
76 
79  integrator(Integrand in):
80  i(in),
81  space(gsl_integration_cquad_workspace_alloc(workspace_size)){
82  if(space == nullptr) throw std::bad_alloc();
83  }
84  ~integrator(){
85  gsl_integration_cquad_workspace_free(space);
86  }
88 
95  double integrate(
96  double a, double b,
97  double epsabs = 10e-8, double epsrel = 10e-6
98  ){
99  double result;
100  gsl_function F;
101  F.function = &detail::call_from_gsl<Integrand>;
102  F.params = &i;
103  gsl_integration_cquad(
104  &F, a, b, epsabs, epsrel, space, &result, &abserr, &nevals
105  );
106  return result;
107  }
109  double get_error() const {
110  return abserr;
111  }
113  double get_num_evals() const {
114  return nevals;
115  }
116  };
117 
119 
123  template<class Integrand>
124  inline
126  return integrator<Integrand>{I};
127  }
128 
129  template<class Integrand>
130  inline
131  double integrate(Integrand I, double x_min, double x_max){
132  return integrator<Integrand>{I}.integrate(x_min, x_max);
133  }
134 
135 }
Definition: alpha_s.hpp:37
double get_error() const
Obtain absolute error of last integration.
Definition: integrate.hpp:109
Class for integrating a function of type Integrand.
Definition: integrate.hpp:68
double integrate(double a, double b, double epsabs=10e-8, double epsrel=10e-6)
Integrate over the integrand specified during construction.
Definition: integrate.hpp:95
integrator(Integrand in)
Constructor.
Definition: integrate.hpp:79
double get_num_evals() const
Obtain number of evaluations during last integration.
Definition: integrate.hpp:113
integrator< Integrand > make_integrator(Integrand I)
helper function to construct an integrator object
Definition: integrate.hpp:125
constexpr double e
Electric charge of the electron in units of the positron charge.
Definition: constants.hpp:70
double integrate(Integrand I, double x_min, double x_max)
Compute the integral of a function.
Definition: integrate.hpp:131