diff --git a/ta_lib/core/src/ops.rs b/ta_lib/core/src/ops.rs index 4f20b09f..dcef6839 100644 --- a/ta_lib/core/src/ops.rs +++ b/ta_lib/core/src/ops.rs @@ -19,6 +19,10 @@ impl Series { self.fmap(|val| val.map(|v| op(v, scalar))) } + pub fn neg(&self) -> Series { + self.fmap(|val| val.map(|v| v.neg())) + } + pub fn add_series(&self, rhs: &Series) -> Series { self.binary_op_series(rhs, |a, b| a + b) } @@ -77,9 +81,13 @@ impl Series { pub fn sub_scalar(&self, scalar: f64) -> Series { self.unary_op_scalar(scalar, |v, s| v - s) } +} - pub fn neg(&self) -> Series { - self.fmap(|val| val.map(|v| v.neg())) +impl Neg for &Series { + type Output = Series; + + fn neg(self) -> Series { + self.neg() } } @@ -99,124 +107,73 @@ impl Series { } } -impl Add> for &Series { - type Output = Series; - - fn add(self, rhs: Series) -> Series { - self.add_series(&rhs) - } -} - -impl Add<&Series> for Series { - type Output = Series; - - fn add(self, rhs: &Series) -> Series { - self.add_series(rhs) - } -} - -impl Add<&Series> for &Series { - type Output = Series; - - fn add(self, rhs: &Series) -> Series { - self.add_series(rhs) - } -} - -impl Mul> for &Series { - type Output = Series; - - fn mul(self, rhs: Series) -> Series { - self.mul_series(&rhs) - } -} - -impl Mul<&Series> for Series { - type Output = Series; - - fn mul(self, rhs: &Series) -> Series { - self.mul_series(rhs) - } -} - -impl Mul<&Series> for &Series { - type Output = Series; - - fn mul(self, rhs: &Series) -> Series { - self.mul_series(rhs) - } -} - -impl Div<&Series> for &Series { - type Output = Series; - - fn div(self, rhs: &Series) -> Series { - self.div_series(rhs) - } -} - -impl Div<&Series> for Series { - type Output = Series; - - fn div(self, rhs: &Series) -> Series { - self.div_series(rhs) - } -} - -impl Div> for &Series { - type Output = Series; - - fn div(self, rhs: Series) -> Series { - self.div_series(&rhs) - } -} - -impl Sub<&Series> for Series { - type Output = Series; - - fn sub(self, rhs: &Series) -> Series { - self.sub_series(rhs) - } -} - -impl Sub> for &Series { - type Output = Series; - - fn sub(self, rhs: Series) -> Series { - self.sub_series(&rhs) - } -} - -impl Sub<&Series> for &Series { - type Output = Series; +macro_rules! impl_series_ops { + ($trait_name:ident, $trait_method:ident, $method:ident) => { + impl $trait_name> for &Series { + type Output = Series; + fn $trait_method(self, rhs: Series) -> Series { + self.$method(&rhs) + } + } - fn sub(self, rhs: &Series) -> Series { - self.sub_series(rhs) - } -} + impl $trait_name<&Series> for Series { + type Output = Series; + fn $trait_method(self, rhs: &Series) -> Series { + self.$method(rhs) + } + } -impl Add for &Series { - type Output = Series; + impl $trait_name<&Series> for &Series { + type Output = Series; + fn $trait_method(self, rhs: &Series) -> Series { + self.$method(rhs) + } + } - fn add(self, scalar: f64) -> Series { - self.add_scalar(scalar) - } + impl $trait_name> for Series { + type Output = Series; + fn $trait_method(self, rhs: Series) -> Series { + self.$method(&rhs) + } + } + }; } -impl Mul for &Series { - type Output = Series; +impl_series_ops!(Add, add, add_series); +impl_series_ops!(Mul, mul, mul_series); +impl_series_ops!(Div, div, div_series); +impl_series_ops!(Sub, sub, sub_series); + +macro_rules! impl_scalar_ops { + ($trait_name:ident, $trait_method:ident, $method:ident) => { + impl $trait_name<&Series> for f64 { + type Output = Series; + fn $trait_method(self, rhs: &Series) -> Series { + rhs.$method(self) + } + } - fn mul(self, scalar: f64) -> Series { - self.mul_scalar(scalar) - } -} + impl $trait_name> for f64 { + type Output = Series; + fn $trait_method(self, rhs: Series) -> Series { + rhs.$method(self) + } + } -impl Mul for Series { - type Output = Series; + impl $trait_name for &Series { + type Output = Series; + fn $trait_method(self, scalar: f64) -> Series { + self.$method(scalar) + } + } - fn mul(self, scalar: f64) -> Series { - self.mul_scalar(scalar) - } + impl $trait_name for Series { + type Output = Series; + fn $trait_method(self, scalar: f64) -> Series { + self.$method(scalar) + } + } + }; } impl Div for &Series { @@ -235,61 +192,37 @@ impl Div for Series { } } -impl Sub for &Series { - type Output = Series; - - fn sub(self, scalar: f64) -> Series { - self.sub_scalar(scalar) - } -} - -impl Add<&Series> for f64 { - type Output = Series; - - fn add(self, rhs: &Series) -> Series { - rhs.add_scalar(self) - } -} - -impl Add> for f64 { - type Output = Series; - - fn add(self, rhs: Series) -> Series { - rhs.add_scalar(self) - } -} - -impl Mul<&Series> for f64 { +impl Div<&Series> for f64 { type Output = Series; - fn mul(self, rhs: &Series) -> Series { - rhs.mul_scalar(self) + fn div(self, rhs: &Series) -> Series { + let scalars = vec![self; rhs.len()]; + Series::from(&scalars).div_series(&rhs) } } -impl Mul> for f64 { +impl Div> for f64 { type Output = Series; - fn mul(self, rhs: Series) -> Series { - rhs.mul_scalar(self) + fn div(self, rhs: Series) -> Series { + let scalars = vec![self; rhs.len()]; + Series::from(&scalars).div_series(&rhs) } } -impl Div<&Series> for f64 { +impl Sub for &Series { type Output = Series; - fn div(self, rhs: &Series) -> Series { - let scalars = vec![self; rhs.len()]; - Series::from(&scalars).div_series(&rhs) + fn sub(self, scalar: f64) -> Series { + self.sub_scalar(scalar) } } -impl Div> for f64 { +impl Sub for Series { type Output = Series; - fn div(self, rhs: Series) -> Series { - let scalars = vec![self; rhs.len()]; - Series::from(&scalars).div_series(&rhs) + fn sub(self, scalar: f64) -> Series { + self.sub_scalar(scalar) } } @@ -309,6 +242,9 @@ impl Sub> for f64 { } } +impl_scalar_ops!(Add, add, add_scalar); +impl_scalar_ops!(Mul, mul, mul_scalar); + impl Mul<&Series> for &Series { type Output = Series; @@ -324,11 +260,3 @@ impl Mul<&Series> for Series { self.mul_series(rhs) } } - -impl Neg for &Series { - type Output = Series; - - fn neg(self) -> Series { - self.neg() - } -}