46 : m_x(x), m_y(y), m_size(x.size()), m_interpType(interpType), m_binType(bin_type)
48 if (!is_sorted(
x.begin(),
x.end()))
49 ErrorCBL(
"the x array is not sorted!",
"FuncGrid",
"FuncGrid.cpp");
51 shared_ptr<gsl_interp_accel> acc(gsl_interp_accel_alloc(), gsl_interp_accel_free);
54 if (
m_size<5 && interpType!=
"Linear") {
55 WarningMsgCBL(
"the array size is less than 5 -> setting interpolation method to Linear!",
"FuncGrid",
"FuncGrid.cpp");
60 m_type = gsl_interp_linear;
63 m_type = gsl_interp_polynomial;
66 m_type = gsl_interp_cspline;
69 m_type = gsl_interp_cspline_periodic;
75 m_type = gsl_interp_akima_periodic;
78 m_type = gsl_interp_steffen;
81 ErrorCBL(
"the value of m_interpType is not permitted!",
"FuncGrid",
"FuncGrid.cpp");
83 vector<double> xx, yy;
90 for (
size_t i=0; i<
m_size; i++) {
91 xx.emplace_back(log10(
m_x[i]));
92 yy.emplace_back(log10(
m_y[i]));
96 ErrorCBL(
"the value of m_binType is not permitted!",
"FuncGrid",
"FuncGrid.cpp");
101 std::shared_ptr<gsl_spline> spline(gsl_spline_alloc(
m_type,
m_size), gsl_spline_free);
102 gsl_spline_init(spline.get(), xx.data(), yy.data(),
m_size);
127 val = m_spline.get()->y[0]+(_xx-m_spline.get()->x[0])/(m_spline.get()->x[1]-m_spline.get()->x[0])*(m_spline.get()->y[1]-m_spline.get()->y[0]);
129 if (val!=val)
return ErrorCBL(
"inside the xx<m_xmin condition, the return value is nan!",
"operator ()",
"FuncGrid.cpp");
133 else if (xx>m_xmax) {
134 val = m_spline.get()->y[m_size-2]+(_xx-m_spline.get()->x[m_size-2])/(m_spline.get()->x[m_size-1]-m_spline.get()->x[m_size-2])*(m_spline.get()->y[m_size-1]-m_spline.get()->y[m_size-2]);
136 if (val!=val)
return ErrorCBL(
"inside the xx>m_xmax condition, the return value is nan!",
"operator ()",
"FuncGrid.cpp");
142 val = (m_binType==
cbl::BinType::_logarithmic_) ? pow(10., gsl_spline_eval(m_spline.get(), _xx, m_acc.get())) : gsl_spline_eval(m_spline.get(), _xx, m_acc.get());
144 if (val!=val)
return ErrorCBL(
"the return value is nan!",
"operator ()",
"FuncGrid.cpp");
157 for (
size_t i=0; i<xx.size(); i++)
158 yy.push_back(this->operator()(xx[i]));
169 double D1 = gsl_spline_eval_deriv(m_spline.get(), xx, m_acc.get());
180 return gsl_spline_eval_deriv2(m_spline.get(), xx, m_acc.get());
189 function<double(
double)> f = bind(&FuncGrid::operator(),
this, std::placeholders::_1);
200 function<double(
double)> f = bind(&FuncGrid::operator(),
this, std::placeholders::_1);
211 function<double(
double)> f = bind(&FuncGrid::operator(),
this, std::placeholders::_1);
222 function<double(
double)> f = bind(&FuncGrid::operator(),
this, std::placeholders::_1);
233 function<double(
double)> f = bind(&
FuncGrid::D1v,
this, std::placeholders::_1);
244 function<double(
double)> f = bind(&
FuncGrid::D2v,
this, std::placeholders::_1);
255 if(!is_sorted(x.begin(), x.end()))
256 ErrorCBL(
"the x array is not sorted!",
"FuncGrid2D",
"FuncGrid.cpp");
257 if(!is_sorted(y.begin(), y.end()))
258 ErrorCBL(
"the y array is not sorted!",
"FuncGrid2D",
"FuncGrid.cpp");
263 m_size_x = m_x.size();
268 m_size_y = m_y.size();
272 std::shared_ptr<double> grid(
new double[m_size_x*m_size_y], [](
double *p ) {
delete[] p; });
274 for (
size_t i=0; i<m_size_x; i++)
275 for (
size_t j=0; j<m_size_y; j++)
276 grid.get()[i+j*m_size_x] = fxy[i][j];
280 m_interpType = interpType;
282 if (m_interpType==
"Linear")
283 m_type = gsl_interp2d_bilinear;
285 else if (m_interpType==
"Cubic")
286 m_type = gsl_interp2d_bicubic;
289 ErrorCBL(
"the value of m_interpType is not permitted!",
"FuncGrid2D",
"FuncGrid.cpp");
291 shared_ptr<gsl_interp_accel> acc_x(gsl_interp_accel_alloc(), gsl_interp_accel_free);
292 shared_ptr<gsl_interp_accel> acc_y(gsl_interp_accel_alloc(), gsl_interp_accel_free);
296 shared_ptr<gsl_spline2d> spline(gsl_spline2d_alloc(m_type, m_size_x, m_size_y), gsl_spline2d_free);
297 gsl_spline2d_init(spline.get(), m_x.data(), m_y.data(), m_fxy.get(), m_size_x, m_size_y);
322 if ((xx>m_xmax || xx<m_xmin) ||(yy>m_ymax || yy<m_ymin))
331 val = gsl_spline2d_eval(m_spline.get(), xx, yy, m_acc_x.get(), m_acc_y.get());
345 for (
size_t i=0; i<xx.size(); i++)
346 yy.push_back(this->operator()(xx[i][0], xx[i][1]));
357 auto integrand = [&] (
const vector<double> var)
359 return this->operator()(var[0], var[1]);
364 return cuba_integral.
IntegrateVegas({{xmin, xmax}, {ymin, ymax}});
373 auto integrand = [&] (
const vector<double> var)
375 return this->operator()(var[0], var[1]);
380 return cuba_integral.
IntegrateSuave({{xmin, xmax}, {ymin, ymax}});
389 auto integrand = [&] (
const vector<double> var)
391 return this->operator()(var[0], var[1]);
405 auto integrand = [&] (
const vector<double> var)
407 return this->operator()(var[0], var[1]);
412 return cuba_integral.
IntegrateCuhre ({{xmin, xmax}, {ymin, ymax}});
Class used to handle functions stored on a grid.
#define coutCBL
CBL print message.
double IntegrateSuave(const double xmin, const double xmax, const double ymin, const double ymax) const
evaluate the 2D integral of the interpolated function using the Suave routine from CUBA libraries
FuncGrid2D()=default
default constructor
double operator()(const double xx, const double yy) const
overloading of the () operator
double IntegrateCuhre(const double xmin, const double xmax, const double ymin, const double ymax) const
evaluate the 2D integral of the interpolated function using the Cuhre routine from CUBA libraries
void free()
free the GSL objects
double IntegrateVegas(const double xmin, const double xmax, const double ymin, const double ymax) const
evaluate the 2D integral of the interpolated function using the Vegas routine from CUBA libraries
double IntegrateDivonne(const double xmin, const double xmax, const double ymin, const double ymax) const
evaluate the 2D integral of the interpolated function using the Divonne routine from CUBA libraries
std::vector< double > eval_func(const std::vector< std::vector< double >> xx) const
evaluate the function at the xx points
BinType m_binType
bin type
std::vector< double > m_y
y values
const gsl_interp_type * m_type
GSL object used to set the interpolation type.
void free()
free the GSL objects
std::shared_ptr< gsl_spline > m_spline
GSL object used to interpolate.
std::vector< double > eval_func(const std::vector< double > xx) const
evaluate the function at the xx points
double integrate_cquad(const double a, const double b, const double rel_err=1.e-2, const double abs_err=1.e-6, const int nevals=100)
compute the definite integral with GSL cquad method
double integrate_qaws(const double a, const double b, const double alpha=0, const double beta=0, const int mu=0, const int nu=0, const double rel_err=1.e-2, const double abs_err=1.e-6, const int limit_size=1000)
compute the definite integral with GSL qaws method
double root(const double x_low, const double x_up, const double fx0=0, const double rel_err=1.e-2, const double abs_err=1.e-6)
find roots with GSL brent method
double m_xmax
maximum x value
double D2v(const double xx) const
compute the second derivative at xx
double operator()(const double xx) const
overloading of the () operator
std::shared_ptr< gsl_interp_accel > m_acc
GSL accelerator object.
double D1v(const double xx) const
compute the first derivative at xx
size_t m_size
size of the x,y vectors, i.e. the grid size
double root_D1v(const double x_low, const double x_up, const double fx0=0, const double rel_err=1.e-2, const double abs_err=1.e-6)
find roots with GSL brent method for the first derivative
double integrate_qag(const double a, const double b, const double rel_err=1.e-2, const double abs_err=1.e-6, const int limit_size=1000, const int rule=6)
compute the definite integral with GSL qag method
double root_D2v(const double x_low, const double x_up, const double fx0=0, const double rel_err=1.e-2, const double abs_err=1.e-6)
find roots with GSL brent method for the second derivative
std::vector< double > x() const
get the private member FuncGrid::m_x
FuncGrid()=default
default constructor
double m_xmin
minimum x value
std::string m_interpType
method used to interpolate
std::vector< double > m_x
x values
double IntegrateSuave(std::vector< std::vector< double >> integration_limits, const bool parallelize=true)
integrate using the Suave routine
double IntegrateDivonne(std::vector< std::vector< double >> integration_limits, const bool parallelize=true)
integrate using the Divonne routine
double IntegrateCuhre(std::vector< std::vector< double >> integration_limits, const bool parallelize=true)
integrate using the Cuhre routine
double IntegrateVegas(std::vector< std::vector< double >> integration_limits, const bool parallelize=true)
integrate using the Vegas routine
static const char fDP2[]
conversion symbol for: double -> std::string
static const char fDP3[]
conversion symbol for: double -> std::string
static const double alpha
: the fine-structure constant
@ _workInProgress_
error due to work in progress
double GSL_integrate_cquad(gsl_function func, const double a, const double b, const double rel_err=1.e-3, const double abs_err=0, const int nevals=100)
integral, using the gsl cquad method
double GSL_integrate_qaws(gsl_function Func, const double a, const double b, const double alpha=0, const double beta=0, const int mu=1, const int nu=0, const double rel_err=1.e-3, const double abs_err=0, const int limit_size=1000)
integral, computed using the GSL qaws method
double GSL_root_brent(gsl_function Func, const double low_guess, const double up_guess, const double rel_err=1.e-3, const double abs_err=0)
function to find roots using GSL qag method
double GSL_integrate_qag(gsl_function Func, const double a, const double b, const double rel_err=1.e-3, const double abs_err=0, const int limit_size=1000, const int rule=6)
integral, computed using the GSL qag method
The global namespace of the CosmoBolognaLib
T Min(const std::vector< T > vect)
minimum element of a std::vector
std::string conv(const T val, const char *fact)
convert a number to a std::string
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
T Max(const std::vector< T > vect)
maximum element of a std::vector
@ _logarithmic_
logarithmic binning
void WarningMsgCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL)
internal CBL warning message