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>
impl<T: FloatScalar, const N: usize> CubicSpline<T, N>
Sourcepub fn new(xs: [T; N], ys: [T; N]) -> Result<Self, InterpError>
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}Sourcepub fn eval(&self, x: T) -> T
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}Sourcepub fn eval_derivative(&self, x: T) -> (T, T)
pub fn eval_derivative(&self, x: T) -> (T, T)
Evaluate the spline and its derivative at x.
Trait Implementations§
Source§impl<T: Clone, const N: usize> Clone for CubicSpline<T, N>
impl<T: Clone, const N: usize> Clone for CubicSpline<T, N>
Source§fn clone(&self) -> CubicSpline<T, N>
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more