pub struct CurveSpline<'a> { /* private fields */ }Expand description
A natural cubic spline through the points of a Curve, solved once
and read many times.
SplineInterpolation::spline_interpolate solves the tridiagonal
system for the spline’s second derivatives on every call, so each read
costs O(n) Decimal arithmetic. CurveSpline::new solves it once;
each CurveSpline::interpolate then locates the segment by binary
search and evaluates one cubic. Use it when one curve is read at many
abscissas: a resampling, a chart, a smile lookup across a chain.
§Same answers as spline_interpolate
spline.interpolate(x) returns exactly what curve.spline_interpolate(x)
returns, to the last digit and including the error and its message: the
same pre-checks run in the same order, the solve and the evaluation are
the same code, and a curve whose system cannot be solved (a repeated
abscissa, an overflowing step) reports that error on every read that
reaches the solve, as spline_interpolate does.
The spline borrows the curve, so the curve cannot change while the solved system is in use.
§Examples
use optionstratlib_math::curves::{Curve, CurveSpline, Point2D};
use optionstratlib_math::geometrics::SplineInterpolation;
use rust_decimal_macros::dec;
let curve = Curve::new(
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]
.into_iter()
.map(|(x, y)| Point2D::new(x, y))
.collect(),
);
let spline = CurveSpline::new(&curve);
for x in [dec!(0.5), dec!(1.25), dec!(3.9)] {
assert_eq!(
spline.interpolate(x).unwrap(),
curve.spline_interpolate(x).unwrap()
);
}
assert!(spline.interpolate(dec!(5)).is_err());Implementations§
Source§impl<'a> CurveSpline<'a>
impl<'a> CurveSpline<'a>
Sourcepub fn new(curve: &'a Curve) -> Self
pub fn new(curve: &'a Curve) -> Self
Solves the natural cubic spline through curve’s points.
Never fails: a curve the spline cannot be built on reports why on
each CurveSpline::interpolate call that needs the solve, exactly
as SplineInterpolation::spline_interpolate would.
Sourcepub fn interpolate(&self, x: Decimal) -> Result<Point2D, InterpolationError>
pub fn interpolate(&self, x: Decimal) -> Result<Point2D, InterpolationError>
Reads the spline at x.
§Errors
The errors of SplineInterpolation::spline_interpolate at the same
x, with the same messages:
InterpolationError::Splinewhen the curve has fewer than three points, whenxis outside its knots, or when a checked step leaves theDecimalrange;InterpolationError::DegenerateIntervalwhen several points sit atx, or when a repeated abscissa elsewhere collapses a knot interval.
Trait Implementations§
Source§impl<'a> Clone for CurveSpline<'a>
impl<'a> Clone for CurveSpline<'a>
Auto Trait Implementations§
impl<'a> Freeze for CurveSpline<'a>
impl<'a> RefUnwindSafe for CurveSpline<'a>
impl<'a> Send for CurveSpline<'a>
impl<'a> Sync for CurveSpline<'a>
impl<'a> Unpin for CurveSpline<'a>
impl<'a> UnsafeUnpin for CurveSpline<'a>
impl<'a> UnwindSafe for CurveSpline<'a>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.