diff options
Diffstat (limited to 'src/complex.rs')
| -rw-r--r-- | src/complex.rs | 58 |
1 files changed, 27 insertions, 31 deletions
diff --git a/src/complex.rs b/src/complex.rs index e498234..d120c29 100644 --- a/src/complex.rs +++ b/src/complex.rs @@ -67,12 +67,10 @@ impl std::fmt::Display for Complex { } else { write!(f, "{}i", self.imag) } + } else if self.imag == 1.0 { + write!(f, "{} + i", self.real) } else { - if self.imag == 1.0 { - write!(f, "{} + i", self.real) - } else { - write!(f, "{} + {}i", self.real, self.imag) - } + write!(f, "{} + {}i", self.real, self.imag) } } } @@ -101,25 +99,23 @@ impl FromStr for Complex { c.real = a.parse()?; c.imag = b[..b.len() - 1].parse()?; } - } else { - if s.contains('i') { - if s.len() == 1 { - c.imag = 1.0; - } else { - c.imag = s[..s.len() - 1].parse()?; - } + } else if s.contains('i') { + if s.len() == 1 { + c.imag = 1.0; } else { - c.real = s.parse()?; + c.imag = s[..s.len() - 1].parse()?; } + } else { + c.real = s.parse()?; } Ok(c) } } -impl Into<Complex> for f64 { - fn into(self) -> Complex { - Complex::new(self, 0.0) +impl From<f64> for Complex { + fn from(val: f64) -> Self { + Complex::new(val, 0.0) } } @@ -127,7 +123,7 @@ impl std::ops::Add for &Complex { type Output = Complex; fn add(self, rhs: Self) -> Self::Output { - Complex::new(self.real + rhs.real, self.imag + rhs.imag) + *self + *rhs } } @@ -135,7 +131,7 @@ impl std::ops::Add for Complex { type Output = Complex; fn add(self, rhs: Self) -> Self::Output { - &self + &rhs + Complex::new(self.real + rhs.real, self.imag + rhs.imag) } } @@ -143,7 +139,7 @@ impl std::ops::Sub for &Complex { type Output = Complex; fn sub(self, rhs: Self) -> Self::Output { - Complex::new(self.real - rhs.real, self.imag - rhs.imag) + *self - *rhs } } @@ -151,7 +147,7 @@ impl std::ops::Sub for Complex { type Output = Complex; fn sub(self, rhs: Self) -> Self::Output { - &self - &rhs + Complex::new(self.real - rhs.real, self.imag - rhs.imag) } } @@ -159,10 +155,7 @@ impl std::ops::Mul for &Complex { type Output = Complex; fn mul(self, rhs: Self) -> Self::Output { - Complex::new( - self.real * rhs.real - self.imag * rhs.imag, - self.real * rhs.imag + self.imag * rhs.real, - ) + *self * *rhs } } @@ -170,7 +163,10 @@ impl std::ops::Mul for Complex { type Output = Complex; fn mul(self, rhs: Self) -> Self::Output { - &self * &rhs + Complex::new( + self.real * rhs.real - self.imag * rhs.imag, + self.real * rhs.imag + self.imag * rhs.real, + ) } } @@ -178,11 +174,7 @@ impl std::ops::Div for &Complex { type Output = Complex; fn div(self, rhs: Self) -> Self::Output { - let z = rhs.real * rhs.real + rhs.imag * rhs.imag; - Complex::new( - (self.real * rhs.real + self.imag * rhs.imag) / z, - (self.imag * rhs.real - self.real * rhs.imag) / z, - ) + *self / *rhs } } @@ -190,6 +182,10 @@ impl std::ops::Div for Complex { type Output = Complex; fn div(self, rhs: Self) -> Self::Output { - &self / &rhs + let z = rhs.real * rhs.real + rhs.imag * rhs.imag; + Complex::new( + (self.real * rhs.real + self.imag * rhs.imag) / z, + (self.imag * rhs.real - self.real * rhs.imag) / z, + ) } } |