From 085e3d6f1d268676b6bc796b0b1fa60a8f58ee1d Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 15 Sep 2024 17:58:22 +0200 Subject: Fix and formatting --- libmu/src/maths/complex.rs | 57 ++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 20 deletions(-) (limited to 'libmu/src/maths/complex.rs') 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 { +#[derive(Copy, Clone)] +pub struct Complex { pub real: T, pub imaginary: T, } -impl Complex { +impl fmt::Debug for Complex { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "Complex {{ real: {:?}, imaginary: {:?} }}", + self.real, self.imaginary + ) + } +} + +impl fmt::Display for Complex { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{} + {}i", self.real, self.imaginary) + } +} + +impl Complex { pub fn new(real: T, imaginary: T) -> Complex { Complex { real, imaginary } } @@ -32,7 +49,7 @@ impl Complex { } } -impl ops::Add for Complex { +impl ops::Add for Complex { type Output = Complex; fn add(self, other: Complex) -> Complex { @@ -43,7 +60,7 @@ impl ops::Add for Complex { } } -impl ops::Sub for Complex { +impl ops::Sub for Complex { type Output = Complex; fn sub(self, other: Complex) -> Complex { @@ -54,7 +71,7 @@ impl ops::Sub for Complex { } } -impl ops::Mul for Complex { +impl ops::Mul for Complex { type Output = Complex; fn mul(self, other: Complex) -> Complex { @@ -65,7 +82,7 @@ impl ops::Mul for Complex { } } -impl ops::Div for Complex { +impl ops::Div for Complex { type Output = Complex; fn div(self, other: Complex) -> Complex { @@ -77,7 +94,7 @@ impl ops::Div for Complex { } } -impl ops::Add for Complex { +impl ops::Add for Complex { type Output = Complex; fn add(self, other: T) -> Complex { @@ -88,7 +105,7 @@ impl ops::Add for Complex { } } -impl ops::Sub for Complex { +impl ops::Sub for Complex { type Output = Complex; fn sub(self, other: T) -> Complex { @@ -99,7 +116,7 @@ impl ops::Sub for Complex { } } -impl ops::Mul for Complex { +impl ops::Mul for Complex { type Output = Complex; fn mul(self, other: T) -> Complex { @@ -110,7 +127,7 @@ impl ops::Mul for Complex { } } -impl ops::Div for Complex { +impl ops::Div for Complex { type Output = Complex; fn div(self, other: T) -> Complex { @@ -121,21 +138,21 @@ impl ops::Div for Complex { } } -impl ops::AddAssign for Complex { +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 { +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 { +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; @@ -144,7 +161,7 @@ impl ops::MulAssign for Complex { } } -impl ops::DivAssign for Complex { +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; @@ -154,26 +171,26 @@ impl ops::DivAssign for Complex { } } -impl ops::AddAssign for Complex { +impl ops::AddAssign for Complex { fn add_assign(&mut self, other: T) { self.real += other; } } -impl ops::SubAssign for Complex { +impl ops::SubAssign for Complex { fn sub_assign(&mut self, other: T) { self.real -= other; } } -impl ops::MulAssign for Complex { +impl ops::MulAssign for Complex { fn mul_assign(&mut self, other: T) { self.real *= other; self.imaginary *= other; } } -impl ops::DivAssign for Complex { +impl ops::DivAssign for Complex { fn div_assign(&mut self, other: T) { self.real /= other; self.imaginary /= other; -- cgit v1.3