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 BSplineA 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.
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);
Sourcepub fn new(§t: Vector<f64>, c: Vector<f64>, k: usize) throws (InterpError) -> BSpline The spline with knots
t, coefficientscand degreek:t.len() == c.len() + k + 1,tnon-decreasing, with a nonempty base interval.nabiuse 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);
Sourcepub fn extrapolate(§self, mode: Extrapolate) -> BSpline This spline with extrapolation
mode.nabiuse 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);The knots.
nabilet b = try interpolate::make_interp_spline([0, 1, 2, 3], [0, 1, 4, 9]); assert(b.knots().len() == b.coefficients().len() + 4);The coefficients.
nabilet b = try interpolate::make_interp_spline([0, 1], [3, 5], 1); assert(b.coefficients()[1] == 5.0);The degree.
The base interval
(t[k], t[n]).nabilet b = try interpolate::make_interp_spline([1, 2, 3, 4], [0, 1, 0, 1]); assert(b.domain() == (1.0, 4.0));The
nu-th derivative, a spline of degreek - nuon the inner knots (zero oncenu > k).nabiuse 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);The
nu-th antiderivative, a spline of degreek + nu, zero at the start of the base interval.nabiuse 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);The same function as a
PPolyon the distinct knots of the base interval: each piece's Taylor coefficients at its left end, from the derivatives.nabiuse 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);The integral over
r, following the extrapolation mode asPPoly::integratedoes.nabilet 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);The real
xin the base interval where the spline equalsy, rising (seePPoly::solve).nabilet b = try interpolate::make_interp_spline([0, 1, 2, 3], [-1, 1, -1, 1]); assert(b.solve(0.0).len() == 3);The real roots in the base interval,
solve(0.0).nabilet 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
impl Curve for BSpline
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.
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.
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.
let b = try interpolate::splrep([0, 1, 2, 3], [-1, 1, -1, 1]);
assert(interpolate::sproot(b).len() == 3);