summaryrefslogtreecommitdiff
path: root/src/complex.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/complex.rs')
-rw-r--r--src/complex.rs46
1 files changed, 29 insertions, 17 deletions
diff --git a/src/complex.rs b/src/complex.rs
index 93495fa..228ba8f 100644
--- a/src/complex.rs
+++ b/src/complex.rs
@@ -1,5 +1,5 @@
use std::ops::{Add, Sub, Mul, Div, Neg};
-use ::num_traits::{Trig, Pow};
+use ::num_traits::{Trig, Pow, ArithmeticOps, SignedArithmeticOps};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Complex<T> {
@@ -12,39 +12,37 @@ impl<T> Complex<T> {
Complex{real: real, imag: imag}
}
}
-impl<T> Complex<T> where T: Neg<Output=T> {
+impl<T> Complex<T> where T: SignedArithmeticOps {
pub fn conjugate(self) -> Complex<T> {
Complex::new(self.real, -self.imag)
}
}
-impl<T> Complex<T> where T: Trig + Mul<Output=T> + Copy {
- pub fn from_polar(r: T, theta: T) -> Complex<T> {
- let real = r*theta.cos();
- let imag = r*theta.sin();
- Complex::new(real, imag)
- }
-}
-
-impl<T> Complex<T> where T: Pow + Add<Output=T> + Copy {
+impl<T> Complex<T> where T: Pow + ArithmeticOps + Copy {
pub fn magnitude(self) -> T {
(self.real.pow(2) + self.imag.pow(2)).sqrt()
}
}
-impl<T> Complex<T> where T: Trig + Copy {
+impl<T> Complex<T> where T: Trig {
pub fn angle(self) -> T {
self.imag.atan2(self.real)
}
}
-impl<T> Complex<T> where T: Trig + Pow + Add<Output=T> + Copy {
+impl<T> Complex<T> where T: Trig + Pow + ArithmeticOps + Copy {
+ pub fn from_polar(r: T, theta: T) -> Complex<T> {
+ let real = r*theta.cos();
+ let imag = r*theta.sin();
+ Complex::new(real, imag)
+ }
+
pub fn to_polar(self) -> (T, T) {
(self.magnitude(), self.angle())
}
}
-impl<T> Add for Complex<T> where T: Add<Output=T> + Copy {
+impl<T> Add for Complex<T> where T: ArithmeticOps + Copy {
type Output = Complex<T>;
fn add(self, other: Self) -> Self {
@@ -54,7 +52,7 @@ impl<T> Add for Complex<T> where T: Add<Output=T> + Copy {
}
}
-impl<T> Sub for Complex<T> where T: Sub<Output=T> + Copy {
+impl<T> Sub for Complex<T> where T: ArithmeticOps + Copy {
type Output = Complex<T>;
fn sub(self, other: Self) -> Self {
@@ -64,7 +62,7 @@ impl<T> Sub for Complex<T> where T: Sub<Output=T> + Copy {
}
}
-impl<T> Mul for Complex<T> where T: Add<Output=T> + Mul<Output=T> + Sub<Output=T> + Copy {
+impl<T> Mul for Complex<T> where T: ArithmeticOps + Copy {
type Output = Complex<T>;
fn mul(self, other: Self) -> Self {
@@ -74,7 +72,7 @@ impl<T> Mul for Complex<T> where T: Add<Output=T> + Mul<Output=T> + Sub<Output=T
}
}
-impl<T> Div for Complex<T> where T: Add<Output=T> + Mul<Output=T> + Sub<Output=T> + Div<Output=T> + Neg<Output=T> + Copy {
+impl<T> Div for Complex<T> where T: SignedArithmeticOps + Copy {
type Output = Complex<T>;
fn div(self, other: Self) -> Self {
@@ -90,6 +88,14 @@ impl<T> Div for Complex<T> where T: Add<Output=T> + Mul<Output=T> + Sub<Output=T
}
}
+impl<T> Neg for Complex<T> where T: SignedArithmeticOps + Copy {
+ type Output = Complex<T>;
+
+ fn neg(self) -> Self {
+ Complex::new(-self.real, -self.imag)
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -124,6 +130,12 @@ mod tests {
}
#[test]
+ fn neg() {
+ let a = Complex::new(6, 8);
+ assert_eq!(-a, Complex::new(0, 0)-a);
+ }
+
+ #[test]
fn from_polar() {
let right = Complex::from_polar(1.0 as f32, 0.0 as f32);
assert!((right.real-1.0).abs() < f32::EPSILON);