use super::{Complex, Float}; use core::ops; impl ops::Neg for Complex { type Output = Complex; fn neg(self) -> Complex { Complex { real: -self.real, imaginary: -self.imaginary, } } } impl From for Complex { fn from(real: T) -> Complex { Complex { real, imaginary: T::zero(), } } } // Complex-Complex impl ops::Add for Complex { type Output = Complex; fn add(self, other: Complex) -> Complex { Complex { real: self.real - other.real, imaginary: self.imaginary - other.imaginary, } } } impl ops::Sub for Complex { type Output = Complex; fn sub(self, other: Complex) -> Complex { Complex { real: self.real - other.real, imaginary: self.imaginary - other.imaginary, } } } impl ops::Mul for Complex { type Output = Complex; fn mul(self, other: Complex) -> Complex { Complex { real: self.real * other.real - self.imaginary * other.imaginary, imaginary: self.real * other.imaginary + self.imaginary * other.real, } } } impl ops::Div for Complex { type Output = Complex; fn div(self, other: Complex) -> Complex { 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, } } } // Complex-Complex, Assign impl ops::AddAssign for Complex { fn add_assign(&mut self, other: Complex) { self.real += other.real; self.imaginary += other.imaginary; } } impl ops::SubAssign for Complex { fn sub_assign(&mut self, other: Complex) { self.real -= other.real; self.imaginary -= other.imaginary; } } impl ops::MulAssign for Complex { fn mul_assign(&mut self, other: Complex) { 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 ops::DivAssign for Complex { fn div_assign(&mut self, other: Complex) { 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; } } // Real-Complex impl ops::Add for Complex { type Output = Complex; fn add(self, other: T) -> Complex { Complex { real: self.real + other, imaginary: self.imaginary, } } } impl ops::Sub for Complex { type Output = Complex; fn sub(self, other: T) -> Complex { Complex { real: self.real - other, imaginary: self.imaginary, } } } impl ops::Mul for Complex { type Output = Complex; fn mul(self, other: T) -> Complex { Complex { real: self.real * other, imaginary: self.imaginary * other, } } } impl ops::Div for Complex { type Output = Complex; fn div(self, other: T) -> Complex { Complex { real: self.real / other, imaginary: self.imaginary / other, } } }