Version: SMASH-3.4
smash::InterpolateDataLinear< T > Class Template Reference

#include <interpolation.h>

template<typename T>
class smash::InterpolateDataLinear< T >

Represent a piecewise linear interpolation.

Parameters
TType of interpolated values.

Definition at line 67 of file interpolation.h.

Collaboration diagram for smash::InterpolateDataLinear< T >:
[legend]

Public Member Functions

 InterpolateDataLinear (const std::vector< T > &x, const std::vector< T > &y, ExtrapolationType extrapolation_type=ExtrapolationType::None)
 Interpolate function f given discrete samples f(x_i) = y_i. More...
 
operator() (T x) const
 Calculate linear interpolation at x. More...
 

Private Attributes

std::vector< T > x_ {}
 x_i More...
 
std::vector< InterpolateLinear< T > > f_ {}
 Piecewise linear interpolation using f(x_i) More...
 
ExtrapolationType extrapolation_type_ = ExtrapolationType::None
 Extrapolation type. More...
 

Constructor & Destructor Documentation

◆ InterpolateDataLinear()

template<typename T >
smash::InterpolateDataLinear< T >::InterpolateDataLinear ( const std::vector< T > &  x,
const std::vector< T > &  y,
ExtrapolationType  extrapolation_type = ExtrapolationType::None 
)

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

Piecewise linear 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, Constant, and Linear.
Returns
The interpolation function.
Exceptions
std::invalid_argumentif vectors x and y have different length.
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 186 of file interpolation.h.

189  : extrapolation_type_{extrapolation_type} {
190  switch (extrapolation_type_) {
195  break;
196  default:
197  throw std::invalid_argument(
198  "The provided extrapolation type is not supported. Valid types are "
199  "'None', 'Zero', 'Constant', and 'Linear'.");
200  }
201  if (x.size() != y.size()) {
202  throw std::invalid_argument(
203  "The interpolation requires two vectors of equal length.");
204  }
205  const size_t n = x.size();
206  const auto p = generate_sort_permutation(
207  x, [&](T const& a, T const& b) { return a < b; });
208  x_ = apply_permutation(x, p);
209  check_duplicates(x_, "InterpolateDataLinear");
210  std::vector<T> y_sorted = apply_permutation(y, p);
211  f_.reserve(n - 1);
212  for (size_t i = 0; i < n - 1; i++) {
213  f_.emplace_back(
214  InterpolateLinear<T>(x_[i], y_sorted[i], x_[i + 1], y_sorted[i + 1]));
215  }
216 }
ExtrapolationType extrapolation_type_
Extrapolation type.
std::vector< InterpolateLinear< T > > f_
Piecewise linear interpolation using f(x_i)
std::vector< T > x_
x_i
@ Linear
Extrapolate using a linear approach.
@ None
No extrapolation is done.
@ Constant
Extrapolate using a constant value.
@ Zero
Extrapolate with zero.
constexpr int p
Proton.
constexpr int n
Neutron.
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:

Member Function Documentation

◆ operator()()

template<typename T >
T smash::InterpolateDataLinear< T >::operator() ( x) const

Calculate linear 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 247 of file interpolation.h.

247  {
248  const double first_x = x_.front();
249  const double last_x = x_.back();
250  if (x0 < first_x || x0 > last_x) {
252  std::ostringstream error_msg{
253  "InterpolateDataLinear only accepts x values within the range of the "
254  "underlying data\nwhen an extrapolation type is not specified. ",
255  std::ios::ate};
256  error_msg << "x value " << x0 << " is out of bounds.";
257  throw std::out_of_range(error_msg.str());
259  return 0.;
261  return (x0 < first_x) ? f_.front()(first_x) : f_.back()(last_x);
263  return (x0 < first_x) ? f_.front()(x0) : f_.back()(x0);
264  }
265  }
266  // Find the piecewise linear interpolation corresponding to x0.
267  const size_t i = find_index(x_, x0);
268  return f_[i](x0);
269 }
size_t find_index(const std::vector< T > &v, T x)
Find the index in v that corresponds to the last value strictly smaller than x.
Here is the call graph for this function:

Member Data Documentation

◆ x_

template<typename T >
std::vector<T> smash::InterpolateDataLinear< T >::x_ {}
private

x_i

Definition at line 107 of file interpolation.h.

◆ f_

template<typename T >
std::vector<InterpolateLinear<T> > smash::InterpolateDataLinear< T >::f_ {}
private

Piecewise linear interpolation using f(x_i)

Definition at line 109 of file interpolation.h.

◆ extrapolation_type_

template<typename T >
ExtrapolationType smash::InterpolateDataLinear< T >::extrapolation_type_ = ExtrapolationType::None
private

Extrapolation type.

Definition at line 111 of file interpolation.h.


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