use std::ops::{Div, Mul, Sub};
use RustQuant_error::RustQuantError;
pub mod linear_interpolator;
pub use linear_interpolator::*;
pub mod exponential_interpolator;
pub use exponential_interpolator::*;
pub trait InterpolationValue: num::Num + std::fmt::Debug + Copy + Clone + Sized {}
pub trait InterpolationIndex:
Sub<Self, Output = Self::Delta> + PartialOrd + Copy + Clone + Sized
{
type Delta: Div<Self::Delta, Output = Self::DeltaDiv>
+ Mul<Self::DeltaDiv, Output = Self::Delta>;
type DeltaDiv: InterpolationValue;
}
pub trait Interpolator<IndexType, ValueType>
where
IndexType: InterpolationIndex,
ValueType: InterpolationValue,
{
fn fit(&mut self) -> Result<(), RustQuantError>;
fn interpolate(&self, point: IndexType) -> Result<ValueType, RustQuantError>;
fn range(&self) -> (IndexType, IndexType);
fn add_point(&mut self, point: (IndexType, ValueType));
}
impl<T> InterpolationValue for T where T: num::Num + std::fmt::Debug + Copy + Clone + Sized {}
macro_rules! impl_interpolation_index {
($a:ty, $b:ty, $c:ty) => {
impl InterpolationIndex for $a {
type Delta = $b;
type DeltaDiv = $c;
}
};
}
impl_interpolation_index!(i8, i8, i8);
impl_interpolation_index!(i16, i16, i16);
impl_interpolation_index!(i32, i32, i32);
impl_interpolation_index!(i64, i64, i64);
impl_interpolation_index!(i128, i128, i128);
impl_interpolation_index!(isize, isize, isize);
impl_interpolation_index!(u8, u8, u8);
impl_interpolation_index!(u16, u16, u16);
impl_interpolation_index!(u32, u32, u32);
impl_interpolation_index!(u64, u64, u64);
impl_interpolation_index!(u128, u128, u128);
impl_interpolation_index!(usize, usize, usize);
impl_interpolation_index!(f32, f32, f32);
impl_interpolation_index!(f64, f64, f64);
impl_interpolation_index!(time::Date, time::Duration, f64);
impl_interpolation_index!(time::Time, time::Duration, f64);
impl_interpolation_index!(time::OffsetDateTime, time::Duration, f64);
impl_interpolation_index!(time::PrimitiveDateTime, time::Duration, f64);
impl_interpolation_index!(
rust_decimal::Decimal,
rust_decimal::Decimal,
rust_decimal::Decimal
);