Version: SMASH-3.4
smash::InterpolateDataSpline Class Reference

#include <interpolation.h>

Represent a cubic spline interpolation.

Definition at line 272 of file interpolation.h.

Collaboration diagram for smash::InterpolateDataSpline:
[legend]

Public Member Functions

 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. More...
 
 ~InterpolateDataSpline ()
 Destructor. More...
 
double operator() (double x) const
 Calculate spline interpolation at x. More...
 

Private Attributes

ExtrapolationType extrapolation_type_ = ExtrapolationType::None
 Extrapolation type. More...
 
double first_x_ = smash_NaN<double>
 First x value of underlying data. More...
 
double last_x_ = smash_NaN<double>
 Last x value of underlying data. More...
 
double first_y_ = smash_NaN<double>
 First y value of underlying data. More...
 
double last_y_ = smash_NaN<double>
 Last y value of underlying data. More...
 
gsl_interp_accel * acc_ = nullptr
 GSL iterator for interpolation lookups. More...
 
gsl_spline * spline_ = nullptr
 GSL spline. More...
 

Constructor & Destructor Documentation

◆ InterpolateDataSpline()

smash::InterpolateDataSpline::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.

Cubic spline interpolation is used.

Parameters
xx-values.
yy-values.
extrapolation_typeType of extrapolation for requested x_i values that are out of bounds. Extrapolation is by default disabled. Possible types are None, Zero, and Constant.
Returns
The interpolation function.
Exceptions
std::invalid_argumentif vectors x and y have different length.
std::invalid_argumentif less than 3 data points are provided.
std::out_of_rangeif values outside of the boundaries of the underlying data are tried to be accessed and extrapolation is disabled.
std::invalid_argumentif unsupported extrapolation type is requested.

Definition at line 14 of file interpolation.cc.

17  : extrapolation_type_{extrapolation_type} {
18  switch (extrapolation_type_) {
22  break;
23  default:
24  throw std::invalid_argument(
25  "The provided extrapolation type is not supported. Valid types are "
26  "'None', 'Zero', and 'Constant'.");
27  }
28  const auto N = x.size();
29  if (y.size() != N) {
30  throw std::invalid_argument(
31  "The interpolation requires two vectors of equal length.");
32  }
33  if (N < 3) {
34  throw std::invalid_argument(
35  "Need at least 3 data points for cubic spline interpolation.");
36  }
37  const auto p = generate_sort_permutation(
38  x, [&](double const& a, double const& b) { return a < b; });
39  const std::vector<double> sorted_x = apply_permutation(x, p);
40  const std::vector<double> sorted_y = apply_permutation(y, p);
41  check_duplicates(sorted_x, "InterpolateDataSpline");
42 
43  first_x_ = sorted_x.front();
44  last_x_ = sorted_x.back();
45  first_y_ = sorted_y.front();
46  last_y_ = sorted_y.back();
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);
50 }
double first_x_
First x value of underlying data.
gsl_spline * spline_
GSL spline.
double last_y_
Last y value of underlying data.
double first_y_
First y value of underlying data.
ExtrapolationType extrapolation_type_
Extrapolation type.
gsl_interp_accel * acc_
GSL iterator for interpolation lookups.
double last_x_
Last x value of underlying data.
@ None
No extrapolation is done.
@ Constant
Extrapolate using a constant value.
@ Zero
Extrapolate with zero.
constexpr int p
Proton.
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.
Here is the call graph for this function:

◆ ~InterpolateDataSpline()

smash::InterpolateDataSpline::~InterpolateDataSpline ( )

Destructor.

Definition at line 52 of file interpolation.cc.

52  {
53  gsl_spline_free(spline_);
54  gsl_interp_accel_free(acc_);
55 }

Member Function Documentation

◆ operator()()

double smash::InterpolateDataSpline::operator() ( double  x) const

Calculate spline interpolation at x.

Parameters
xInterpolation argument.
Returns
Interpolated value.
Exceptions
std::out_of_rangeif values outside of the boundaries of the underlying data are tried to be accessed and extrapolation is disabled.

Definition at line 57 of file interpolation.cc.

57  {
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. ",
63  std::ios::ate};
64  error_msg << "x value " << xi << " is out of bounds.";
65  throw std::out_of_range(error_msg.str());
67  return 0.;
69  return (xi < first_x_) ? first_y_ : last_y_;
70  }
71  }
72  // cubic spline interpolation
73  return gsl_spline_eval(spline_, xi, acc_);
74 }

Member Data Documentation

◆ extrapolation_type_

ExtrapolationType smash::InterpolateDataSpline::extrapolation_type_ = ExtrapolationType::None
private

Extrapolation type.

Definition at line 316 of file interpolation.h.

◆ first_x_

double smash::InterpolateDataSpline::first_x_ = smash_NaN<double>
private

First x value of underlying data.

Definition at line 318 of file interpolation.h.

◆ last_x_

double smash::InterpolateDataSpline::last_x_ = smash_NaN<double>
private

Last x value of underlying data.

Definition at line 320 of file interpolation.h.

◆ first_y_

double smash::InterpolateDataSpline::first_y_ = smash_NaN<double>
private

First y value of underlying data.

Definition at line 322 of file interpolation.h.

◆ last_y_

double smash::InterpolateDataSpline::last_y_ = smash_NaN<double>
private

Last y value of underlying data.

Definition at line 324 of file interpolation.h.

◆ acc_

gsl_interp_accel* smash::InterpolateDataSpline::acc_ = nullptr
private

GSL iterator for interpolation lookups.

Definition at line 326 of file interpolation.h.

◆ spline_

gsl_spline* smash::InterpolateDataSpline::spline_ = nullptr
private

GSL spline.

Definition at line 328 of file interpolation.h.


The documentation for this class was generated from the following files: