pub enum Interpolator {
Interp0D(f64),
Interp1D(Interp1D),
Interp2D(Interp2D),
Interp3D(Interp3D),
InterpND(InterpND),
}Expand description
An interpolator, with different functionality depending on variant.
Interpolation is executed by calling Interpolator::interpolate.
The length of the supplied point slice must be equal to the intepolator dimensionality.
The interpolator dimensionality can be retrieved by calling Interpolator::ndim.
§Note
For interpolators of dimensionality N ≥ 1:
- By design, instantiation must be done via the Interpolator enum’s
new_*methods (new_1d,new_2d,new_3d,new_nd). These run a validation step that catches any potential errors early.- To set or get field values, use the corresponding named methods (
x,set_x, etc.).
- To set or get field values, use the corresponding named methods (
- An interpolation
Strategy(e.g. linear, left-nearest, etc.) must be specified. Not all interpolation strategies are implemented for every dimensionality.Strategy::Linearis implemented for all dimensionalities. - An
Extrapolatesetting must be specified. This controls what happens when a point is beyond the range of supplied coordinates. If you are unsure which variant to choose,Extrapolate::Erroris likely what you want.
For 0D (constant-value) interpolators, instantiate directly, e.g. Interpolator::Interp0D(0.5)
Variants§
Interp0D(f64)
Useful for returning a constant-value result from an interpolator.
§Example:
use ninterp::*;
// 0-D is unique, the value is directly provided in the variant
let const_value = 0.5;
let interp = Interpolator::Interp0D(const_value);
assert_eq!(
interp.interpolate(&[]).unwrap(), // an empty point is required for 0-D
const_value
);Interp1D(Interp1D)
Interpolates in one dimension.
Applicable interpolation strategies:
Applicable extrapolation strategies:
§Example (linear, using Extrapolate::Enable):
use ninterp::prelude::*;
// f(x) = 0.2 * x + 0.2
let interp = Interpolator::new_1d(
// x
vec![0., 1., 2.], // x0, x1, x2
// f(x)
vec![0.2, 0.4, 0.6], // f(x0), f(x1), f(x2)
Strategy::Linear, // linear interpolation
Extrapolate::Enable, // linearly extrapolate when point is out of bounds
)
.unwrap(); // handle data validation results
assert_eq!(interp.interpolate(&[1.5]).unwrap(), 0.5);
assert_eq!(interp.interpolate(&[-1.]).unwrap(), 0.); // extrapolation below grid
assert_eq!(interp.interpolate(&[2.2]).unwrap(), 0.64); // extrapolation above gridInterp2D(Interp2D)
Interpolates in two dimensions.
Applicable interpolation strategies:
Applicable extrapolation strategies:
§Example (using Extrapolate::Clamp):
use ninterp::prelude::*;
// f(x, y) = 0.2 * x + 0.4 * y
let interp = Interpolator::new_2d(
// x
vec![0., 1., 2.], // x0, x1, x2
// y
vec![0., 1., 2.], // y0, y1, y2
// f(x, y)
vec![
vec![0.0, 0.4, 0.8], // f(x0, y0), f(x0, y1), f(x0, y2)
vec![0.2, 0.6, 1.0], // f(x1, y0), f(x1, y1), f(x1, y2)
vec![0.4, 0.8, 1.2], // f(x2, y0), f(x2, y1), f(x2, y2)
],
Strategy::Linear,
Extrapolate::Clamp, // restrict point within grid bounds
)
.unwrap();
assert_eq!(interp.interpolate(&[1.5, 1.5]).unwrap(), 0.9);
assert_eq!(
interp.interpolate(&[-1., 2.5]).unwrap(),
interp.interpolate(&[0., 2.]).unwrap()
); // point is restricted to within grid boundsInterp3D(Interp3D)
Interpolates in three dimensions.
Applicable interpolation strategies:
Applicable extrapolation strategies:
§Example (using Extrapolate::Error):
use ninterp::prelude::*;
// f(x, y, z) = 0.2 * x + 0.2 * y + 0.2 * z
let interp = Interpolator::new_3d(
// x
vec![1., 2.], // x0, x1
// y
vec![1., 2.], // y0, y1
// z
vec![1., 2.], // z0, z1
// f(x, y, z)
vec![
vec![
vec![0.6, 0.8], // f(x0, y0, z0), f(x0, y0, z1)
vec![0.8, 1.0], // f(x0, y1, z0), f(x0, y1, z1)
],
vec![
vec![0.8, 1.0], // f(x1, y0, z0), f(x1, y0, z1)
vec![1.0, 1.2], // f(x1, y1, z0), f(x1, y1, z1)
],
],
Strategy::Linear,
Extrapolate::Error, // return an error when point is out of bounds
)
.unwrap();
assert_eq!(interp.interpolate(&[1.5, 1.5, 1.5]).unwrap(), 0.9);
// out of bounds point with `Extrapolate::Error` fails
assert!(matches!(
interp.interpolate(&[2.5, 2.5, 2.5]).unwrap_err(),
ninterp::error::InterpolationError::ExtrapolationError(_)
));InterpND(InterpND)
Interpolates with any dimensionality.
Applicable interpolation strategies:
Applicable extrapolation strategies:
§Example (using Extrapolate::Error):
use ninterp::prelude::*;
use ndarray::array;
// f(x, y, z) = 0.2 * x + 0.2 * y + 0.2 * z
let interp = Interpolator::new_nd(
// grid
vec![
vec![1., 2.], // x0, x1
vec![1., 2.], // y0, y1
vec![1., 2.], // z0, z1
],
// values
array![
[
[0.6, 0.8], // f(x0, y0, z0), f(x0, y0, z1)
[0.8, 1.0], // f(x0, y1, z0), f(x0, y1, z1)
],
[
[0.8, 1.0], // f(x1, y0, z0), f(x1, y0, z1)
[1.0, 1.2], // f(x1, y1, z0), f(x1, y1, z1)
],
].into_dyn(),
Strategy::Linear,
Extrapolate::Error, // return an error when point is out of bounds
)
.unwrap();
assert_eq!(interp.interpolate(&[1.5, 1.5, 1.5]).unwrap(), 0.9);
// out of bounds point with `Extrapolate::Error` fails
assert!(matches!(
interp.interpolate(&[2.5, 2.5, 2.5]).unwrap_err(),
ninterp::error::InterpolationError::ExtrapolationError(_)
));Implementations§
Source§impl Interpolator
impl Interpolator
pub fn new_1d( x: Vec<f64>, f_x: Vec<f64>, strategy: Strategy, extrapolate: Extrapolate, ) -> Result<Self, ValidationError>
pub fn new_2d( x: Vec<f64>, y: Vec<f64>, f_xy: Vec<Vec<f64>>, strategy: Strategy, extrapolate: Extrapolate, ) -> Result<Self, ValidationError>
pub fn new_3d( x: Vec<f64>, y: Vec<f64>, z: Vec<f64>, f_xyz: Vec<Vec<Vec<f64>>>, strategy: Strategy, extrapolate: Extrapolate, ) -> Result<Self, ValidationError>
pub fn new_nd( grid: Vec<Vec<f64>>, values: ArrayD<f64>, strategy: Strategy, extrapolate: Extrapolate, ) -> Result<Self, ValidationError>
Source§impl Interpolator
impl Interpolator
Sourcepub fn set_strategy(&mut self, strategy: Strategy) -> Result<(), Error>
pub fn set_strategy(&mut self, strategy: Strategy) -> Result<(), Error>
Set strategy field on any interpolator.
Sourcepub fn extrapolate(&self) -> Result<&Extrapolate, Error>
pub fn extrapolate(&self) -> Result<&Extrapolate, Error>
Get extrapolate field from any interpolator.
Sourcepub fn set_extrapolate(&mut self, extrapolate: Extrapolate) -> Result<(), Error>
pub fn set_extrapolate(&mut self, extrapolate: Extrapolate) -> Result<(), Error>
Set extrapolate field on any interpolator.
Sourcepub fn set_x(&mut self, x: Vec<f64>) -> Result<(), Error>
pub fn set_x(&mut self, x: Vec<f64>) -> Result<(), Error>
Set x field on 1D/2D/3D interpolator.
Sourcepub fn set_f_x(&mut self, f_x: Vec<f64>) -> Result<(), Error>
pub fn set_f_x(&mut self, f_x: Vec<f64>) -> Result<(), Error>
Set f_x field on 1D interpolator.
Sourcepub fn set_f_xy(&mut self, f_xy: Vec<Vec<f64>>) -> Result<(), Error>
pub fn set_f_xy(&mut self, f_xy: Vec<Vec<f64>>) -> Result<(), Error>
Set f_xy field on 2D interpolator.
Sourcepub fn set_f_xyz(&mut self, f_xyz: Vec<Vec<Vec<f64>>>) -> Result<(), Error>
pub fn set_f_xyz(&mut self, f_xyz: Vec<Vec<Vec<f64>>>) -> Result<(), Error>
Set f_xyz field on 3D interpolator.
Trait Implementations§
Source§impl Clone for Interpolator
impl Clone for Interpolator
Source§fn clone(&self) -> Interpolator
fn clone(&self) -> Interpolator
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Interpolator
impl Debug for Interpolator
Source§impl InterpMethods for Interpolator
impl InterpMethods for Interpolator
Source§fn interpolate(&self, point: &[f64]) -> Result<f64, InterpolationError>
fn interpolate(&self, point: &[f64]) -> Result<f64, InterpolationError>
Interpolate at supplied point, after checking point validity. Length of supplied point must match interpolator dimensionality.
Source§impl PartialEq for Interpolator
impl PartialEq for Interpolator
impl StructuralPartialEq for Interpolator
Auto Trait Implementations§
impl Freeze for Interpolator
impl RefUnwindSafe for Interpolator
impl Send for Interpolator
impl Sync for Interpolator
impl Unpin for Interpolator
impl UnwindSafe for Interpolator
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> 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 more