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