use num_traits::{Num, NumCast, Signed}; use crate::signals::utils::{apply1, apply2, apply2_const}; use crate::signals::{ConstantSignal, Signal}; use super::traits::LinearInterpolatable; impl core::ops::Neg for &Signal where T: Signed + Copy, { type Output = Signal; /// Negate the signal at each time point fn neg(self) -> Self::Output { apply1(self, |v| -v) } } impl core::ops::Add for &Signal where T: core::ops::Add + Num + NumCast + Copy + LinearInterpolatable, { type Output = Signal; /// Add the given signal with another fn add(self, rhs: Self) -> Self::Output { apply2(self, rhs, |lhs, rhs| lhs + rhs) } } impl core::ops::Add<&ConstantSignal> for &Signal where T: core::ops::Add + Num + NumCast + Copy + LinearInterpolatable, { type Output = Signal; /// Add the given signal with another fn add(self, rhs: &ConstantSignal) -> Self::Output { apply2_const(self, rhs, |lhs, rhs| lhs + rhs) } } impl core::ops::Mul for &Signal where T: core::ops::Mul + Num + NumCast + Copy + LinearInterpolatable, { type Output = Signal; /// Multiply the given signal with another fn mul(self, rhs: Self) -> Self::Output { apply2(self, rhs, |lhs, rhs| lhs * rhs) } } impl core::ops::Mul<&ConstantSignal> for &Signal where T: core::ops::Mul + Num + NumCast + Copy + LinearInterpolatable, { type Output = Signal; /// Multiply the given signal with another fn mul(self, rhs: &ConstantSignal) -> Self::Output { apply2_const(self, rhs, |lhs, rhs| lhs * rhs) } } impl core::ops::Sub for &Signal where T: core::ops::Sub + Num + NumCast + Copy + LinearInterpolatable, { type Output = Signal; /// Subtract the given signal with another fn sub(self, rhs: Self) -> Self::Output { apply2(self, rhs, |lhs, rhs| lhs - rhs) } } impl core::ops::Sub<&ConstantSignal> for &Signal where T: core::ops::Sub + Num + NumCast + Copy + LinearInterpolatable, { type Output = Signal; /// Subtiply the given signal with another fn sub(self, rhs: &ConstantSignal) -> Self::Output { apply2_const(self, rhs, |lhs, rhs| lhs - rhs) } } impl core::ops::Div for &Signal where T: core::ops::Div + Num + NumCast + Copy + LinearInterpolatable, { type Output = Signal; /// Divide the given signal with another fn div(self, rhs: Self) -> Self::Output { apply2(self, rhs, |lhs, rhs| lhs / rhs) } } impl core::ops::Div<&ConstantSignal> for &Signal where T: core::ops::Div + Num + NumCast + Copy + LinearInterpolatable, { type Output = Signal; /// Divide the given signal with another fn div(self, rhs: &ConstantSignal) -> Self::Output { apply2_const(self, rhs, |lhs, rhs| lhs / rhs) } } impl num_traits::Pow for &Signal where T: num_traits::Pow + Num + NumCast + Copy + LinearInterpolatable, { type Output = Signal; /// Returns the values in `self` to the power of the values in `other` fn pow(self, other: Self) -> Self::Output { apply2(self, other, |lhs, rhs| lhs.pow(rhs)) } } impl core::ops::Neg for &ConstantSignal where T: Signed + Copy, { type Output = ConstantSignal; /// Negate the signal at each time point fn neg(self) -> Self::Output { ConstantSignal::new(self.value.neg()) } } impl core::ops::Add for &ConstantSignal where T: core::ops::Add + Num + Copy, { type Output = ConstantSignal; /// Add the given signal with another fn add(self, rhs: Self) -> Self::Output { ConstantSignal::::new(self.value + rhs.value) } } impl core::ops::Add<&Signal> for &ConstantSignal where T: core::ops::Add + Num + NumCast + Copy + LinearInterpolatable, { type Output = Signal; /// Add the given signal with another fn add(self, rhs: &Signal) -> Self::Output { rhs + self } } impl core::ops::Mul for &ConstantSignal where T: core::ops::Mul + Num + Copy, { type Output = ConstantSignal; /// Multiply the given signal with another fn mul(self, rhs: Self) -> Self::Output { ConstantSignal::::new(self.value * rhs.value) } } impl core::ops::Mul<&Signal> for &ConstantSignal where T: core::ops::Mul + Num + NumCast + Copy + LinearInterpolatable, { type Output = Signal; /// Multiply the given signal with another fn mul(self, rhs: &Signal) -> Self::Output { rhs * self } } impl core::ops::Sub for &ConstantSignal where T: core::ops::Sub + Num + Copy, { type Output = ConstantSignal; /// Subtract the given signal with another fn sub(self, rhs: Self) -> Self::Output { ConstantSignal::::new(self.value - rhs.value) } } impl core::ops::Sub<&Signal> for &ConstantSignal where T: core::ops::Sub + Num + NumCast + Copy + LinearInterpolatable, { type Output = Signal; /// Subtract the given signal with another fn sub(self, rhs: &Signal) -> Self::Output { apply2_const(rhs, self, |rhs, lhs| lhs - rhs) } } impl core::ops::Div for &ConstantSignal where T: core::ops::Div + Num + Copy, { type Output = ConstantSignal; /// Divide the given signal with another fn div(self, rhs: Self) -> Self::Output { ConstantSignal::::new(self.value / rhs.value) } } impl core::ops::Div<&Signal> for &ConstantSignal where T: core::ops::Div + Num + NumCast + Copy + LinearInterpolatable, { type Output = Signal; /// Divide the given signal with another fn div(self, rhs: &Signal) -> Self::Output { apply2_const(rhs, self, |rhs, lhs| lhs / rhs) } }