77// - LICENSE-MIT.md
88// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99
10- use std:: ops:: { Div , Mul , MulAssign , Sub , Add , AddAssign , Neg } ;
10+ use std:: { i8, ops:: { Add , AddAssign , Div , Mul , MulAssign , Neg , Sub } } ;
11+ use rust_decimal:: prelude:: * ;
1112use RustQuant_error :: RustQuantError ;
1213
1314pub mod linear_interpolator;
@@ -32,6 +33,7 @@ pub trait InterpolationValue: num::Num
3233 + Neg < Output = Self >
3334 + AddAssign
3435 + MulAssign
36+ + SquareRoot < Self >
3537 + Copy
3638 + Clone
3739 + Sized
@@ -46,6 +48,12 @@ pub trait IntoValue<ValueType> {
4648 fn into_value ( self ) -> ValueType ;
4749}
4850
51+ /// Trait to compute the square root of a value.
52+ pub trait SquareRoot < ValueType > {
53+ /// Calculate square root of a value.
54+ fn square_root ( self ) -> ValueType ;
55+ }
56+
4957/// Trait describing requirements to be an index of interpolation.
5058pub trait InterpolationIndex :
5159 Sub < Self , Output = Self :: Delta > + PartialOrd + Copy + Clone + Sized + std:: fmt:: Display + Send + Sync
@@ -95,6 +103,7 @@ impl<T> InterpolationValue for T where T: num::Num
95103 + Neg < Output = Self >
96104 + AddAssign
97105 + MulAssign
106+ + SquareRoot < Self >
98107 + Copy
99108 + Clone
100109 + Sized
@@ -132,25 +141,63 @@ macro_rules! impl_time_delta_into_value {
132141 } ;
133142}
134143
144+ macro_rules! impl_int_square_root {
145+ ( $c: ty) => {
146+ impl SquareRoot <$c> for $c {
147+ fn square_root( self ) -> $c {
148+ self . isqrt( )
149+ }
150+ }
151+ } ;
152+ }
153+
154+ macro_rules! impl_float_square_root {
155+ ( $c: ty) => {
156+ impl SquareRoot <$c> for $c {
157+ fn square_root( self ) -> $c {
158+ self . sqrt( )
159+ }
160+ }
161+ } ;
162+ }
163+
164+ macro_rules! impl_decimal_square_root {
165+ ( $c: ty) => {
166+ impl SquareRoot <$c> for $c {
167+ fn square_root( self ) -> $c {
168+ self . sqrt( ) . unwrap( )
169+ }
170+ }
171+ } ;
172+ }
173+
135174// Implement InterpolationIndex for all signed integer types.
136175impl_interpolation_index ! ( i8 , i8 , i8 ) ;
137176impl_num_delta_into_value ! ( i8 , i8 ) ;
177+ impl_int_square_root ! ( i8 ) ;
138178impl_interpolation_index ! ( i16 , i16 , i16 ) ;
139179impl_num_delta_into_value ! ( i16 , i16 ) ;
180+ impl_int_square_root ! ( i16 ) ;
140181impl_interpolation_index ! ( i32 , i32 , i32 ) ;
141182impl_num_delta_into_value ! ( i32 , i32 ) ;
183+ impl_int_square_root ! ( i32 ) ;
142184impl_interpolation_index ! ( i64 , i64 , i64 ) ;
143185impl_num_delta_into_value ! ( i64 , i64 ) ;
186+ impl_int_square_root ! ( i64 ) ;
144187impl_interpolation_index ! ( i128 , i128 , i128 ) ;
145188impl_num_delta_into_value ! ( i128 , i128 ) ;
189+ impl_int_square_root ! ( i128 ) ;
146190impl_interpolation_index ! ( isize , isize , isize ) ;
147191impl_num_delta_into_value ! ( isize , isize ) ;
192+ impl_int_square_root ! ( isize ) ;
148193
149194// Implement InterpolationIndex for all floating point types.
150195impl_interpolation_index ! ( f32 , f32 , f32 ) ;
151196impl_num_delta_into_value ! ( f32 , f32 ) ;
152197impl_interpolation_index ! ( f64 , f64 , f64 ) ;
153198impl_num_delta_into_value ! ( f64 , f64 ) ;
199+ impl_float_square_root ! ( f32 ) ;
200+ impl_float_square_root ! ( f64 ) ;
154201
155202// Implement InterpolationIndex for date/time types.
156203impl_interpolation_index ! ( time:: Date , time:: Duration , f64 ) ;
@@ -166,3 +213,4 @@ impl_interpolation_index!(
166213 rust_decimal:: Decimal
167214) ;
168215impl_num_delta_into_value ! ( rust_decimal:: Decimal , rust_decimal:: Decimal ) ;
216+ impl_decimal_square_root ! ( rust_decimal:: Decimal ) ;
0 commit comments