use serde::{Deserialize, Serialize};
use super::{
cubicspline::CubicSplineInterpolator, linear::LinearInterpolator,
loglinear::LogLinearInterpolator,
};
use crate::{ad::scalar::Scalar, utils::errors::Result};
pub trait StaticInterpolate<T>
where
T: Scalar,
{
fn interpolate(x: T, x_: &[T], y_: &[T], enable_extrapolation: bool) -> Result<T>;
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
pub enum Interpolator {
Linear,
LogLinear,
CubicSpline,
}
pub trait Interpolate<T>
where
T: Scalar,
{
fn interpolate(&self, x: T, x_: &[T], y_: &[T], enable_extrapolation: bool) -> Result<T>;
}
impl<T> Interpolate<T> for Interpolator
where
T: Scalar,
LinearInterpolator: StaticInterpolate<T>,
LogLinearInterpolator: StaticInterpolate<T>,
CubicSplineInterpolator: StaticInterpolate<T>,
{
fn interpolate(&self, x: T, x_: &[T], y_: &[T], enable_extrapolation: bool) -> Result<T> {
match self {
Self::Linear => LinearInterpolator::interpolate(x, x_, y_, enable_extrapolation),
Self::LogLinear => LogLinearInterpolator::interpolate(x, x_, y_, enable_extrapolation),
Self::CubicSpline => {
CubicSplineInterpolator::interpolate(x, x_, y_, enable_extrapolation)
}
}
}
}