Skip to main content

CubicSpline

Struct CubicSpline 

Source
pub struct CubicSpline<T, const N: usize> { /* private fields */ }
Expand description

Natural cubic spline interpolant (fixed-size, stack-allocated).

Uses natural boundary conditions (S’‘(x₀) = S’’(x_{N-1}) = 0). The tridiagonal system for second derivatives is solved via the Thomas algorithm in O(N). Requires at least 3 points.

Each segment stores coefficients [a, b, c, d] for: S_i(x) = a + b·(x - x_i) + c·(x - x_i)² + d·(x - x_i)³

§Example

use numeris::interp::CubicSpline;

let xs = [0.0_f64, 1.0, 2.0, 3.0];
let ys = [0.0, 1.0, 0.0, 1.0];
let spline = CubicSpline::new(xs, ys).unwrap();

// Passes through knots exactly
assert!((spline.eval(0.0) - 0.0).abs() < 1e-14);
assert!((spline.eval(1.0) - 1.0).abs() < 1e-14);
assert!((spline.eval(2.0) - 0.0).abs() < 1e-14);

Implementations§

Source§

impl<T: FloatScalar, const N: usize> CubicSpline<T, N>

Source

pub fn new(xs: [T; N], ys: [T; N]) -> Result<Self, InterpError>

Construct a natural cubic spline from sorted knots.

Returns InterpError::TooFewPoints if N < 3, InterpError::NotSorted if xs is not strictly increasing.

Examples found in repository?
docs/examples/gen_plots.rs (line 207)
198fn make_interp_plot() -> String {
199    let tau = 2.0 * PI;
200    let kx: [f64; 6] = core::array::from_fn(|i| tau * i as f64 / 5.0);
201    let ky: [f64; 6] = core::array::from_fn(|i| kx[i].sin());
202    let kd: [f64; 6] = core::array::from_fn(|i| kx[i].cos());
203
204    let linear = LinearInterp::new(kx, ky).unwrap();
205    let hermite = HermiteInterp::new(kx, ky, kd).unwrap();
206    let lagrange = LagrangeInterp::new(kx, ky).unwrap();
207    let spline = CubicSpline::new(kx, ky).unwrap();
208
209    const N: usize = 200;
210    let mut xv = vec![0.0; N];
211    let mut y_true = vec![0.0; N];
212    let mut y_lin = vec![0.0; N];
213    let mut y_her = vec![0.0; N];
214    let mut y_lag = vec![0.0; N];
215    let mut y_spl = vec![0.0; N];
216    for i in 0..N {
217        let xi = tau * i as f64 / (N - 1) as f64;
218        xv[i] = xi;
219        y_true[i] = xi.sin();
220        y_lin[i] = linear.eval(xi);
221        y_her[i] = hermite.eval(xi);
222        y_lag[i] = lagrange.eval(xi);
223        y_spl[i] = spline.eval(xi);
224    }
225
226    let traces = format!(
227        "[{{\"type\":\"scatter\",\"mode\":\"lines\",\"name\":\"sin(x) exact\",\
228          \"x\":{},\"y\":{},\"line\":{{\"width\":2.5,\"color\":\"rgba(120,120,120,0.6)\",\"dash\":\"dot\"}}}},\
229         {{\"type\":\"scatter\",\"mode\":\"lines\",\"name\":\"Linear\",\
230          \"x\":{},\"y\":{},\"line\":{{\"width\":2}}}},\
231         {{\"type\":\"scatter\",\"mode\":\"lines\",\"name\":\"Hermite\",\
232          \"x\":{},\"y\":{},\"line\":{{\"width\":2}}}},\
233         {{\"type\":\"scatter\",\"mode\":\"lines\",\"name\":\"Lagrange\",\
234          \"x\":{},\"y\":{},\"line\":{{\"width\":2}}}},\
235         {{\"type\":\"scatter\",\"mode\":\"lines\",\"name\":\"Cubic Spline\",\
236          \"x\":{},\"y\":{},\"line\":{{\"width\":2}}}},\
237         {{\"type\":\"scatter\",\"mode\":\"markers\",\"name\":\"knots\",\
238          \"x\":{},\"y\":{},\"marker\":{{\"size\":9,\"color\":\"black\",\"symbol\":\"diamond\"}}}}]",
239        fmt_arr(&xv), fmt_arr(&y_true),
240        fmt_arr(&xv), fmt_arr(&y_lin),
241        fmt_arr(&xv), fmt_arr(&y_her),
242        fmt_arr(&xv), fmt_arr(&y_lag),
243        fmt_arr(&xv), fmt_arr(&y_spl),
244        fmt_arr(&kx), fmt_arr(&ky),
245    );
246
247    let layout = decorate_layout(
248        "Interpolation Methods on sin(x) — 6 Knots",
249        "x",
250        "y",
251        "",
252    );
253
254    plotly_snippet("plot-interp", &traces, &layout, 440)
255}
Source

