interpolate::ppoly
On this page
Piecewise polynomials: the form every cubic interpolant (spline, PCHIP, Akima, Hermite) is evaluated, differentiated, integrated and solved in.
Structs
PPolySource
pub struct PPolyA piecewise polynomial of degree k on rising breakpoints b[0] < ... < b[n]: on [b[i], b[i + 1]) it is sum_j c[i, j] (x - b[i])^j.
Evaluation is a binary search and Horner's rule (O(log n + k)); a
sorted eval walks the intervals instead (O(1) per point). The
coefficients are in absolute units, c[i, j] ~ y / h^j for spacing
h, so data whose y / h^k leaves the f64 range (spacing 1e-150
with values 1e150) overflow: rescale x first.
use interpolate::{Curve, PPoly};
use core::array::Matrix;
// x on [0, 1), then 1 + 2 (x - 1) on [1, 2].
let c: Matrix<f64> = [[0, 1], [1, 2]];
let p = try PPoly::new([0, 1, 2], c);
assert(p.at(0.5) == 0.5 && p.at(1.5) == 2.0);
assert(p.integrate(0.0..2.0) == 2.5);
Sourcepub fn new(§breaks: Vector<f64>, coef: Matrix<f64>) throws (InterpError) -> PPoly The piecewise polynomial with breakpoints
breaks(n + 1 rising values) and coefficientscoef(n x (k + 1); columnjis the coefficient of(x - breaks[i])^j, ascending).nabiuse interpolate::{Curve, PPoly}; use core::array::Matrix; let c: Matrix<f64> = [[1, 0, 1]]; // 1 + x^2 on [0, 2] let p = try PPoly::new([0, 2], c); assert(p.at(2.0) == 5.0 && p.degree() == 2);
Sourcepub fn extrapolate(§self, mode: Extrapolate) -> PPoly This polynomial with extrapolation
mode(seeExtrapolate).nabiuse interpolate::{Curve, Extrapolate}; let s = try interpolate::pchip([0, 1, 2], [0, 1, 0]); assert(s.extrapolate(Extrapolate::Fill(f64::NAN)).at(3.0).is_nan());The degree of the pieces.
The breakpoints, n + 1 rising values.
nabilet s = try interpolate::cubic_spline([0, 1, 3], [1, 2, 0]); assert(s.breaks().len() == 3);The coefficients, n x (k + 1), ascending powers of
x - b[i].nabilet s = try interpolate::cubic_spline([0, 1], [1, 3]); // a line let c = s.coefficients(); assert(c[0, 0] == 1.0 && c[0, 1] == 2.0 && c[0, 2] == 0.0);(b[0], b[n]).The
nu-th derivative, a piecewise polynomial of degreek - nu(zero oncenu > k).nabiuse interpolate::Curve; let s = try interpolate::cubic_spline([0, 1, 2, 3], [0, 1, 8, 27]); // x^3 assert((s.derivative(1).at(2.0) - 12.0).abs() < 1e-12); assert((s.derivative(2).at(2.0) - 12.0).abs() < 1e-12);The
nu-th antiderivative, continuous and zero atb[0].nabiuse interpolate::Curve; let s = try interpolate::cubic_spline([0, 1, 2], [1, 1, 1]); assert((s.antiderivative(1).at(2.0) - 2.0).abs() < 1e-15);The integral over
r(r.start > r.endgives the negative), following the extrapolation mode outside the breakpoints:Extendintegrates the end pieces,Clampthe end values,Fill(v)the constantv,Periodicwhole periods.nabiuse interpolate::Extrapolate; let s = try interpolate::linear([0, 1], [0, 1]).ppoly(); assert(s.integrate(0.0..1.0) == 0.5); assert(s.extrapolate(Extrapolate::Periodic).integrate(0.0..3.0) == 1.5);The real
xin[b[0], b[n]]where the polynomial equalsy, rising. A piece that is constant atygives its left end only. Each piece is split at the roots of its derivative (found the same way, recursively), then each monotone part with a sign change is solved by bisection and Newton steps to full precision.nabilet s = try interpolate::cubic_spline([0, 1, 2, 3], [-1, 1, -1, 1]); let r = s.solve(0.0); assert(r.len() == 3 && (r[1] - 1.5).abs() < 1e-12);The real roots in the domain,
solve(0.0).nabilet s = try interpolate::cubic_spline([0, 1, 2], [1, -1, 1]); assert(s.roots().len() == 2);
Trait implementations 1
impl Curve for PPoly