9#include <range/v3/range/conversion.hpp>
18 std::vector<double> supporting_points,
21 bool supp_pnts_sorted)
25 if (!supp_pnts_sorted)
35 const std::size_t i = std::distance(
supp_pnts_.begin(), it);
37 "Variable {:d} and variable {:d} are the same. Piecewise linear "
38 "interpolation is not possible\n",
44 std::vector<int>
const& supporting_points,
45 std::vector<double>
const& values_at_supp_pnts,
46 bool supp_pnts_sorted)
47 :
supp_pnts_(supporting_points | ranges::to<std::vector<double>>()),
50 if (!supp_pnts_sorted)
60 const std::size_t i = std::distance(
supp_pnts_.begin(), it);
62 "Variable {:d} and variable {:d} are the same. Piecewise linear "
63 "interpolation is not possible\n",
83 std::size_t
const interval_idx = std::distance(
supp_pnts_.begin(), it) - 1;
87 double const x_r =
supp_pnts_[interval_idx + 1];
94 const double t = (pnt_to_interpolate - x) / (x_r - x);
95 return std::lerp(f, f_r,
t);
99 double const pnt_to_interpolate)
const
101 if (pnt_to_interpolate <
supp_pnts_.front() ||
108 pnt_to_interpolate));
109 std::size_t interval_idx = std::distance(
supp_pnts_.begin(), it);
116 if (interval_idx > 1 && interval_idx <
supp_pnts_.size() - 2)
119 double const x_ll =
supp_pnts_[interval_idx - 2];
120 double const x_l =
supp_pnts_[interval_idx - 1];
122 double const x_r =
supp_pnts_[interval_idx + 1];
130 double const tangent_right = (f_l - f_r) / (x_l - x_r);
131 double const tangent_left = (f_ll - f) / (x_ll - x);
132 double const w = (pnt_to_interpolate - x) / (x_l - x);
133 return (1. - w) * tangent_right + w * tangent_left;
std::vector< double > supp_pnts_
std::vector< double > values_at_supp_pnts_
double getSupportMin() const
PiecewiseLinearInterpolation(std::vector< double > supporting_points, std::vector< double > values_at_supp_pnts, bool supp_pnts_sorted=false)
double getDerivative(double const pnt_to_interpolate) const
Calculates derivative using quadratic interpolation and central difference quotient.
double getSupportMax() const
double getValue(double pnt_to_interpolate) const
Calculates the interpolation value.
void quicksort(It1 first1, It1 last1, It2 first2, Comparator compare)