10 #ifndef SRC_INCLUDE_INTERPOLATION_H_ 11 #define SRC_INCLUDE_INTERPOLATION_H_ 13 #include <gsl/gsl_errno.h> 14 #include <gsl/gsl_spline.h> 88 std::vector<InterpolateLinear<T>>
f_;
94 slope_ = (y1 - y0) / (x1 - x0);
114 template <
typename T,
typename Cmp>
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]); });
131 template <
typename T>
134 std::vector<T> copied_v = v;
135 std::transform(p.begin(), p.end(), copied_v.begin(),
136 [&](
size_t i) {
return v[i]; });
150 template <
typename T>
152 const std::string& error_position) {
153 auto it = std::adjacent_find(x.begin(), 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());
162 template <
typename T>
164 const std::vector<T>& y) {
165 assert(x.size() == y.size());
166 const size_t n = x.size();
168 x, [&](T
const& a, T
const& b) {
return a < b; });
173 for (
size_t i = 0; i < n - 1; i++) {
197 template <
typename T>
199 const auto it = std::lower_bound(v.begin(), v.end(), x);
200 if (it == v.begin()) {
203 return it - 1 - v.begin();
207 template <
typename T>
211 if (i >= f_.size()) {
234 const std::vector<double>& y);
264 #endif // SRC_INCLUDE_INTERPOLATION_H_ std::vector< T > apply_permutation(const std::vector< T > &v, const Permutation &p)
Apply a permutation to a vector.
std::vector< size_t > Permutation
Represent a permutation.
Represent a cubic spline interpolation.
gsl_spline * spline_
GSL spline.
T slope_
Slope of the linear interpolation.
double last_y_
Last y value.
std::vector< InterpolateLinear< T > > f_
Piecewise linear interpolation using f(x_i)
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.
T operator()(T x) const
Calculate spline interpolation at x.
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.
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).
T operator()(T x) const
Calculate spline interpolation at x.
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.
Represent a piecewise linear interpolation.
InterpolateDataLinear(const std::vector< T > &x, const std::vector< T > &y)
Interpolate function f given discrete samples f(x_i) = y_i.