Version: SMASH-3.1
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 62 of file interpolation.h.

Public Member Functions

 InterpolateDataLinear (const std::vector< T > &x, const std::vector< T > &y)
 Interpolate function f given discrete samples f(x_i) = y_i. More...
 
operator() (T x) const
 Calculate spline 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...
 

Constructor & Destructor Documentation

◆ InterpolateDataLinear()

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

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

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

Piecewise linear interpolation is used. Values outside the given samples will use the outmost linear interpolation.

Definition at line 195 of file interpolation.h.

196  {
197  assert(x.size() == y.size());
198  const size_t n = x.size();
199  const auto p = generate_sort_permutation(
200  x, [&](T const& a, T const& b) { return a < b; });
201  x_ = apply_permutation(x, p);
202  check_duplicates(x_, "InterpolateDataLinear");
203  std::vector<T> y_sorted = std::move(apply_permutation(y, p));
204  f_.reserve(n - 1);
205  for (size_t i = 0; i < n - 1; i++) {
206  f_.emplace_back(
207  InterpolateLinear<T>(x_[i], y_sorted[i], x_[i + 1], y_sorted[i + 1]));
208  }
209 }
std::vector< InterpolateLinear< T > > f_
Piecewise linear interpolation using f(x_i)
Definition: interpolation.h:88
std::vector< T > x_
x_i
Definition: interpolation.h:86
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.

Member Function Documentation

◆ operator()()

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

Calculate spline interpolation at x.

Parameters
xInterpolation argument.
Returns
Interpolated value.

Definition at line 240 of file interpolation.h.

240  {
241  // Find the piecewise linear interpolation corresponding to x0.
242  size_t i = find_index(x_, x0);
243  if (i >= f_.size()) {
244  // We don't have a linear interpolation beyond the last point in x_.
245  // Use the last linear interpolation instead.
246  i = f_.size() - 1;
247  }
248  return f_[i](x0);
249 }
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.

Member Data Documentation

◆ x_

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

x_i

Definition at line 86 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 88 of file interpolation.h.


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