pub struct Spline<I, T> {
pub interp: I,
pub xa: Vec<T>,
pub ya: Vec<T>,
/* private fields */
}
Expand description
1D Higher level interface.
A Spline owns the data it is constructed with, and provides the same evalulation methods as the lower-level Interpolator object, without needing to provide the data arrays in every call.
§Example
let mut acc = Accelerator::new();
let xa = [0.0, 1.0, 2.0, 3.0, 4.0];
let ya = [0.0, 2.0, 4.0, 6.0, 8.0];
let interp = Cubic.build(&xa, &ya)?;
let typ = Cubic;
let spline = Spline::build(typ, &xa, &ya)?;
let x = 1.5;
let y_interp = interp.eval(&xa, &ya, x, &mut acc)?;
let y_spline = spline.eval(x, &mut acc)?;
assert_eq!(y_interp, y_spline);
Fields§
§interp: I
The lower-level Interpolator
.
xa: Vec<T>
The owned x data.
ya: Vec<T>
The owned y data.
Implementations§
Source§impl<I, T> Spline<I, T>
impl<I, T> Spline<I, T>
Sourcepub fn build(
typ: impl InterpType<T, Interpolator = I>,
xa: &[T],
ya: &[T],
) -> Result<Self, InterpolationError>
pub fn build( typ: impl InterpType<T, Interpolator = I>, xa: &[T], ya: &[T], ) -> Result<Self, InterpolationError>
Constructs a Spline of an Interpolation type typ
from the data arrays xa
and ya
.
§Example
let xa = [0.0, 1.0, 2.0, 3.0, 4.0];
let ya = [0.0, 2.0, 4.0, 6.0, 8.0];
let typ = Cubic;
let spline = Spline::build(typ, &xa, &ya)?;
Sourcepub fn eval(&self, x: T, acc: &mut Accelerator) -> Result<T, DomainError>
pub fn eval(&self, x: T, acc: &mut Accelerator) -> Result<T, DomainError>
Returns the interpolated value y
for a given point x
, using the Accelerator
acc
.
§Example
let mut acc = Accelerator::new();
let xa = [0.0, 1.0, 2.0, 3.0, 4.0];
let ya = [0.0, 2.0, 4.0, 6.0, 8.0];
let typ = Cubic;
let spline = Spline::build(typ, &xa, &ya)?;
let y = spline.eval(1.5, &mut acc)?;
assert_eq!(y, 3.0);
§Errors
Returns a DomainError
if x
is outside the range of xa
.
Sourcepub fn eval_deriv(&self, x: T, acc: &mut Accelerator) -> Result<T, DomainError>
pub fn eval_deriv(&self, x: T, acc: &mut Accelerator) -> Result<T, DomainError>
Returns the derivative dy/dx
of an interpolated function for a given point x
, using the
Accelerator
acc
.
§Example
let mut acc = Accelerator::new();
let xa = [0.0, 1.0, 2.0, 3.0, 4.0];
let ya = [0.0, 2.0, 4.0, 6.0, 8.0];
let typ = Cubic;
let spline = Spline::build(typ, &xa, &ya)?;
let dydx = spline.eval_deriv(1.5, &mut acc)?;
assert_eq!(dydx, 2.0);
§Errors
Returns a DomainError
if x
is outside the range of xa
.
Sourcepub fn eval_deriv2(&self, x: T, acc: &mut Accelerator) -> Result<T, DomainError>
pub fn eval_deriv2(&self, x: T, acc: &mut Accelerator) -> Result<T, DomainError>
Returns the second derivative d²y/dx²
of an interpolated function for a given point x
, using the
Accelerator
acc
.
§Example
let mut acc = Accelerator::new();
let xa = [0.0, 1.0, 2.0, 3.0, 4.0];
let ya = [0.0, 2.0, 4.0, 6.0, 8.0];
let typ = Cubic;
let spline = Spline::build(typ, &xa, &ya)?;
let dydx = spline.eval_deriv2(1.5, &mut acc)?;
assert_eq!(dydx, 0.0);
§Errors
Returns a DomainError
if x
is outside the range of xa
.
Sourcepub fn eval_integ(
&self,
a: T,
b: T,
acc: &mut Accelerator,
) -> Result<T, DomainError>
pub fn eval_integ( &self, a: T, b: T, acc: &mut Accelerator, ) -> Result<T, DomainError>
Returns the numerical integral of an interpolated function over the range [a
,b
], using the
Accelerator
acc
.
§Example
let mut acc = Accelerator::new();
let xa = [0.0, 1.0, 2.0, 3.0, 4.0];
let ya = [0.0, 2.0, 4.0, 6.0, 8.0];
let typ = Cubic;
let spline = Spline::build(typ, &xa, &ya)?;
let int = spline.eval_integ(0.0, 2.0, &mut acc)?;
assert_eq!(int, 4.0);
§Errors
Returns a DomainError
if a
or b
is outside the range of xa.