pub fn eval(&self, x: T) -> T

Evaluate the spline at x.

Examples found in repository?
docs/examples/gen_plots.rs (line 223)
198fn make_interp_plot() -> String {
199    let tau = 2.0 * PI;
200    let kx: [f64; 6] = core::array::from_fn(|i| tau * i as f64 / 5.0);
201    let ky: [f64; 6] = core::array::from_fn(|i| kx[i].sin());
202    let kd: [f64; 6] = core::array::from_fn(|i| kx[i].cos());
203
204    let linear = LinearInterp::new(kx, ky).unwrap();
205    let hermite = HermiteInterp::new(kx, ky, kd).unwrap();
206    let lagrange = LagrangeInterp::new(kx, ky).unwrap();
207    let spline = CubicSpline::new(kx, ky).unwrap();
208
209    const N: usize = 200;
210    let mut xv = vec![0.0; N];
211    let mut y_true = vec![0.0; N];
212    let mut y_lin = vec![0.0; N];
213    let mut y_her = vec![0.0; N];
214    let mut y_lag = vec![0.0; N];
215    let mut y_spl = vec![0.0; N];
216    for i in 0..N {
217        let xi = tau * i as f64 / (N - 1) as f64;
218        xv[i] = xi;
219        y_true[i] = xi.sin();
220        y_lin[i] = linear.eval(xi);
221        y_her[i] = hermite.eval(xi);
222        y_lag[i] = lagrange.eval(xi);
223        y_spl[i] = spline.eval(xi);
224    }
225
226    let traces = format!(
227        "[{{\"type\":\"scatter\",\"mode\":\"lines\",\"name\":\"sin(x) exact\",\
228          \"x\":{},\"y\":{},\"line\":{{\"width\":2.5,\"color\":\"rgba(120,120,120,0.6)\",\"dash\":\"dot\"}}}},\
229         {{\"type\":\"scatter\",\"mode\":\"lines\",\"name\":\"Linear\",\
230          \"x\":{},\"y\":{},\"line\":{{\"width\":2}}}},\
231         {{\"type\":\"scatter\",\"mode\":\"lines\",\"name\":\"Hermite\",\
232          \"x\":{},\"y\":{},\"line\":{{\"width\":2}}}},\
233         {{\"type\":\"scatter\",\"mode\":\"lines\",\"name\":\"Lagrange\",\
234          \"x\":{},\"y\":{},\"line\":{{\"width\":2}}}},\
235         {{\"type\":\"scatter\",\"mode\":\"lines\",\"name\":\"Cubic Spline\",\
236          \"x\":{},\"y\":{},\"line\":{{\"width\":2}}}},\
237         {{\"type\":\"scatter\",\"mode\":\"markers\",\"name\":\"knots\",\
238          \"x\":{},\"y\":{},\"marker\":{{\"size\":9,\"color\":\"black\",\"symbol\":\"diamond\"}}}}]",
239        fmt_arr(&xv), fmt_arr(&y_true),
240        fmt_arr(&xv), fmt_arr(&y_lin),
241        fmt_arr(&xv), fmt_arr(&y_her),
242        fmt_arr(&xv), fmt_arr(&y_lag),
243        fmt_arr(&xv), fmt_arr(&y_spl),
244        fmt_arr(&kx), fmt_arr(&ky),
245    );
246
247    let layout = decorate_layout(
248        "Interpolation Methods on sin(x) — 6 Knots",
249        "x",
250        "y",
251        "",
252    );
253
254    plotly_snippet("plot-interp", &traces, &layout, 440)
255}
Source

pub fn eval_derivative(&self, x: T) -> (T, T)

Evaluate the spline and its derivative at x.

Source

pub fn xs(&self) -> &[T; N]

The knot x-values.

Trait Implementations§

Source§

impl<T: Clone, const N: usize> Clone for CubicSpline<T, N>

Source§

fn clone(&self) -> CubicSpline<T, N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, const N: usize> Debug for CubicSpline<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for CubicSpline<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for CubicSpline<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for CubicSpline<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for CubicSpline<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for CubicSpline<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnsafeUnpin for CubicSpline<T, N>
where T: UnsafeUnpin,

§

impl<T, const N: usize> UnwindSafe for CubicSpline<T, N>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.