Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
donjuanplatinum committed Jun 27, 2024
1 parent bf2fd26 commit 021d794
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions src/math/complex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::NumOps;
use core::ops::{Add, Mul, Sub,Div};

use core::ops::{Add, Div, Mul, Sub};

/// impl<'a,'b,T: Clone + NumOps> Method<&'a Complex<T>> for &'b Complex<T>
macro_rules! impl_ref_ref {
Expand Down Expand Up @@ -88,20 +87,18 @@ impl_all!(impl Add, add);
impl<T: NumOps> Sub<Complex<T>> for Complex<T> {
type Output = Complex<T>;
#[inline]
fn sub(self,rhs:Complex<T>) -> Self::Output {
fn sub(self, rhs: Complex<T>) -> Self::Output {
return Complex {
re: self.re - rhs.re,
im: self.im - rhs.im,
};
}
}

impl_all!(impl Sub,sub);
impl_all!(impl Sub, sub);

/// (a+bi) * (c+di) = (a*c - b*d)+(a*d + b*c)i
impl<T: NumOps + Clone> Mul<Complex<T>>
for Complex<T>
{
impl<T: NumOps + Clone> Mul<Complex<T>> for Complex<T> {
type Output = Complex<T>;
#[inline]
fn mul(self, rhs: Complex<T>) -> Self::Output {
Expand All @@ -112,38 +109,29 @@ impl<T: NumOps + Clone> Mul<Complex<T>>
}
}





impl_all!(impl Mul, mul);


/// a+bi / c+di = (a * c + b * d)/ (c*c + d*d) + ((b*c - a*d)/(c*c + d*d))i
impl<T: NumOps + Clone> Div<Complex<T>>
for Complex<T>
{
impl<T: NumOps + Clone> Div<Complex<T>> for Complex<T> {
type Output = Complex<T>;
#[inline]
fn div(self, rhs: Complex<T>) -> Self::Output {
let tmp = rhs.re.clone() * rhs.re.clone() + rhs.im.clone() * rhs.im.clone();
let re = (self.re.clone() * rhs.re.clone() + self.im.clone() * rhs.im.clone()) / tmp.clone();
let im = (self.im.clone() * rhs.re.clone() - self.re.clone() * rhs.im.clone()) / tmp;
return Complex {
re,
im,
};
let tmp = rhs.re.clone() * rhs.re.clone() + rhs.im.clone() * rhs.im.clone();
let re =
(self.re.clone() * rhs.re.clone() + self.im.clone() * rhs.im.clone()) / tmp.clone();
let im = (self.im.clone() * rhs.re.clone() - self.re.clone() * rhs.im.clone()) / tmp;
return Complex { re, im };
}
}

impl_all!(impl Div, div);

/// a+bi -> (a,b)
impl<T> core::fmt::Display for Complex<T>
where T: core::fmt::Display,
where
T: core::fmt::Display,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
return write!(f,"({},{})",self.re,self.im);
return write!(f, "({},{})", self.re, self.im);
}
}

0 comments on commit 021d794

Please sign in to comment.