Version: SMASH-3.1
smash::InterpolateDataSpline Class Reference

#include <interpolation.h>

Represent a cubic spline interpolation.

Definition at line 252 of file interpolation.h.

Public Member Functions

 InterpolateDataSpline (const std::vector< double > &x, const std::vector< double > &y)
 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

double first_x_
 First x value. More...
 
double last_x_
 Last x value. More...
 
double first_y_
 First y value. More...
 
double last_y_
 Last y value. More...
 
gsl_interp_accel * acc_
 GSL iterator for interpolation lookups. More...
 
gsl_spline * spline_
 GSL spline. More...
 

Constructor & Destructor Documentation

◆ InterpolateDataSpline()

smash::InterpolateDataSpline::InterpolateDataSpline ( const std::vector< double > &  x,
const std::vector< double > &  y 
)

Interpolate function f given discrete samples f(x_i) = y_i.

Parameters
xx-values.
yy-values.
Returns
The interpolation function.

Cubic spline interpolation is used. Values outside the given samples will use the outmost sample as a constant extrapolation.

Definition at line 14 of file interpolation.cc.

15  {
16  const auto N = x.size();
17  if (y.size() != N) {
18  throw std::runtime_error(
19  "Need two vectors of equal length for interpolation.");
20  }
21  if (N < 3) {
22  throw std::runtime_error(
23  "Need at least 3 data points for cubic spline interpolation.");
24  }
25  const auto p = generate_sort_permutation(
26  x, [&](double const& a, double const& b) { return a < b; });
27  const std::vector<double> sorted_x = apply_permutation(x, p);
28  const std::vector<double> sorted_y = apply_permutation(y, p);
29  check_duplicates(sorted_x, "InterpolateDataSpline");
30 
31  first_x_ = sorted_x.front();
32  last_x_ = sorted_x.back();
33  first_y_ = sorted_y.front();
34  last_y_ = sorted_y.back();
35  acc_ = gsl_interp_accel_alloc();
36  spline_ = gsl_spline_alloc(gsl_interp_cspline, N);
37  gsl_spline_init(spline_, &(*sorted_x.begin()), &(*sorted_y.begin()), N);
38 }
double first_x_
First x value.
gsl_spline * spline_
GSL spline.
double last_y_
Last y value.
double first_y_
First y value.
gsl_interp_accel * acc_
GSL iterator for interpolation lookups.
double last_x_
Last x value.
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.

◆ ~InterpolateDataSpline()

smash::InterpolateDataSpline::~InterpolateDataSpline ( )

Destructor.

Definition at line 40 of file interpolation.cc.

40  {
41  gsl_spline_free(spline_);
42  gsl_interp_accel_free(acc_);
43 }

Member Function Documentation

◆ operator()()

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

Calculate spline interpolation at x.

Parameters
xInterpolation argument.
Returns
Interpolated value.

Definition at line 45 of file interpolation.cc.

45  {
46  // constant extrapolation
47  if (xi < first_x_) {
48  return first_y_;
49  }
50  if (xi > last_x_) {
51  return last_y_;
52  }
53  // cubic spline interpolation
54  return gsl_spline_eval(spline_, xi, acc_);
55 }

Member Data Documentation

◆ first_x_

double smash::InterpolateDataSpline::first_x_
private

First x value.

Definition at line 281 of file interpolation.h.

◆ last_x_

double smash::InterpolateDataSpline::last_x_
private

Last x value.

Definition at line 283 of file interpolation.h.

◆ first_y_

double smash::InterpolateDataSpline::first_y_
private

First y value.

Definition at line 285 of file interpolation.h.

◆ last_y_

double smash::InterpolateDataSpline::last_y_
private

Last y value.

Definition at line 287 of file interpolation.h.

◆ acc_

gsl_interp_accel* smash::InterpolateDataSpline::acc_
private

GSL iterator for interpolation lookups.

Definition at line 289 of file interpolation.h.

◆ spline_

gsl_spline* smash::InterpolateDataSpline::spline_
private

GSL spline.

Definition at line 291 of file interpolation.h.


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