36 throw std::runtime_error(
"CubicSpline: not initialized");
38 if (x <=
x_.front()) {
40 }
else if (x >=
x_.back()) {
44 const double dx = x -
x_[i];
45 result =
y_[i] +
b_[i] * dx +
c_[i] * dx * dx +
d_[i] * dx * dx * dx;
53 throw std::runtime_error(
"CubicSpline: not initialized");
59 if (xa <
x_.front()) {
61 const auto dx =
x_.front() - xa;
62 const auto dy =
y_.front() - y;
63 result += dx *
y_.front() - dx * dy / 2.0;
69 const auto dx = xb -
x_.back();
70 const auto dy = y -
y_.back();
71 result += dx *
y_.back() + dx * dy / 2.0;
80 for (
size_t m = ia; m < ib; ++m) {
90 const size_t n =
x_.size();
95 std::vector<double> h(n - 1);
96 std::vector<double> alpha(n - 1);
97 for (
size_t i = 0; i < n - 1; ++i) {
98 h[i] =
x_[i + 1] -
x_[i];
100 3.0 * ((
y_[i + 1] -
y_[i]) / h[i] - (i > 0 ? (
y_[i] -
y_[i - 1]) / h[i - 1] : 0.0));
103 std::vector l(n, 1.0);
104 std::vector mu(n, 0.0);
105 std::vector z(n, 0.0);
107 for (
size_t i = 1; i < n - 1; ++i) {
108 l[i] = 2.0 * (
x_[i + 1] -
x_[i - 1]) - h[i - 1] * mu[i - 1];
110 z[i] = (alpha[i] - h[i - 1] * z[i - 1]) / l[i];
114 for (
int i =
static_cast<int>(n) - 2; i >= 0; --i) {
115 c_[i] = z[i] - mu[i] *
c_[i + 1];
116 b_[i] = (
y_[i + 1] -
y_[i]) / h[i] - h[i] * (
c_[i + 1] + 2.0 *
c_[i]) / 3.0;
117 d_[i] = (
c_[i + 1] -
c_[i]) / (3.0 * h[i]);
126 for (
size_t i = 0; i <
x_.size() - 1; ++i) {
132 return y_[i] * dx +
b_[i] * dx * dx / 2.0 +
c_[i] * dx * dx * dx / 3.0
133 +
d_[i] * dx * dx * dx * dx / 4.0;
137 double dx = x -
x_[0];
138 return y_[0] +
b_[0] * dx;
142 size_t n =
x_.size();
143 double dx = x -
x_[n - 1];
144 return y_[n - 1] +
b_[n - 2] * dx;
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.
void computeIntegrals()
Compute the integrals of the intervals.
double evalIntegral(double xa, double xb, Accelerator &accel) const override
Evaluate the integral of the spline at x.
void init(const std::vector< double > &x, const std::vector< double > &y) override
Initialize from tabulated data (natural spline).
void computeCoefficients()
Compute natural spline coefficients.
double extrapolateLeft(double x) const
Linear extrapolation to the left of the data range.
double extrapolateRight(double x) const
Linear extrapolation to the right of the data range.
CubicSpline()=default
Default constructor.
double eval(double x, Accelerator &accel) const override
Evaluate the spline at x using an accelerator.
double integral(size_t i, double dx) const
Calculate the integral from x_[i] to x_[i]+dx.
std::vector< double > integrals_