37 throw std::runtime_error(
"LinearSpline: not initialized");
39 if (x <=
x_.front()) {
41 }
else if (x >=
x_.back()) {
45 result =
m_[i] * x +
c_[i];
54 throw std::runtime_error(
"LinearSpline: not initialized");
60 if (xa <
x_.front()) {
62 const auto dx =
x_.front() - xa;
63 const auto dy =
y_.front() - y;
64 result += dx *
y_.front() - dx * dy / 2.0;
70 const auto dx = xb -
x_.back();
71 const auto dy = y -
y_.back();
72 result += dx *
y_.back() + dx * dy / 2.0;
81 for (
size_t m = ia; m < ib; ++m) {
90 m_.resize(
x_.size() - 1);
91 c_.resize(
x_.size() - 1);
92 for (
size_t i = 0; i <
x_.size() - 1; ++i) {
93 m_[i] = (
y_[i + 1] -
y_[i]) / (
x_[i + 1] -
x_[i]);
100 for (
size_t i = 0; i <
x_.size() - 1; ++i) {
106 const auto x2 =
x_[i] + dx;
107 return m_[i] * (x2 * x2 -
x_[i] *
x_[i]) / 2.0 +
c_[i] * (x2 -
x_[i]);
Accelerator caching last interval indices.
virtual void init(const std::vector< double > &x, const std::vector< double > &y)
Initialize from tabulated data (natural spline).
size_t findInterval(double x, size_t &intervalCache) const
Return the interval index for the given x-coordinate, using the supplied cached value if possible.
std::vector< double > integrals_
LinearSpline()=default
Default constructor.
double evalIntegral(double xa, double xb, Accelerator &accel) const override
Evaluate the integral of the spline at x.
double extrapolateRight(double x) const
Linear extrapolation to the right of the data range.
double extrapolateLeft(double x) const
Linear extrapolation to the left of the data range.
void computeCoefficients()
Compute linear spline coefficients.
double integral(size_t i, double dx) const
Calculate the integral from x_[i] to x_[i]+dx.
void computeIntegrals()
Compute the integrals of the intervals.
void init(const std::vector< double > &x, const std::vector< double > &y) override
Initialize from tabulated data (natural spline).
double eval(double x, Accelerator &accel) const override
Evaluate the spline at x using an accelerator.