40 std::unique_ptr<Function>
interpolate(
const std::vector<double>& x,
const std::vector<double>& y,
43 if (x.size() != y.size()) {
45 <<
"size: X=" << x.size() <<
", Y=" << y.size();
48 if (x.size() == 1 && extrapolate) {
50 return make_unique<FunctionAdapter>([c](double){
return c;});
55 std::vector<double> final_x;
56 std::vector<double> final_y;
58 final_x.reserve(x.size());
59 final_y.reserve(x.size());
61 final_x.emplace_back(x[0]);
62 final_y.emplace_back(y[0]);
63 for (std::size_t i = 1; i < x.size(); ++i) {
66 logger.
warn() <<
"Ignoring duplicate pair (" << x[i] <<
", " << y[i]
67 <<
") during interpolation";
71 <<
"supported. Entries: (" << x[i] <<
", " << y[i] <<
") and (" 72 << x[i-1] <<
", " << y[i-1] <<
")";
77 <<
"but found " << x[i] <<
" after " << x[i-1];
79 final_x.emplace_back(x[i]);
80 final_y.emplace_back(y[i]);
94 std::vector<double> x;
95 std::vector<double> y;
96 x.reserve(dataset.
size());
97 y.reserve(dataset.
size());
98 for (
auto& pair : dataset) {
99 x.emplace_back(pair.first);
100 y.emplace_back(pair.second);
size_t size() const
Get the size of the vector container.
void warn(const std::string &logMessage)
static Elements::Logging logger
std::unique_ptr< Function > splineInterpolation(const std::vector< double > &x, const std::vector< double > &y, bool extrapolate)
Performs cubic spline interpolation for the given set of data points.
std::unique_ptr< Function > linearInterpolation(const std::vector< double > &x, const std::vector< double > &y, bool extrapolate)
Performs linear interpolation for the given set of data points.
This module provides an interface for accessing two dimensional datasets (pairs of (X...
InterpolationType
Enumeration of the different supported interpolation types.
ELEMENTS_API std::unique_ptr< Function > interpolate(const std::vector< double > &x, const std::vector< double > &y, InterpolationType type, bool extrapolate=false)
static Logging getLogger(const std::string &name="")