77// - LICENSE-MIT.md
88// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99
10- use std:: ops:: { Div , Mul , Sub , AddAssign } ;
10+ use std:: ops:: { Div , Mul , MulAssign , Sub , Add , AddAssign , Neg } ;
1111use RustQuant_error :: RustQuantError ;
1212
1313pub mod linear_interpolator;
@@ -19,19 +19,42 @@ pub use exponential_interpolator::*;
1919pub mod b_splines;
2020pub use b_splines:: * ;
2121
22+ pub mod cubic_spline;
23+ pub use cubic_spline:: * ;
24+
25+
2226// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2327// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2428
2529/// Trait describing requirements to be interpolated.
26- pub trait InterpolationValue : num:: Num + AddAssign + std:: fmt:: Debug + Copy + Clone + Sized { }
30+ pub trait InterpolationValue : num:: Num
31+ + num:: FromPrimitive
32+ + Neg < Output = Self >
33+ + AddAssign
34+ + MulAssign
35+ + Copy
36+ + Clone
37+ + Sized
38+ + std:: fmt:: Display
39+ + std:: fmt:: Debug { }
40+
41+ /// Trait to convert a Delta type into a value of `ValueType`.
42+ pub trait IntoValue < ValueType > {
43+ /// Convert `self` into `ValueType`.
44+ fn into_value ( self ) -> ValueType ;
45+ }
2746
2847/// Trait describing requirements to be an index of interpolation.
2948pub trait InterpolationIndex :
3049 Sub < Self , Output = Self :: Delta > + PartialOrd + Copy + Clone + Sized + std:: fmt:: Display
3150{
3251 /// Type of the difference of `Self` - `Self`
3352 type Delta : Div < Self :: Delta , Output = Self :: DeltaDiv >
34- + Mul < Self :: DeltaDiv , Output = Self :: Delta > ;
53+ + Mul < Self :: DeltaDiv , Output = Self :: Delta >
54+ + Add < Self :: Delta , Output = Self :: Delta >
55+ + Sub < Self :: Delta , Output = Self :: Delta >
56+ + IntoValue < Self :: DeltaDiv >
57+ + Copy ;
3558
3659 /// Type of `Delta` / `Delta`
3760 type DeltaDiv : InterpolationValue ;
6386 fn add_point ( & mut self , point : ( IndexType , ValueType ) ) ;
6487}
6588
66- impl < T > InterpolationValue for T where T : num:: Num + AddAssign + std:: fmt:: Debug + Copy + Clone + Sized { }
89+ impl < T > InterpolationValue for T where T : num:: Num
90+ + num:: FromPrimitive
91+ + Neg < Output = Self >
92+ + AddAssign
93+ + MulAssign
94+ + Copy
95+ + Clone
96+ + Sized
97+ + std:: fmt:: Display
98+ + std:: fmt:: Debug { }
6799
68100macro_rules! impl_interpolation_index {
69101 ( $a: ty, $b: ty, $c: ty) => {
@@ -74,35 +106,57 @@ macro_rules! impl_interpolation_index {
74106 } ;
75107}
76108
109+ macro_rules! impl_num_delta_into_value {
110+ ( $b: ty, $c: ty) => {
111+ impl IntoValue <$c> for $b {
112+ fn into_value( self ) -> $c {
113+ self as $c
114+ }
115+ }
116+ } ;
117+ }
118+
119+ macro_rules! impl_time_delta_into_value {
120+ ( $b: ty, $c: ty) => {
121+ impl IntoValue <$c> for $b {
122+ fn into_value( self ) -> $c {
123+ self . as_seconds_f64( )
124+ }
125+ }
126+ } ;
127+ }
128+
77129// Implement InterpolationIndex for all signed integer types.
78130impl_interpolation_index ! ( i8 , i8 , i8 ) ;
131+ impl_num_delta_into_value ! ( i8 , i8 ) ;
79132impl_interpolation_index ! ( i16 , i16 , i16 ) ;
133+ impl_num_delta_into_value ! ( i16 , i16 ) ;
80134impl_interpolation_index ! ( i32 , i32 , i32 ) ;
135+ impl_num_delta_into_value ! ( i32 , i32 ) ;
81136impl_interpolation_index ! ( i64 , i64 , i64 ) ;
137+ impl_num_delta_into_value ! ( i64 , i64 ) ;
82138impl_interpolation_index ! ( i128 , i128 , i128 ) ;
139+ impl_num_delta_into_value ! ( i128 , i128 ) ;
83140impl_interpolation_index ! ( isize , isize , isize ) ;
84-
85- // Implement InterpolationIndex for all unsigned integer types.
86- impl_interpolation_index ! ( u8 , u8 , u8 ) ;
87- impl_interpolation_index ! ( u16 , u16 , u16 ) ;
88- impl_interpolation_index ! ( u32 , u32 , u32 ) ;
89- impl_interpolation_index ! ( u64 , u64 , u64 ) ;
90- impl_interpolation_index ! ( u128 , u128 , u128 ) ;
91- impl_interpolation_index ! ( usize , usize , usize ) ;
141+ impl_num_delta_into_value ! ( isize , isize ) ;
92142
93143// Implement InterpolationIndex for all floating point types.
94144impl_interpolation_index ! ( f32 , f32 , f32 ) ;
145+ impl_num_delta_into_value ! ( f32 , f32 ) ;
95146impl_interpolation_index ! ( f64 , f64 , f64 ) ;
147+ impl_num_delta_into_value ! ( f64 , f64 ) ;
96148
97149// Implement InterpolationIndex for date/time types.
98150impl_interpolation_index ! ( time:: Date , time:: Duration , f64 ) ;
99151impl_interpolation_index ! ( time:: Time , time:: Duration , f64 ) ;
100152impl_interpolation_index ! ( time:: OffsetDateTime , time:: Duration , f64 ) ;
101153impl_interpolation_index ! ( time:: PrimitiveDateTime , time:: Duration , f64 ) ;
154+ impl_time_delta_into_value ! ( time:: Duration , f64 ) ;
102155
103156// Implement InterpolationIndex for Decimal type.
104157impl_interpolation_index ! (
105158 rust_decimal:: Decimal ,
106159 rust_decimal:: Decimal ,
107160 rust_decimal:: Decimal
108161) ;
162+ impl_num_delta_into_value ! ( rust_decimal:: Decimal , rust_decimal:: Decimal ) ;
0 commit comments