15 const std::vector<double>& x,
const std::vector<double>& y,
17 : extrapolation_type_{extrapolation_type} {
24 throw std::invalid_argument(
25 "The provided extrapolation type is not supported. Valid types are "
26 "'None', 'Zero', and 'Constant'.");
28 const auto N = x.size();
30 throw std::invalid_argument(
31 "The interpolation requires two vectors of equal length.");
34 throw std::invalid_argument(
35 "Need at least 3 data points for cubic spline interpolation.");
38 x, [&](
double const& a,
double const& b) {
return a < b; });
47 acc_ = gsl_interp_accel_alloc();
48 spline_ = gsl_spline_alloc(gsl_interp_cspline, N);
49 gsl_spline_init(
spline_, &(*sorted_x.begin()), &(*sorted_y.begin()), N);
54 gsl_interp_accel_free(
acc_);
58 if (xi < first_x_ || xi >
last_x_) {
60 std::ostringstream error_msg{
61 "InterpolateDataSpline only accepts x values within the range of the "
62 "underlying data\nwhen an extrapolation type is not specified. ",
64 error_msg <<
"x value " << xi <<
" is out of bounds.";
65 throw std::out_of_range(error_msg.str());
double first_x_
First x value of underlying data.
gsl_spline * spline_
GSL spline.
~InterpolateDataSpline()
Destructor.
double last_y_
Last y value of underlying data.
double operator()(double x) const
Calculate spline interpolation at x.
double first_y_
First y value of underlying data.
ExtrapolationType extrapolation_type_
Extrapolation type.
InterpolateDataSpline(const std::vector< double > &x, const std::vector< double > &y, ExtrapolationType extrapolation_type=ExtrapolationType::None)
Interpolate function f given discrete samples f(x_i) = y_i.
gsl_interp_accel * acc_
GSL iterator for interpolation lookups.
double last_x_
Last x value of underlying data.
ExtrapolationType
Allows to specify the desired extrapolation type.
@ None
No extrapolation is done.
@ Constant
Extrapolate using a constant value.
@ Zero
Extrapolate with zero.
std::vector< T > apply_permutation(const std::vector< T > &v, const Permutation &p)
Apply a permutation to a vector.
void check_duplicates(const std::vector< T > &x, const std::string &error_position)
Check whether two components have the same value in a sorted vector x.
Permutation generate_sort_permutation(std::vector< T > const &v, Cmp compare)
Calculate the permutations necessary for sorting a vector.