Version: SMASH-1.5
interpolation.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2018
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_INTERPOLATION_H_
11 #define SRC_INCLUDE_INTERPOLATION_H_
12 
13 #include <gsl/gsl_errno.h>
14 #include <gsl/gsl_spline.h>
15 
16 #include <algorithm>
17 #include <cassert>
18 #include <cstddef>
19 #include <numeric>
20 #include <sstream>
21 #include <stdexcept>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 namespace smash {
27 
33 template <typename T>
35  public:
37  T slope_;
45  InterpolateLinear(T x0, T y0, T x1, T y1);
46 
53  T operator()(T x) const;
54 };
55 
61 template <typename T>
63  public:
75  InterpolateDataLinear(const std::vector<T>& x, const std::vector<T>& y);
82  T operator()(T x) const;
83 
84  private:
86  std::vector<T> x_;
88  std::vector<InterpolateLinear<T>> f_;
89 };
90 
91 template <typename T>
92 InterpolateLinear<T>::InterpolateLinear(T x0, T y0, T x1, T y1) {
93  assert(x0 != x1);
94  slope_ = (y1 - y0) / (x1 - x0);
95  yintercept_ = y0 - slope_ * x0;
96 }
97 
98 template <typename T>
100  return slope_ * x + yintercept_;
101 }
102 
104 using Permutation = std::vector<size_t>;
105 
114 template <typename T, typename Cmp>
115 Permutation generate_sort_permutation(std::vector<T> const& v, Cmp compare) {
116  Permutation p(v.size());
117  std::iota(p.begin(), p.end(), 0);
118  std::sort(p.begin(), p.end(),
119  [&](size_t i, size_t j) { return compare(v[i], v[j]); });
120  return p;
121 }
122 
131 template <typename T>
132 std::vector<T> apply_permutation(const std::vector<T>& v,
133  const Permutation& p) {
134  std::vector<T> copied_v = v;
135  std::transform(p.begin(), p.end(), copied_v.begin(),
136  [&](size_t i) { return v[i]; });
137  return copied_v;
138 }
139 
150 template <typename T>
151 void check_duplicates(const std::vector<T>& x,
152  const std::string& error_position) {
153  auto it = std::adjacent_find(x.begin(), x.end());
154  if (it != x.end()) {
155  std::stringstream error_msg;
156  error_msg << error_position << ": Each x value must be unique. \"" << *it
157  << "\" was found twice.";
158  throw std::runtime_error(error_msg.str());
159  }
160 }
161 
162 template <typename T>
164  const std::vector<T>& y) {
165  assert(x.size() == y.size());
166  const size_t n = x.size();
167  const auto p = generate_sort_permutation(
168  x, [&](T const& a, T const& b) { return a < b; });
169  x_ = apply_permutation(x, p);
170  check_duplicates(x_, "InterpolateDataLinear");
171  std::vector<T> y_sorted = std::move(apply_permutation(y, p));
172  f_.reserve(n - 1);
173  for (size_t i = 0; i < n - 1; i++) {
174  f_.emplace_back(
175  InterpolateLinear<T>(x_[i], y_sorted[i], x_[i + 1], y_sorted[i + 1]));
176  }
177 }
178 
197 template <typename T>
198 size_t find_index(const std::vector<T>& v, T x) {
199  const auto it = std::lower_bound(v.begin(), v.end(), x);
200  if (it == v.begin()) {
201  return 0;
202  } else {
203  return it - 1 - v.begin();
204  }
205 }
206 
207 template <typename T>
209  // Find the piecewise linear interpolation corresponding to x0.
210  size_t i = find_index(x_, x0);
211  if (i >= f_.size()) {
212  // We don't have a linear interpolation beyond the last point in x_.
213  // Use the last linear interpolation instead.
214  i = f_.size() - 1;
215  }
216  return f_[i](x0);
217 }
218 
221  public:
233  InterpolateDataSpline(const std::vector<double>& x,
234  const std::vector<double>& y);
235 
238 
245  double operator()(double x) const;
246 
247  private:
249  double first_x_;
251  double last_x_;
253  double first_y_;
255  double last_y_;
257  gsl_interp_accel* acc_;
259  gsl_spline* spline_;
260 };
261 
262 } // namespace smash
263 
264 #endif // SRC_INCLUDE_INTERPOLATION_H_
T operator()(T x) const
Calculate spline interpolation at x.
std::vector< T > apply_permutation(const std::vector< T > &v, const Permutation &p)
Apply a permutation to a vector.
~InterpolateDataSpline()
Destructor.
std::vector< size_t > Permutation
Represent a permutation.
Represent a cubic spline interpolation.
InterpolateDataSpline(const std::vector< double > &x, const std::vector< double > &y)
Interpolate function f given discrete samples f(x_i) = y_i.
gsl_spline * spline_
GSL spline.
T operator()(T x) const
Calculate spline interpolation at x.
Definition: interpolation.h:99
T slope_
Slope of the linear interpolation.
Definition: interpolation.h:37
double last_y_
Last y value.
std::vector< InterpolateLinear< T > > f_
Piecewise linear interpolation using f(x_i)
Definition: interpolation.h:88
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.
double first_y_
First y value.
double last_x_
Last x value.
Permutation generate_sort_permutation(std::vector< T > const &v, Cmp compare)
Calculate the permutations necessary for sorting a vector.
gsl_interp_accel * acc_
GSL iterator for interpolation lookups.
Represent a linear interpolation.
Definition: interpolation.h:34
double first_x_
First x value.
InterpolateLinear(T x0, T y0, T x1, T y1)
Linear interpolation given two points (x0, y0) and (x1, y1).
Definition: interpolation.h:92
constexpr int p
Proton.
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.
T yintercept_
y-axis intercept of the linear interpolation.
Definition: interpolation.h:39
constexpr int n
Neutron.
double operator()(double x) const
Calculate spline interpolation at x.
std::vector< T > x_
x_i
Definition: interpolation.h:86
Definition: action.h:24
Represent a piecewise linear interpolation.
Definition: interpolation.h:62
InterpolateDataLinear(const std::vector< T > &x, const std::vector< T > &y)
Interpolate function f given discrete samples f(x_i) = y_i.