From 2886938169060f7a4e4ef50994bd4908bdcfcb20 Mon Sep 17 00:00:00 2001 From: Corey Richardson Date: Mon, 12 May 2014 21:07:08 -0700 Subject: [PATCH] num: rename Cmplx to Complex Cmplx is a uselessly short name. Change it to be more clear. [breaking-change] --- src/libnum/complex.rs | 106 +++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/src/libnum/complex.rs b/src/libnum/complex.rs index 930dbe40ec7..3a666273d4a 100644 --- a/src/libnum/complex.rs +++ b/src/libnum/complex.rs @@ -23,21 +23,21 @@ use std::num::{Zero,One,ToStrRadix}; /// A complex number in Cartesian form. #[deriving(Eq,Clone)] -pub struct Cmplx { +pub struct Complex { /// Real portion of the complex number pub re: T, /// Imaginary portion of the complex number pub im: T } -pub type Complex32 = Cmplx; -pub type Complex64 = Cmplx; +pub type Complex32 = Complex; +pub type Complex64 = Complex; -impl Cmplx { - /// Create a new Cmplx +impl Complex { + /// Create a new Complex #[inline] - pub fn new(re: T, im: T) -> Cmplx { - Cmplx { re: re, im: im } + pub fn new(re: T, im: T) -> Complex { + Complex { re: re, im: im } } /** @@ -52,33 +52,33 @@ impl Cmplx { /// Returns the complex conjugate. i.e. `re - i im` #[inline] - pub fn conj(&self) -> Cmplx { - Cmplx::new(self.re.clone(), -self.im) + pub fn conj(&self) -> Complex { + Complex::new(self.re.clone(), -self.im) } /// Multiplies `self` by the scalar `t`. #[inline] - pub fn scale(&self, t: T) -> Cmplx { - Cmplx::new(self.re * t, self.im * t) + pub fn scale(&self, t: T) -> Complex { + Complex::new(self.re * t, self.im * t) } /// Divides `self` by the scalar `t`. #[inline] - pub fn unscale(&self, t: T) -> Cmplx { - Cmplx::new(self.re / t, self.im / t) + pub fn unscale(&self, t: T) -> Complex { + Complex::new(self.re / t, self.im / t) } /// Returns `1/self` #[inline] - pub fn inv(&self) -> Cmplx { + pub fn inv(&self) -> Complex { let norm_sqr = self.norm_sqr(); - Cmplx::new(self.re / norm_sqr, + Complex::new(self.re / norm_sqr, -self.im / norm_sqr) } } -impl Cmplx { +impl Complex { /// Calculate |self| #[inline] pub fn norm(&self) -> T { @@ -86,7 +86,7 @@ impl Cmplx { } } -impl Cmplx { +impl Complex { /// Calculate the principal Arg of self. #[inline] pub fn arg(&self) -> T { @@ -100,58 +100,58 @@ impl Cmplx { } /// Convert a polar representation into a complex number. #[inline] - pub fn from_polar(r: &T, theta: &T) -> Cmplx { - Cmplx::new(*r * theta.cos(), *r * theta.sin()) + pub fn from_polar(r: &T, theta: &T) -> Complex { + Complex::new(*r * theta.cos(), *r * theta.sin()) } } /* arithmetic */ // (a + i b) + (c + i d) == (a + c) + i (b + d) -impl Add, Cmplx> for Cmplx { +impl Add, Complex> for Complex { #[inline] - fn add(&self, other: &Cmplx) -> Cmplx { - Cmplx::new(self.re + other.re, self.im + other.im) + fn add(&self, other: &Complex) -> Complex { + Complex::new(self.re + other.re, self.im + other.im) } } // (a + i b) - (c + i d) == (a - c) + i (b - d) -impl Sub, Cmplx> for Cmplx { +impl Sub, Complex> for Complex { #[inline] - fn sub(&self, other: &Cmplx) -> Cmplx { - Cmplx::new(self.re - other.re, self.im - other.im) + fn sub(&self, other: &Complex) -> Complex { + Complex::new(self.re - other.re, self.im - other.im) } } // (a + i b) * (c + i d) == (a*c - b*d) + i (a*d + b*c) -impl Mul, Cmplx> for Cmplx { +impl Mul, Complex> for Complex { #[inline] - fn mul(&self, other: &Cmplx) -> Cmplx { - Cmplx::new(self.re*other.re - self.im*other.im, + fn mul(&self, other: &Complex) -> Complex { + Complex::new(self.re*other.re - self.im*other.im, self.re*other.im + self.im*other.re) } } // (a + i b) / (c + i d) == [(a + i b) * (c - i d)] / (c*c + d*d) // == [(a*c + b*d) / (c*c + d*d)] + i [(b*c - a*d) / (c*c + d*d)] -impl Div, Cmplx> for Cmplx { +impl Div, Complex> for Complex { #[inline] - fn div(&self, other: &Cmplx) -> Cmplx { + fn div(&self, other: &Complex) -> Complex { let norm_sqr = other.norm_sqr(); - Cmplx::new((self.re*other.re + self.im*other.im) / norm_sqr, + Complex::new((self.re*other.re + self.im*other.im) / norm_sqr, (self.im*other.re - self.re*other.im) / norm_sqr) } } -impl Neg> for Cmplx { +impl Neg> for Complex { #[inline] - fn neg(&self) -> Cmplx { - Cmplx::new(-self.re, -self.im) + fn neg(&self) -> Complex { + Complex::new(-self.re, -self.im) } } /* constants */ -impl Zero for Cmplx { +impl Zero for Complex { #[inline] - fn zero() -> Cmplx { - Cmplx::new(Zero::zero(), Zero::zero()) + fn zero() -> Complex { + Complex::new(Zero::zero(), Zero::zero()) } #[inline] @@ -160,15 +160,15 @@ impl Zero for Cmplx { } } -impl One for Cmplx { +impl One for Complex { #[inline] - fn one() -> Cmplx { - Cmplx::new(One::one(), Zero::zero()) + fn one() -> Complex { + Complex::new(One::one(), Zero::zero()) } } /* string conversions */ -impl fmt::Show for Cmplx { +impl fmt::Show for Complex { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if self.im < Zero::zero() { write!(f.buf, "{}-{}i", self.re, -self.im) @@ -178,7 +178,7 @@ impl fmt::Show for Cmplx { } } -impl ToStrRadix for Cmplx { +impl ToStrRadix for Complex { fn to_str_radix(&self, radix: uint) -> ~str { if self.im < Zero::zero() { format!("{}-{}i", self.re.to_str_radix(radix), (-self.im).to_str_radix(radix)) @@ -192,22 +192,22 @@ impl ToStrRadix for Cmplx { mod test { #![allow(non_uppercase_statics)] - use super::{Complex64, Cmplx}; + use super::{Complex64, Complex}; use std::num::{Zero,One,Float}; - pub static _0_0i : Complex64 = Cmplx { re: 0.0, im: 0.0 }; - pub static _1_0i : Complex64 = Cmplx { re: 1.0, im: 0.0 }; - pub static _1_1i : Complex64 = Cmplx { re: 1.0, im: 1.0 }; - pub static _0_1i : Complex64 = Cmplx { re: 0.0, im: 1.0 }; - pub static _neg1_1i : Complex64 = Cmplx { re: -1.0, im: 1.0 }; - pub static _05_05i : Complex64 = Cmplx { re: 0.5, im: 0.5 }; + pub static _0_0i : Complex64 = Complex { re: 0.0, im: 0.0 }; + pub static _1_0i : Complex64 = Complex { re: 1.0, im: 0.0 }; + pub static _1_1i : Complex64 = Complex { re: 1.0, im: 1.0 }; + pub static _0_1i : Complex64 = Complex { re: 0.0, im: 1.0 }; + pub static _neg1_1i : Complex64 = Complex { re: -1.0, im: 1.0 }; + pub static _05_05i : Complex64 = Complex { re: 0.5, im: 0.5 }; pub static all_consts : [Complex64, .. 5] = [_0_0i, _1_0i, _1_1i, _neg1_1i, _05_05i]; #[test] fn test_consts() { - // check our constants are what Cmplx::new creates + // check our constants are what Complex::new creates fn test(c : Complex64, r : f64, i: f64) { - assert_eq!(c, Cmplx::new(r,i)); + assert_eq!(c, Complex::new(r,i)); } test(_0_0i, 0.0, 0.0); test(_1_0i, 1.0, 0.0); @@ -246,7 +246,7 @@ mod test { #[test] fn test_conj() { for &c in all_consts.iter() { - assert_eq!(c.conj(), Cmplx::new(c.re, -c.im)); + assert_eq!(c.conj(), Complex::new(c.re, -c.im)); assert_eq!(c.conj().conj(), c); } } @@ -280,7 +280,7 @@ mod test { fn test_polar_conv() { fn test(c: Complex64) { let (r, theta) = c.to_polar(); - assert!((c - Cmplx::from_polar(&r, &theta)).norm() < 1e-6); + assert!((c - Complex::from_polar(&r, &theta)).norm() < 1e-6); } for &c in all_consts.iter() { test(c); } }