7
7
// - LICENSE-MIT.md
8
8
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9
9
10
- use std:: ops:: { Div , Mul , Sub , AddAssign } ;
10
+ use std:: ops:: { Div , Mul , MulAssign , Sub , Add , AddAssign , Neg } ;
11
11
use RustQuant_error :: RustQuantError ;
12
12
13
13
pub mod linear_interpolator;
@@ -19,19 +19,42 @@ pub use exponential_interpolator::*;
19
19
pub mod b_splines;
20
20
pub use b_splines:: * ;
21
21
22
+ pub mod cubic_spline;
23
+ pub use cubic_spline:: * ;
24
+
25
+
22
26
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23
27
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24
28
25
29
/// 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
+ }
27
46
28
47
/// Trait describing requirements to be an index of interpolation.
29
48
pub trait InterpolationIndex :
30
49
Sub < Self , Output = Self :: Delta > + PartialOrd + Copy + Clone + Sized + std:: fmt:: Display
31
50
{
32
51
/// Type of the difference of `Self` - `Self`
33
52
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 ;
35
58
36
59
/// Type of `Delta` / `Delta`
37
60
type DeltaDiv : InterpolationValue ;
63
86
fn add_point ( & mut self , point : ( IndexType , ValueType ) ) ;
64
87
}
65
88
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 { }
67
99
68
100
macro_rules! impl_interpolation_index {
69
101
( $a: ty, $b: ty, $c: ty) => {
@@ -74,35 +106,57 @@ macro_rules! impl_interpolation_index {
74
106
} ;
75
107
}
76
108
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
+
77
129
// Implement InterpolationIndex for all signed integer types.
78
130
impl_interpolation_index ! ( i8 , i8 , i8 ) ;
131
+ impl_num_delta_into_value ! ( i8 , i8 ) ;
79
132
impl_interpolation_index ! ( i16 , i16 , i16 ) ;
133
+ impl_num_delta_into_value ! ( i16 , i16 ) ;
80
134
impl_interpolation_index ! ( i32 , i32 , i32 ) ;
135
+ impl_num_delta_into_value ! ( i32 , i32 ) ;
81
136
impl_interpolation_index ! ( i64 , i64 , i64 ) ;
137
+ impl_num_delta_into_value ! ( i64 , i64 ) ;
82
138
impl_interpolation_index ! ( i128 , i128 , i128 ) ;
139
+ impl_num_delta_into_value ! ( i128 , i128 ) ;
83
140
impl_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 ) ;
92
142
93
143
// Implement InterpolationIndex for all floating point types.
94
144
impl_interpolation_index ! ( f32 , f32 , f32 ) ;
145
+ impl_num_delta_into_value ! ( f32 , f32 ) ;
95
146
impl_interpolation_index ! ( f64 , f64 , f64 ) ;
147
+ impl_num_delta_into_value ! ( f64 , f64 ) ;
96
148
97
149
// Implement InterpolationIndex for date/time types.
98
150
impl_interpolation_index ! ( time:: Date , time:: Duration , f64 ) ;
99
151
impl_interpolation_index ! ( time:: Time , time:: Duration , f64 ) ;
100
152
impl_interpolation_index ! ( time:: OffsetDateTime , time:: Duration , f64 ) ;
101
153
impl_interpolation_index ! ( time:: PrimitiveDateTime , time:: Duration , f64 ) ;
154
+ impl_time_delta_into_value ! ( time:: Duration , f64 ) ;
102
155
103
156
// Implement InterpolationIndex for Decimal type.
104
157
impl_interpolation_index ! (
105
158
rust_decimal:: Decimal ,
106
159
rust_decimal:: Decimal ,
107
160
rust_decimal:: Decimal
108
161
) ;
162
+ impl_num_delta_into_value ! ( rust_decimal:: Decimal , rust_decimal:: Decimal ) ;
0 commit comments