ndarray_interp/interp1d/strategies/
mod.rs

1use std::fmt::Debug;
2
3use ndarray::{ArrayBase, ArrayViewMut, Data, Dimension, Ix1};
4use num_traits::Num;
5
6use super::Interp1D;
7use crate::{BuilderError, InterpolateError};
8
9pub mod cubic_spline;
10pub mod linear;
11
12pub trait Interp1DStrategyBuilder<Sd, Sx, D>
13where
14    Sd: Data,
15    Sd::Elem: Num + Debug + Send,
16    Sx: Data<Elem = Sd::Elem>,
17    D: Dimension,
18    Self: Sized,
19{
20    const MINIMUM_DATA_LENGHT: usize;
21    type FinishedStrat: Interp1DStrategy<Sd, Sx, D>;
22
23    /// initialize the strategy by validating data and
24    /// possibly calculating coefficients
25    /// This method is called in [`Interp1DBuilder::build`](crate::interp1d::Interp1DBuilder::build)
26    ///
27    /// When this method is called by [`Interp1DBuilder`](crate::interp1d::Interp1DBuilder) the
28    /// following properties are guaranteed:
29    ///  - x is strictly monotonically rising
30    ///  - the lenght of x equals the lenght of the data Axis 0
31    ///  - the lenght is at least `MINIMUM_DATA_LENGHT`
32    ///  - Interpolation will happen along axis 0
33    fn build<Sx2>(
34        self,
35        x: &ArrayBase<Sx2, Ix1>,
36        data: &ArrayBase<Sd, D>,
37    ) -> Result<Self::FinishedStrat, BuilderError>
38    where
39        Sx2: Data<Elem = Sd::Elem>;
40}
41
42pub trait Interp1DStrategy<Sd, Sx, D>
43where
44    Sd: Data,
45    Sd::Elem: Num + Debug + Send,
46    Sx: Data<Elem = Sd::Elem>,
47    D: Dimension,
48    Self: Sized,
49{
50    /// Interpolate the at position x into the target array.
51    /// This is used internally by [`Interp1D`].
52    ///
53    /// When called by [`Interp1D`] the following
54    /// properties are guaranteed:
55    ///  - The shape of the target array matches the
56    ///     shape of the data array (provided to the builder)
57    ///     with the first axis removed.
58    ///  - x can be any valid `Sx::Elem`
59    fn interp_into(
60        &self,
61        interpolator: &Interp1D<Sd, Sx, D, Self>,
62        target: ArrayViewMut<Sd::Elem, D::Smaller>,
63        x: Sx::Elem,
64    ) -> Result<(), InterpolateError>;
65}