aboutsummaryrefslogtreecommitdiff
path: root/src/complex.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/complex.rs')
-rw-r--r--src/complex.rs30
1 files changed, 15 insertions, 15 deletions
diff --git a/src/complex.rs b/src/complex.rs
index d120c29..5b95df4 100644
--- a/src/complex.rs
+++ b/src/complex.rs
@@ -1,6 +1,6 @@
+use std::f64::consts::E;
use std::num::ParseFloatError;
use std::str::FromStr;
-use std::f64::consts::E;
#[derive(Clone, Copy, Default, Debug)]
pub struct Complex {
@@ -13,12 +13,8 @@ impl Complex {
Self { real, imag }
}
- pub fn div(&mut self, other: &Self) {
- let z = other.real * other.real + other.imag * other.imag;
- self.real = self.real * other.real + self.imag * other.imag;
- self.real /= z;
- self.imag = self.imag * other.real - self.real * other.imag;
- self.imag /= z;
+ pub fn is_real(&self) -> bool {
+ self.imag == 0.0
}
pub fn abs(&self) -> f64 {
@@ -26,12 +22,16 @@ impl Complex {
}
pub fn pow(&self, rhs: Self) -> Self {
- let r = self.abs();
- let x = (self.imag / self.real).atan();
- let lnr = r.ln();
- let alpha = lnr * rhs.imag + x * rhs.real;
- let e_exp = E.powf(lnr * rhs.real - rhs.imag * x);
- Complex::new(e_exp * alpha.cos(), e_exp * alpha.sin())
+ if self.is_real() && rhs.is_real() {
+ Complex::new(self.real.powf(rhs.real), 0.0)
+ } else {
+ let r = self.abs();
+ let x = (self.imag / self.real).atan();
+ let lnr = r.ln();
+ let alpha = lnr * rhs.imag + x * rhs.real;
+ let e_exp = E.powf(lnr * rhs.real - rhs.imag * x);
+ Complex::new(e_exp * alpha.cos(), e_exp * alpha.sin())
+ }
}
pub fn sqrt(&self) -> Self {
@@ -39,14 +39,14 @@ impl Complex {
}
pub fn cos(&self) -> Self {
- let e : Complex = E.into();
+ let e: Complex = E.into();
let i = Complex::new(0.0, 1.0);
Complex::new(e.pow(&i * self).real, 0.0)
}
pub fn sin(&self) -> Self {
- let e : Complex = E.into();
+ let e: Complex = E.into();
let i = Complex::new(0.0, 1.0);
Complex::new(e.pow(&i * self).imag, 0.0)