pub struct HermiteInterp<T, const N: usize> { /* private fields */ }Expand description
Cubic Hermite interpolant (fixed-size, stack-allocated).
Uses user-supplied derivatives at each knot. Each segment is a cubic polynomial matching both value and first derivative at its endpoints. Requires at least 2 points.
§Example
use numeris::interp::HermiteInterp;
// Interpolate sin(x) with known derivatives cos(x)
let xs = [0.0_f64, 1.0, 2.0];
let ys = [0.0_f64.sin(), 1.0_f64.sin(), 2.0_f64.sin()];
let dys = [0.0_f64.cos(), 1.0_f64.cos(), 2.0_f64.cos()];
let interp = HermiteInterp::new(xs, ys, dys).unwrap();
let mid = 0.5;
assert!((interp.eval(mid) - mid.sin()).abs() < 0.002);Implementations§
Source§impl<T: FloatScalar, const N: usize> HermiteInterp<T, N>
impl<T: FloatScalar, const N: usize> HermiteInterp<T, N>
Sourcepub fn new(xs: [T; N], ys: [T; N], dys: [T; N]) -> Result<Self, InterpError>
pub fn new(xs: [T; N], ys: [T; N], dys: [T; N]) -> Result<Self, InterpError>
Construct a Hermite interpolant from knots and derivatives.
Examples found in repository?
docs/examples/gen_plots.rs (line 205)
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 interpolant at x.
Examples found in repository?
docs/examples/gen_plots.rs (line 221)
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 interpolant and its derivative at x.
Trait Implementations§
Source§impl<T: Clone, const N: usize> Clone for HermiteInterp<T, N>
impl<T: Clone, const N: usize> Clone for HermiteInterp<T, N>
Source§fn clone(&self) -> HermiteInterp<T, N>
fn clone(&self) -> HermiteInterp<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 HermiteInterp<T, N>where
T: Freeze,
impl<T, const N: usize> RefUnwindSafe for HermiteInterp<T, N>where
T: RefUnwindSafe,
impl<T, const N: usize> Send for HermiteInterp<T, N>where
T: Send,
impl<T, const N: usize> Sync for HermiteInterp<T, N>where
T: Sync,
impl<T, const N: usize> Unpin for HermiteInterp<T, N>where
T: Unpin,
impl<T, const N: usize> UnsafeUnpin for HermiteInterp<T, N>where
T: UnsafeUnpin,
impl<T, const N: usize> UnwindSafe for HermiteInterp<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