Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
integration.cpp
Go to the documentation of this file.
1#include "integration.h"
2
3#include <gsl/gsl_errno.h>
4
5namespace {
6 std::once_flag gsl_error_handler_once_flag;
7
8 void ensure_gsl_nonfatal() {
9 std::call_once(gsl_error_handler_once_flag, []() {
10 gsl_set_error_handler_off();
11 });
12 }
13}
14
15double integrate(RealValuedFunction f, double l, double u, double prec) {
16 ensure_gsl_nonfatal();
17 if (!std::isfinite(l) || !std::isfinite(u) || !std::isfinite(prec) || prec <= 0.0) {
18 throw std::invalid_argument("integrate: invalid bounds or precision");
19 }
20 if (l == u) {
21 return 0.0;
22 }
23 double res = 0.0;
24 double err = 0.0;
25 size_t max_intervals = 1000;
26 gsl_function F;
27 F.function = &unwrap_lambda_unidim;
28 F.params = &f;
29
30 gsl_integration_workspace* w = gsl_integration_workspace_alloc(max_intervals);
31 if (!w) {
32 throw std::runtime_error("integrate: gsl_integration_workspace_alloc failed");
33 }
34 const int status = gsl_integration_qag(
35 &F, l, u,
36 0.0, prec,
37 max_intervals,
38 GSL_INTEG_GAUSS21,
39 w,
40 &res, &err
41 );
42
43 gsl_integration_workspace_free(w);
44
45 if (status != GSL_SUCCESS || !std::isfinite(res)) {
46 std::ostringstream oss;
47 oss << "integrate: gsl_integration_qag failed"
48 << " status=" << status
49 << " (" << gsl_strerror(status) << ")"
50 << ", l=" << l
51 << ", u=" << u
52 << ", prec=" << prec
53 << ", res=" << res
54 << ", err=" << err;
55 throw std::runtime_error(oss.str());
56 }
57
58 return res;
59
60 return res;
61}
62
63scalar_t c_integrate(ComplexValuedFunction f, double l, double u, double prec) {
64 return scalar_t(integrate([f] (double x) -> double { return f(x).real(); }, l, u, prec),
65 integrate([f] (double x) -> double { return f(x).imag(); }, l, u, prec));
66}
std::function< scalar_t(double)> ComplexValuedFunction
Definition functions.h:12
std::function< double(double)> RealValuedFunction
Definition functions.h:10
double unwrap_lambda_unidim(double x, void *p)
scalar_t c_integrate(ComplexValuedFunction f, double l, double u, double prec)
Performs numerical integration of a complex-valued function of a real variable.
double integrate(RealValuedFunction f, double l, double u, double prec)
Performs numerical integration of a real-valued function of a real variable.
double f(double x)
Wilson special function f depending on x.