diff options
| author | hachem <im@hachem.wtf> | 2024-09-15 17:58:22 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2024-09-15 17:58:22 +0200 |
| commit | 085e3d6f1d268676b6bc796b0b1fa60a8f58ee1d (patch) | |
| tree | 6cae6e39d50e5c078b7f68c3603314352e1d1625 /libmu/src/maths/complex.rs | |
| parent | aca7dbace9f02a85004ad9b61a410977cd6c64f1 (diff) | |
Fix and formatting
Diffstat (limited to 'libmu/src/maths/complex.rs')
| -rw-r--r-- | libmu/src/maths/complex.rs | 57 |
1 files changed, 37 insertions, 20 deletions
diff --git a/libmu/src/maths/complex.rs b/libmu/src/maths/complex.rs index dcd5902..79212b6 100644 --- a/libmu/src/maths/complex.rs +++ b/libmu/src/maths/complex.rs @@ -1,12 +1,29 @@ -use crate::numeric_types::FloatTypes; -use std::ops; +use crate::numeric_types::Float; +use core::{fmt, ops}; -pub struct Complex<T: FloatTypes> { +#[derive(Copy, Clone)] +pub struct Complex<T: Float> { pub real: T, pub imaginary: T, } -impl<T: FloatTypes> Complex<T> { +impl<T: Float + fmt::Debug> fmt::Debug for Complex<T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "Complex {{ real: {:?}, imaginary: {:?} }}", + self.real, self.imaginary + ) + } +} + +impl<T: Float + fmt::Display> fmt::Display for Complex<T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{} + {}i", self.real, self.imaginary) + } +} + +impl<T: Float> Complex<T> { pub fn new(real: T, imaginary: T) -> Complex<T> { Complex { real, imaginary } } @@ -32,7 +49,7 @@ impl<T: FloatTypes> Complex<T> { } } -impl<T: FloatTypes> ops::Add for Complex<T> { +impl<T: Float> ops::Add for Complex<T> { type Output = Complex<T>; fn add(self, other: Complex<T>) -> Complex<T> { @@ -43,7 +60,7 @@ impl<T: FloatTypes> ops::Add for Complex<T> { } } -impl<T: FloatTypes> ops::Sub for Complex<T> { +impl<T: Float> ops::Sub for Complex<T> { type Output = Complex<T>; fn sub(self, other: Complex<T>) -> Complex<T> { @@ -54,7 +71,7 @@ impl<T: FloatTypes> ops::Sub for Complex<T> { } } -impl<T: FloatTypes> ops::Mul for Complex<T> { +impl<T: Float> ops::Mul for Complex<T> { type Output = Complex<T>; fn mul(self, other: Complex<T>) -> Complex<T> { @@ -65,7 +82,7 @@ impl<T: FloatTypes> ops::Mul for Complex<T> { } } -impl<T: FloatTypes> ops::Div for Complex<T> { +impl<T: Float> ops::Div for Complex<T> { type Output = Complex<T>; fn div(self, other: Complex<T>) -> Complex<T> { @@ -77,7 +94,7 @@ impl<T: FloatTypes> ops::Div for Complex<T> { } } -impl<T: FloatTypes> ops::Add<T> for Complex<T> { +impl<T: Float> ops::Add<T> for Complex<T> { type Output = Complex<T>; fn add(self, other: T) -> Complex<T> { @@ -88,7 +105,7 @@ impl<T: FloatTypes> ops::Add<T> for Complex<T> { } } -impl<T: FloatTypes> ops::Sub<T> for Complex<T> { +impl<T: Float> ops::Sub<T> for Complex<T> { type Output = Complex<T>; fn sub(self, other: T) -> Complex<T> { @@ -99,7 +116,7 @@ impl<T: FloatTypes> ops::Sub<T> for Complex<T> { } } -impl<T: FloatTypes> ops::Mul<T> for Complex<T> { +impl<T: Float> ops::Mul<T> for Complex<T> { type Output = Complex<T>; fn mul(self, other: T) -> Complex<T> { @@ -110,7 +127,7 @@ impl<T: FloatTypes> ops::Mul<T> for Complex<T> { } } -impl<T: FloatTypes> ops::Div<T> for Complex<T> { +impl<T: Float> ops::Div<T> for Complex<T> { type Output = Complex<T>; fn div(self, other: T) -> Complex<T> { @@ -121,21 +138,21 @@ impl<T: FloatTypes> ops::Div<T> for Complex<T> { } } -impl<T: FloatTypes> ops::AddAssign for Complex<T> { +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: FloatTypes> ops::SubAssign for Complex<T> { +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: FloatTypes> ops::MulAssign for Complex<T> { +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; @@ -144,7 +161,7 @@ impl<T: FloatTypes> ops::MulAssign for Complex<T> { } } -impl<T: FloatTypes> ops::DivAssign for Complex<T> { +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; @@ -154,26 +171,26 @@ impl<T: FloatTypes> ops::DivAssign for Complex<T> { } } -impl<T: FloatTypes> ops::AddAssign<T> for Complex<T> { +impl<T: Float> ops::AddAssign<T> for Complex<T> { fn add_assign(&mut self, other: T) { self.real += other; } } -impl<T: FloatTypes> ops::SubAssign<T> for Complex<T> { +impl<T: Float> ops::SubAssign<T> for Complex<T> { fn sub_assign(&mut self, other: T) { self.real -= other; } } -impl<T: FloatTypes> ops::MulAssign<T> for Complex<T> { +impl<T: Float> ops::MulAssign<T> for Complex<T> { fn mul_assign(&mut self, other: T) { self.real *= other; self.imaginary *= other; } } -impl<T: FloatTypes> ops::DivAssign<T> for Complex<T> { +impl<T: Float> ops::DivAssign<T> for Complex<T> { fn div_assign(&mut self, other: T) { self.real /= other; self.imaginary /= other; |
