3#include <gsl/gsl_errno.h>
6 std::once_flag gsl_error_handler_once_flag;
8 void ensure_gsl_nonfatal() {
9 std::call_once(gsl_error_handler_once_flag, []() {
10 gsl_set_error_handler_off();
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");
25 size_t max_intervals = 1000;
30 gsl_integration_workspace* w = gsl_integration_workspace_alloc(max_intervals);
32 throw std::runtime_error(
"integrate: gsl_integration_workspace_alloc failed");
34 const int status = gsl_integration_qag(
43 gsl_integration_workspace_free(w);
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) <<
")"
55 throw std::runtime_error(oss.str());
65 integrate([
f] (
double x) ->
double {
return f(x).imag(); }, l, u, prec));
std::function< scalar_t(double)> ComplexValuedFunction
std::function< double(double)> RealValuedFunction
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.