From c9073bec568efa90c85e381c12dbcd20fe86a8f0 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Mon, 5 Dec 2016 21:22:42 +0200 Subject: Refactored number traits --- src/complex.rs | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) (limited to 'src/complex.rs') 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 { @@ -12,39 +12,37 @@ impl Complex { Complex{real: real, imag: imag} } } -impl Complex where T: Neg { +impl Complex where T: SignedArithmeticOps { pub fn conjugate(self) -> Complex { Complex::new(self.real, -self.imag) } } -impl Complex where T: Trig + Mul + Copy { - pub fn from_polar(r: T, theta: T) -> Complex { - let real = r*theta.cos(); - let imag = r*theta.sin(); - Complex::new(real, imag) - } -} - -impl Complex where T: Pow + Add + Copy { +impl Complex where T: Pow + ArithmeticOps + Copy { pub fn magnitude(self) -> T { (self.real.pow(2) + self.imag.pow(2)).sqrt() } } -impl Complex where T: Trig + Copy { +impl Complex where T: Trig { pub fn angle(self) -> T { self.imag.atan2(self.real) } } -impl Complex where T: Trig + Pow + Add + Copy { +impl Complex where T: Trig + Pow + ArithmeticOps + Copy { + pub fn from_polar(r: T, theta: T) -> Complex { + 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 Add for Complex where T: Add + Copy { +impl Add for Complex where T: ArithmeticOps + Copy { type Output = Complex; fn add(self, other: Self) -> Self { @@ -54,7 +52,7 @@ impl Add for Complex where T: Add + Copy { } } -impl Sub for Complex where T: Sub + Copy { +impl Sub for Complex where T: ArithmeticOps + Copy { type Output = Complex; fn sub(self, other: Self) -> Self { @@ -64,7 +62,7 @@ impl Sub for Complex where T: Sub + Copy { } } -impl Mul for Complex where T: Add + Mul + Sub + Copy { +impl Mul for Complex where T: ArithmeticOps + Copy { type Output = Complex; fn mul(self, other: Self) -> Self { @@ -74,7 +72,7 @@ impl Mul for Complex where T: Add + Mul + Sub Div for Complex where T: Add + Mul + Sub + Div + Neg + Copy { +impl Div for Complex where T: SignedArithmeticOps + Copy { type Output = Complex; fn div(self, other: Self) -> Self { @@ -90,6 +88,14 @@ impl Div for Complex where T: Add + Mul + Sub Neg for Complex where T: SignedArithmeticOps + Copy { + type Output = Complex; + + fn neg(self) -> Self { + Complex::new(-self.real, -self.imag) + } +} + #[cfg(test)] mod tests { use super::*; @@ -123,6 +129,12 @@ mod tests { assert_eq!(a/b, Complex::new(2, 0)); } + #[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); -- cgit v1.2.3