packages

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 PPoly

A 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.

nabi
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);
  • pub fn new(breaks: Vector<f64>, coef: Matrix<f64>) throws (InterpError) -> PPoly§

    Source

    The piecewise polynomial with breakpoints breaks (n + 1 rising values) and coefficients coef (n x (k + 1); column j is the coefficient of (x - breaks[i])^j, ascending).

    nabi
    use 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);
    
  • pub fn extrapolate(self, mode: Extrapolate) -> PPoly§

    Source

    This polynomial with extrapolation mode (see Extrapolate).

    nabi
    use 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());
    
  • pub fn degree(self) -> usize§

    Source

    The degree of the pieces.

  • pub fn breaks(self) -> Vector<f64>§

    Source

    The breakpoints, n + 1 rising values.

    nabi
    let s = try interpolate::cubic_spline([0, 1, 3], [1, 2, 0]);
    assert(s.breaks().len() == 3);
    
  • pub fn coefficients(self) -> Matrix<f64>§

    Source

    The coefficients, n x (k + 1), ascending powers of x - b[i].

    nabi
    let 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);
    
  • pub fn domain(self) -> (f64, f64)§

    Source

    (b[0], b[n]).

  • pub fn derivative(self, nu: usize = 1) -> PPoly§

    Source

    The nu-th derivative, a piecewise polynomial of degree k - nu (zero once nu > k).

    nabi
    use 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);
    
  • pub fn antiderivative(self, nu: usize = 1) -> PPoly§

    Source

    The nu-th antiderivative, continuous and zero at b[0].

    nabi
    use 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);
    
  • pub fn integrate(self, r: Range<f64>) -> f64§

    Source

    The integral over r (r.start > r.end gives the negative), following the extrapolation mode outside the breakpoints: Extend integrates the end pieces, Clamp the end values, Fill(v) the constant v, Periodic whole periods.

    nabi
    use 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);
    
  • pub fn solve(self, y: f64 = 0.0) -> Vector<f64>§

    Source

    The real x in [b[0], b[n]] where the polynomial equals y, rising. A piece that is constant at y gives 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.

    nabi
    let 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);
    
  • pub fn roots(self) -> Vector<f64>§

    Source

    The real roots in the domain, solve(0.0).

    nabi
    let s = try interpolate::cubic_spline([0, 1, 2], [1, -1, 1]);
    assert(s.roots().len() == 2);
    
Trait implementations 1