From ae8abfa8335edf856f423e7a60c5bb830a79d5a7 Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 22 Sep 2024 14:24:27 +0200 Subject: Refactor and implement other gates --- libpsi/src/maths/complex.rs | 183 ++++++-------------------------------------- 1 file changed, 23 insertions(+), 160 deletions(-) (limited to 'libpsi/src/maths/complex.rs') 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 fmt::Display for Complex { } } +impl Numeric for Complex { + fn zero() -> Self { + Complex::new(0.0, 0.0) + } + + fn one() -> Self { + Complex::new(1.0, 0.0) + } +} + +impl Numeric for Complex { + fn zero() -> Self { + Complex::new(0.0, 0.0) + } + + fn one() -> Self { + Complex::new(1.0, 0.0) + } +} + impl Complex { pub fn new(real: T, imaginary: T) -> Complex { Complex { real, imaginary } @@ -58,162 +80,3 @@ impl Complex { T::sqrt(self.norm()) } } - -impl ops::Neg for Complex { - type Output = Complex; - - fn neg(self) -> Complex { - Complex { - real: -self.real, - imaginary: -self.imaginary, - } - } -} - -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, - } - } -} - -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, - } - } -} - -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; - } -} - -impl ops::AddAssign for Complex { - fn add_assign(&mut self, other: T) { - self.real += other; - } -} - -impl ops::SubAssign for Complex { - fn sub_assign(&mut self, other: T) { - self.real -= other; - } -} - -impl ops::MulAssign for Complex { - fn mul_assign(&mut self, other: T) { - self.real *= other; - self.imaginary *= other; - } -} - -impl ops::DivAssign for Complex { - fn div_assign(&mut self, other: T) { - self.real /= other; - self.imaginary /= other; - } -} -- cgit v1.3