Skip to main content

conspire/math/interpolate/
mod.rs

1#[cfg(test)]
2mod test;
3
4use super::{Scalar, Tensor, TensorVec, Vector, integrate::IntegrationError};
5use std::ops::{Mul, Sub};
6
7/// Linear interpolation schemes.
8pub struct LinearInterpolation {}
9
10/// One-dimensional interpolation schemes.
11pub trait Interpolate1D<F, T>
12where
13    F: TensorVec<Item = T>,
14    T: Tensor,
15{
16    /// One-dimensional interpolation.
17    fn interpolate_1d(x: &Vector, xp: &Vector, fp: &F) -> F;
18}
19
20/// Solution interpolation schemes.
21pub trait InterpolateSolution<Y, U>
22where
23    Y: Tensor,
24    for<'a> &'a Y: Mul<Scalar, Output = Y> + Sub<&'a Y, Output = Y>,
25    U: TensorVec<Item = Y>,
26{
27    /// Solution interpolation.
28    #[allow(clippy::too_many_arguments)]
29    fn interpolate(
30        &self,
31        time: &Vector,
32        tp: &Vector,
33        yp: &U,
34        dydtp: &U,
35        k_sol: &[U],
36        function: impl FnMut(Scalar, &Y) -> Result<Y, String>,
37    ) -> Result<(U, U), IntegrationError>;
38}
39
40impl<F, T> Interpolate1D<F, T> for LinearInterpolation
41where
42    F: TensorVec<Item = T>,
43    T: Tensor,
44{
45    fn interpolate_1d(x: &Vector, xp: &Vector, fp: &F) -> F {
46        let mut i = 0;
47        x.iter()
48            .map(|x_k| {
49                i = xp.iter().position(|xp_i| xp_i > x_k).unwrap();
50                (fp[i].clone() - &fp[i - 1]) / (xp[i] - xp[i - 1]) * (x_k - xp[i - 1]) + &fp[i - 1]
51            })
52            .collect()
53    }
54}