packages

interpolate::bspline

On this page

B-splines: a spline of degree k as coefficients on a knot vector, evaluated by de Boor's algorithm, with derivatives, antiderivatives, integrals and roots; built by interpolation (make_interp_spline), least squares (make_lsq_spline) or smoothing (splrep).

Structs

BSplineSource

pub struct BSpline

A spline of degree k, s(x) = sum_i c[i] B[i, k](x), on the knots t (n + k + 1 of them, non-decreasing) with n coefficients. Its base interval is [t[k], t[n]]; outside it the end pieces continue (Extrapolate::Extend). Evaluation is de Boor's algorithm, O(log n + k^2), stable for any degree.

nabi
use interpolate::{BSpline, Curve};

// Bernstein form on clamped knots: x^2 on [0, 1].
let b = try BSpline::new([0, 0, 0, 1, 1, 1], [0, 0, 1], 2);
assert(b.at(0.5) == 0.25 && b.at(1.0) == 1.0);
  • pub fn new(t: Vector<f64>, c: Vector<f64>, k: usize) throws (InterpError) -> BSpline§

    Source

    The spline with knots t, coefficients c and degree k: t.len() == c.len() + k + 1, t non-decreasing, with a nonempty base interval.

    nabi
    use interpolate::{BSpline, Curve};
    
    // Coefficients 0, 1, 2 on clamped linear knots: the line y = x.
    let b = try BSpline::new([0, 0, 1, 2, 2], [0, 1, 2], 1);
    assert(b.at(1.25) == 1.25);
    
  • pub fn extrapolate(self, mode: Extrapolate) -> BSpline§

    Source

    This spline with extrapolation mode.

    nabi
    use interpolate::{Curve, Extrapolate};
    
    let b = try interpolate::make_interp_spline([0, 1, 2, 3], [0, 1, 4, 9]);
    assert(b.extrapolate(Extrapolate::Clamp).at(5.0) == 9.0);
    
  • pub fn knots(self) -> Vector<f64>§

    Source

    The knots.

    nabi
    let b = try interpolate::make_interp_spline([0, 1, 2, 3], [0, 1, 4, 9]);
    assert(b.knots().len() == b.coefficients().len() + 4);
    
  • pub fn coefficients(self) -> Vector<f64>§

    Source

    The coefficients.

    nabi
    let b = try interpolate::make_interp_spline([0, 1], [3, 5], 1);
    assert(b.coefficients()[1] == 5.0);
    
  • pub fn degree(self) -> usize§

    Source

    The degree.

  • pub fn domain(self) -> (f64, f64)§

    Source

    The base interval (t[k], t[n]).

    nabi
    let b = try interpolate::make_interp_spline([1, 2, 3, 4], [0, 1, 0, 1]);
    assert(b.domain() == (1.0, 4.0));
    
  • pub fn derivative(self, nu: usize = 1) -> BSpline§

    Source

    The nu-th derivative, a spline of degree k - nu on the inner knots (zero once nu > k).

    nabi
    use interpolate::Curve;
    
    let b = try interpolate::make_interp_spline([0, 1, 2, 3, 4], [0, 1, 8, 27, 64]);
    assert((b.derivative(1).at(2.5) - 18.75).abs() < 1e-12);
    
  • pub fn antiderivative(self, nu: usize = 1) -> BSpline§

    Source

    The nu-th antiderivative, a spline of degree k + nu, zero at the start of the base interval.

    nabi
    use interpolate::Curve;
    
    let b = try interpolate::make_interp_spline([0, 1, 2, 3], [1, 1, 1, 1]);
    assert((b.antiderivative(1).at(3.0) - 3.0).abs() < 1e-14);
    
  • pub fn to_ppoly(self) -> PPoly§

    Source

    The same function as a PPoly on the distinct knots of the base interval: each piece's Taylor coefficients at its left end, from the derivatives.

    nabi
    use interpolate::Curve;
    
    let b = try interpolate::make_interp_spline([0, 1, 2, 3, 4], [0, 1, 0, 1, 0]);
    let p = b.to_ppoly();
    assert((p.at(2.7) - b.at(2.7)).abs() < 1e-14);
    
  • pub fn integrate(self, r: Range<f64>) -> f64§

    Source

    The integral over r, following the extrapolation mode as PPoly::integrate does.

    nabi
    let b = try interpolate::make_interp_spline([0, 1, 2, 3], [0, 1, 4, 9]);
    assert((b.integrate(0.0..3.0) - 9.0).abs() < 1e-13);
    
  • pub fn solve(self, y: f64 = 0.0) -> Vector<f64>§

    Source

    The real x in the base interval where the spline equals y, rising (see PPoly::solve).

    nabi
    let b = try interpolate::make_interp_spline([0, 1, 2, 3], [-1, 1, -1, 1]);
    assert(b.solve(0.0).len() == 3);
    
  • pub fn roots(self) -> Vector<f64>§

    Source

    The real roots in the base interval, solve(0.0).

    nabi
    let b = try interpolate::make_interp_spline([0, 1, 2], [-1, 0, 1], 1);
    assert(b.roots() == core::array::Vector::<f64>::from([1.0]));
    
Trait implementations 1

Functions

splevSource

pub fn splev<const N: usize>(x: Array<f64, N>, b: BSpline, der: usize = 0) -> Array<f64, N>

b at every element of x (its der-th derivative when der > 0), FITPACK's splev.

nabi
use core::array::Vector;

let b = try interpolate::splrep([0, 1, 2, 3], [0, 1, 4, 9]);
let xs: Vector<f64> = [1.5];
assert((interpolate::splev(xs, b, 1)[0] - 3.0).abs() < 1e-12);

splintSource

pub fn splint(r: Range<f64>, b: BSpline) -> f64

The integral of b over r, FITPACK's splint.

nabi
let b = try interpolate::splrep([0, 1, 2, 3], [2, 2, 2, 2]);
assert((interpolate::splint(0.0..3.0, b) - 6.0).abs() < 1e-14);

sprootSource

pub fn sproot(b: BSpline) -> Vector<f64>

The roots of b in its base interval, FITPACK's sproot.

nabi
let b = try interpolate::splrep([0, 1, 2, 3], [-1, 1, -1, 1]);
assert(interpolate::sproot(b).len() == 3);