From 1fb7989e9405f9795a1d5b3953881b9bb8a137b4 Mon Sep 17 00:00:00 2001 From: hachem Date: Sat, 6 Dec 2025 22:33:56 +0100 Subject: [add]: custom gates + rework vertical renderer --- libpsi-core/src/maths/complex.rs | 48 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'libpsi-core/src/maths') diff --git a/libpsi-core/src/maths/complex.rs b/libpsi-core/src/maths/complex.rs index 86e4788..31eae69 100644 --- a/libpsi-core/src/maths/complex.rs +++ b/libpsi-core/src/maths/complex.rs @@ -126,13 +126,53 @@ impl Complex { impl_ops!(Add, add, +); impl_ops!(Sub, sub, -); -impl_ops!(Mul, mul, *); -impl_ops!(Div, div, /); + +impl ops::Mul for Complex { + type Output = Complex; + + fn mul(self, other: Complex) -> Complex { + // (a + bi) * (c + di) = (ac - bd) + (ad + bc)i + 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 { + // (a + bi) / (c + di) = ((ac + bd) + (bc - ad)i) / (c² + d²) + 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_ops!(AddAssign, add_assign, +, assign); impl_ops!(SubAssign, sub_assign, -, assign); -impl_ops!(MulAssign, mul_assign, *, assign); -impl_ops!(DivAssign, div_assign, /, assign); + +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_imag = self.real * other.imaginary + self.imaginary * other.real; + self.real = new_real; + self.imaginary = new_imag; + } +} + +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_imag = (self.imaginary * other.real - self.real * other.imaginary) / denom; + self.real = new_real; + self.imaginary = new_imag; + } +} impl_ops!(Add, add, +, real); impl_ops!(Sub, sub, -, real); -- cgit v1.3