neco-spline
Natural cubic spline interpolation with no dependencies beyond the standard library by default, plus an optional serde feature. Useful when you need a smooth curve through a set of data points.
A Japanese mathematical note is available in MATH-ja.md.
Natural cubic spline
Given data points $(x_0, y_0), \dots, (x_n, y_n)$, the spline constructs a piecewise cubic polynomial that passes through every point while maintaining $C^2$ continuity. On each interval the polynomial is:
$$S_i(x) = a_i + b_i(x - x_i) + c_i(x - x_i)^2 + d_i(x - x_i)^3$$
The natural boundary conditions ($S'' = 0$ at both endpoints) yield a tridiagonal system solved in $O(n)$ time by the Thomas algorithm. Among all interpolating splines, this is the unique one that minimizes total curvature $\int |f''(x)|^2 , dx$.
Usage
[]
= "0.1"
Basic interpolation
use CubicSpline;
let spline = new.unwrap;
let y = spline.evaluate;
Boundary clamping
Values outside the data range return the nearest endpoint value:
let spline = new.unwrap;
assert_eq!; // clamped to left endpoint
assert_eq!; // clamped to right endpoint
Error handling
use ;
// Fewer than 2 points
let err = new;
assert!;
// Non-ascending x values
let err = new;
assert!;
API
| Item | Description |
|---|---|
CubicSpline |
Natural cubic spline interpolator |
CubicSpline::new(points) |
Build a spline from &[(f32, f32)] (returns Result) |
CubicSpline::evaluate(x) |
Evaluate the spline at a given $x$ |
SplineError::InsufficientPoints |
Fewer than 2 control points |
SplineError::NonAscendingX |
Control points not in strictly ascending $x$ order |
Optional features
| Feature | Description |
|---|---|
serde |
Enables Serialize / Deserialize for CubicSpline |
License
MIT