26 template <
typename T =
size_t>
void SetSamples(
const std::vector<T> &x,
const std::vector<double> &y)
28 assert(x.size() == y.size());
30 if (x.empty() || y.empty())
35 std::vector<double> xvals(x.begin(), x.end());
36 std::vector<double> yvals = y;
42 if (xvals.size() == 1)
46 slopel = (yvals[1] - y1) / (xvals[1] - x1);
47 sloper = (y2 - yvals[yvals.size() - 2]) / (x2 - xvals[xvals.size() - 2]);
50 bool fallbackToLinear =
false;
55 fallbackToLinear =
true;
62 for (
size_t i = 1; i < xvals.size(); ++i)
63 if (xvals[i] <= xvals[i - 1])
65 fallbackToLinear =
true;
72 linearExtrapolation = std::make_unique<SimpleLinearRegression<double, double>>();
73 linearExtrapolation->SetTrueLinearRegression(trueInterpolation);
76 for (
size_t i = 0; i < xvals.size(); ++i)
87 xvals.erase(xvals.begin(), xvals.begin() + offset);
88 yvals.erase(yvals.begin(), yvals.begin() + offset);
91 linearExtrapolation->SetSamples(xvals, yvals);
95 spline = std::make_unique<boost::math::interpolators::pchip<std::vector<double>>>(std::move(xvals), std::move(yvals));
102 const bool smallx = x <= x1;
103 const bool largex = x >= x2;
104 if (smallx || largex)
111 result = y1 + slopel * (x - x1);
118 result = y2 + sloper * (x - x2);
123 result = (*spline)(x);
125 else if (linearExtrapolation)
127 result = linearExtrapolation->Predict(x);
131 const double slope = (y2 - y1) / (x2 - x1);
132 result = y1 + slope * (x - x1);
135 if (trueInterpolation)
138 const auto m = 1E-12;