diff options
Diffstat (limited to 'libpsi/src/maths/complex.rs')
| -rw-r--r-- | libpsi/src/maths/complex.rs | 183 |
1 files changed, 23 insertions, 160 deletions
diff --git a/libpsi/src/maths/complex.rs b/libpsi/src/maths/complex.rs index 28a3121..bb19510 100644 --- a/libpsi/src/maths/complex.rs +++ b/libpsi/src/maths/complex.rs @@ -1,5 +1,7 @@ use crate::Float; -use core::{fmt, ops}; +use core::fmt; + +use super::Numeric; #[macro_export] macro_rules! complex { @@ -30,6 +32,26 @@ impl<T: Float + fmt::Display> fmt::Display for Complex<T> { } } +impl Numeric for Complex<f32> { + fn zero() -> Self { + Complex::new(0.0, 0.0) + } + + fn one() -> Self { + Complex::new(1.0, 0.0) + } +} + +impl Numeric for Complex<f64> { + fn zero() -> Self { + Complex::new(0.0, 0.0) + } + + fn one() -> Self { + Complex::new(1.0, 0.0) + } +} + impl<T: Float> Complex<T> { pub fn new(real: T, imaginary: T) -> Complex<T> { Complex { real, imaginary } @@ -58,162 +80,3 @@ impl<T: Float> Complex<T> { T::sqrt(self.norm()) } } - -impl<T: Float> ops::Neg for Complex<T> { - type Output = Complex<T>; - - fn neg(self) -> Complex<T> { - Complex { - real: -self.real, - imaginary: -self.imaginary, - } - } -} - -impl<T: Float> ops::Add for Complex<T> { - type Output = Complex<T>; - - fn add(self, other: Complex<T>) -> Complex<T> { - Complex { - real: self.real + other.real, - imaginary: self.imaginary + other.imaginary, - } - } -} - -impl<T: Float> ops::Sub for Complex<T> { - type Output = Complex<T>; - - fn sub(self, other: Complex<T>) -> Complex<T> { - Complex { - real: self.real - other.real, - imaginary: self.imaginary - other.imaginary, - } - } -} - -impl<T: Float> ops::Mul for Complex<T> { - type Output = Complex<T>; - - fn mul(self, other: Complex<T>) -> Complex<T> { - Complex { - real: self.real * other.real - self.imaginary * other.imaginary, - imaginary: self.real * other.imaginary + self.imaginary * other.real, - } - } -} - -impl<T: Float> ops::Div for Complex<T> { - type Output = Complex<T>; - - fn div(self, other: Complex<T>) -> Complex<T> { - let denom = other.real * other.real + other.imaginary * other.imaginary; - Complex { - real: (self.real * other.real + self.imaginary * other.imaginary) / denom, - imaginary: (self.imaginary * other.real - self.real * other.imaginary) / denom, - } - } -} - -impl<T: Float> ops::Add<T> for Complex<T> { - type Output = Complex<T>; - - fn add(self, other: T) -> Complex<T> { - Complex { - real: self.real + other, - imaginary: self.imaginary, - } - } -} - -impl<T: Float> ops::Sub<T> for Complex<T> { - type Output = Complex<T>; - - fn sub(self, other: T) -> Complex<T> { - Complex { - real: self.real - other, - imaginary: self.imaginary, - } - } -} - -impl<T: Float> ops::Mul<T> for Complex<T> { - type Output = Complex<T>; - - fn mul(self, other: T) -> Complex<T> { - Complex { - real: self.real * other, - imaginary: self.imaginary * other, - } - } -} - -impl<T: Float> ops::Div<T> for Complex<T> { - type Output = Complex<T>; - - fn div(self, other: T) -> Complex<T> { - Complex { - real: self.real / other, - imaginary: self.imaginary / other, - } - } -} - -impl<T: Float> ops::AddAssign for Complex<T> { - fn add_assign(&mut self, other: Complex<T>) { - self.real += other.real; - self.imaginary += other.imaginary; - } -} - -impl<T: Float> ops::SubAssign for Complex<T> { - fn sub_assign(&mut self, other: Complex<T>) { - self.real -= other.real; - self.imaginary -= other.imaginary; - } -} - -impl<T: Float> ops::MulAssign for Complex<T> { - fn mul_assign(&mut self, other: Complex<T>) { - let new_real = self.real * other.real - self.imaginary * other.imaginary; - let new_imaginary = self.real * other.imaginary + self.imaginary * other.real; - self.real = new_real; - self.imaginary = new_imaginary; - } -} - -impl<T: Float> ops::DivAssign for Complex<T> { - fn div_assign(&mut self, other: Complex<T>) { - let denom = other.real * other.real + other.imaginary * other.imaginary; - let new_real = (self.real * other.real + self.imaginary * other.imaginary) / denom; - let new_imaginary = (self.imaginary * other.real - self.real * other.imaginary) / denom; - self.real = new_real; - self.imaginary = new_imaginary; - } -} - -impl<T: Float> ops::AddAssign<T> for Complex<T> { - fn add_assign(&mut self, other: T) { - self.real += other; - } -} - -impl<T: Float> ops::SubAssign<T> for Complex<T> { - fn sub_assign(&mut self, other: T) { - self.real -= other; - } -} - -impl<T: Float> ops::MulAssign<T> for Complex<T> { - fn mul_assign(&mut self, other: T) { - self.real *= other; - self.imaginary *= other; - } -} - -impl<T: Float> ops::DivAssign<T> for Complex<T> { - fn div_assign(&mut self, other: T) { - self.real /= other; - self.imaginary /= other; - } -} |
