45 cbl::glob::FuncGrid_Bspline::FuncGrid_Bspline (
const std::vector<double> x,
const std::vector<double> fx,
const int nbreakpoints,
const int order,
const double frac,
const double xmin,
const double xmax)
47 this->set(x, fx, nbreakpoints, order, frac, xmin, xmax);
54 cbl::glob::FuncGrid_Bspline::FuncGrid_Bspline (
const std::vector<double> x,
const std::vector<double> fx,
const std::vector<double> breakpoints,
const int order,
const double frac)
56 this->set(x, fx, breakpoints, order, frac);
63 void cbl::glob::FuncGrid_Bspline::m_set_bspline(
const vector<double> x,
const vector<double> fx,
const int nbreakpoints,
const int order)
68 m_nbreakpoints = nbreakpoints;
69 m_ncoefficients = m_nbreakpoints-2+m_order;
72 shared_ptr<gsl_bspline_workspace> bspline(gsl_bspline_alloc(m_order, m_nbreakpoints), gsl_bspline_free);
73 shared_ptr<gsl_vector> Bcoeff(gsl_vector_alloc(m_ncoefficients), gsl_vector_free);
74 shared_ptr<gsl_vector> weights(gsl_vector_alloc(m_ncoefficients), gsl_vector_free);
75 shared_ptr<gsl_matrix> covariance(gsl_matrix_alloc(m_ncoefficients, m_ncoefficients), gsl_matrix_free);
80 m_covariance = covariance;
86 void cbl::glob::FuncGrid_Bspline::m_set_knots (
const double xmin,
const double xmax)
97 void cbl::glob::FuncGrid_Bspline::m_set_knots (
const std::vector<double> breakpoints)
99 shared_ptr<gsl_vector> bp(gsl_vector_alloc(m_nbreakpoints), gsl_vector_free);
101 for (
int i=0; i<m_nbreakpoints; i++)
102 gsl_vector_set(bp.get(), i, breakpoints[i]);
104 gsl_bspline_knots(bp.get(), m_bspline.get());
111 void cbl::glob::FuncGrid_Bspline::m_compute_func_integral ()
113 auto func = [&] (
const double &xx) {
return this->operator()(xx,
par::defaultDouble);};
121 void cbl::glob::FuncGrid_Bspline::m_linear_fit(
const double frac)
123 cout <<
"Computing basis spline coefficients" << endl;
125 size_t n = m_x.size();
128 shared_ptr<gsl_vector> y(gsl_vector_alloc(n), gsl_vector_free);
129 shared_ptr<gsl_vector> w(gsl_vector_alloc(n), gsl_vector_free);
131 for (
size_t i=0; i<m_x.size(); i++) {
132 gsl_vector_set(y.get(), i, m_fx[i]);
133 gsl_vector_set(w.get(), i, (frac>0) ? pow(frac*m_fx[i], -2) : 1.);
137 shared_ptr<gsl_matrix> XX(gsl_matrix_alloc(n, m_ncoefficients), gsl_matrix_free);
138 for (
size_t i = 0; i < n; ++i)
141 gsl_bspline_eval(m_x[i], m_Bcoeff.get(), m_bspline.get());
143 for (
int j = 0; j < m_ncoefficients; ++j)
144 gsl_matrix_set(XX.get(), i, j, gsl_vector_get(m_Bcoeff.get(), j));
150 shared_ptr<gsl_multifit_linear_workspace> mw(gsl_multifit_linear_alloc(n, m_ncoefficients), gsl_multifit_linear_free);
152 gsl_multifit_wlinear(XX.get(), w.get(), y.get(), m_weights.get(), m_covariance.get(), &chisq, mw.get());
154 cout <<
"Done! Chi2/d.o.f. = " << chisq/(n-m_ncoefficients) << endl;
161 void cbl::glob::FuncGrid_Bspline::set (
const std::vector<double> x,
const std::vector<double> fx,
const int nbreakpoints,
const int order,
const double frac,
const double xmin,
const double xmax)
163 this->m_set_bspline(x, fx, nbreakpoints, order);
164 this->m_set_knots(xmin, xmax);
165 this->m_linear_fit(frac);
166 this->m_compute_func_integral();
173 void cbl::glob::FuncGrid_Bspline::set (
const std::vector<double> x,
const std::vector<double> fx,
const std::vector<double> breakpoints,
const int order,
const double frac)
175 this->m_set_bspline(x, fx,
static_cast<int>(breakpoints.size()), order);
176 this->m_set_knots(breakpoints);
177 this->m_linear_fit(frac);
178 this->m_compute_func_integral();
185 double cbl::glob::FuncGrid_Bspline::operator () (
const double xx,
const double integral)
const
189 gsl_bspline_eval(xx, m_Bcoeff.get(), m_bspline.get());
190 gsl_multifit_linear_est(m_Bcoeff.get(), m_weights.get(), m_covariance.get(), &fx, &fx_err);
199 vector<double> cbl::glob::FuncGrid_Bspline::eval_func (
const vector<double> xx,
const double integral)
const
201 vector<double> vv(xx.size(), 0), yerr;
203 for (
size_t i=0; i<xx.size(); i++)
204 vv[i] = this->
operator()(xx[i], integral);
Class used to handle functions interpolated using a basis spline http://mathworld....
static const double defaultDouble
default double value
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
T Max(const std::vector< T > vect)
maximum element of a std::vector