Skip to content

Commit 463436f

Browse files
committed
auto merge of #14167 : cmr/rust/cmplx, r=alexcrichton
[breaking-change]
2 parents e162438 + 2886938 commit 463436f

File tree

1 file changed

+53
-53
lines changed

1 file changed

+53
-53
lines changed

src/libnum/complex.rs

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ use std::num::{Zero,One,ToStrRadix};
2323

2424
/// A complex number in Cartesian form.
2525
#[deriving(Eq,Clone)]
26-
pub struct Cmplx<T> {
26+
pub struct Complex<T> {
2727
/// Real portion of the complex number
2828
pub re: T,
2929
/// Imaginary portion of the complex number
3030
pub im: T
3131
}
3232

33-
pub type Complex32 = Cmplx<f32>;
34-
pub type Complex64 = Cmplx<f64>;
33+
pub type Complex32 = Complex<f32>;
34+
pub type Complex64 = Complex<f64>;
3535

36-
impl<T: Clone + Num> Cmplx<T> {
37-
/// Create a new Cmplx
36+
impl<T: Clone + Num> Complex<T> {
37+
/// Create a new Complex
3838
#[inline]
39-
pub fn new(re: T, im: T) -> Cmplx<T> {
40-
Cmplx { re: re, im: im }
39+
pub fn new(re: T, im: T) -> Complex<T> {
40+
Complex { re: re, im: im }
4141
}
4242

4343
/**
@@ -52,41 +52,41 @@ impl<T: Clone + Num> Cmplx<T> {
5252

5353
/// Returns the complex conjugate. i.e. `re - i im`
5454
#[inline]
55-
pub fn conj(&self) -> Cmplx<T> {
56-
Cmplx::new(self.re.clone(), -self.im)
55+
pub fn conj(&self) -> Complex<T> {
56+
Complex::new(self.re.clone(), -self.im)
5757
}
5858

5959

6060
/// Multiplies `self` by the scalar `t`.
6161
#[inline]
62-
pub fn scale(&self, t: T) -> Cmplx<T> {
63-
Cmplx::new(self.re * t, self.im * t)
62+
pub fn scale(&self, t: T) -> Complex<T> {
63+
Complex::new(self.re * t, self.im * t)
6464
}
6565

6666
/// Divides `self` by the scalar `t`.
6767
#[inline]
68-
pub fn unscale(&self, t: T) -> Cmplx<T> {
69-
Cmplx::new(self.re / t, self.im / t)
68+
pub fn unscale(&self, t: T) -> Complex<T> {
69+
Complex::new(self.re / t, self.im / t)
7070
}
7171

7272
/// Returns `1/self`
7373
#[inline]
74-
pub fn inv(&self) -> Cmplx<T> {
74+
pub fn inv(&self) -> Complex<T> {
7575
let norm_sqr = self.norm_sqr();
76-
Cmplx::new(self.re / norm_sqr,
76+
Complex::new(self.re / norm_sqr,
7777
-self.im / norm_sqr)
7878
}
7979
}
8080

81-
impl<T: Clone + Float> Cmplx<T> {
81+
impl<T: Clone + Float> Complex<T> {
8282
/// Calculate |self|
8383
#[inline]
8484
pub fn norm(&self) -> T {
8585
self.re.hypot(self.im)
8686
}
8787
}
8888

89-
impl<T: Clone + Float> Cmplx<T> {
89+
impl<T: Clone + Float> Complex<T> {
9090
/// Calculate the principal Arg of self.
9191
#[inline]
9292
pub fn arg(&self) -> T {
@@ -100,58 +100,58 @@ impl<T: Clone + Float> Cmplx<T> {
100100
}
101101
/// Convert a polar representation into a complex number.
102102
#[inline]
103-
pub fn from_polar(r: &T, theta: &T) -> Cmplx<T> {
104-
Cmplx::new(*r * theta.cos(), *r * theta.sin())
103+
pub fn from_polar(r: &T, theta: &T) -> Complex<T> {
104+
Complex::new(*r * theta.cos(), *r * theta.sin())
105105
}
106106
}
107107

108108
/* arithmetic */
109109
// (a + i b) + (c + i d) == (a + c) + i (b + d)
110-
impl<T: Clone + Num> Add<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
110+
impl<T: Clone + Num> Add<Complex<T>, Complex<T>> for Complex<T> {
111111
#[inline]
112-
fn add(&self, other: &Cmplx<T>) -> Cmplx<T> {
113-
Cmplx::new(self.re + other.re, self.im + other.im)
112+
fn add(&self, other: &Complex<T>) -> Complex<T> {
113+
Complex::new(self.re + other.re, self.im + other.im)
114114
}
115115
}
116116
// (a + i b) - (c + i d) == (a - c) + i (b - d)
117-
impl<T: Clone + Num> Sub<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
117+
impl<T: Clone + Num> Sub<Complex<T>, Complex<T>> for Complex<T> {
118118
#[inline]
119-
fn sub(&self, other: &Cmplx<T>) -> Cmplx<T> {
120-
Cmplx::new(self.re - other.re, self.im - other.im)
119+
fn sub(&self, other: &Complex<T>) -> Complex<T> {
120+
Complex::new(self.re - other.re, self.im - other.im)
121121
}
122122
}
123123
// (a + i b) * (c + i d) == (a*c - b*d) + i (a*d + b*c)
124-
impl<T: Clone + Num> Mul<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
124+
impl<T: Clone + Num> Mul<Complex<T>, Complex<T>> for Complex<T> {
125125
#[inline]
126-
fn mul(&self, other: &Cmplx<T>) -> Cmplx<T> {
127-
Cmplx::new(self.re*other.re - self.im*other.im,
126+
fn mul(&self, other: &Complex<T>) -> Complex<T> {
127+
Complex::new(self.re*other.re - self.im*other.im,
128128
self.re*other.im + self.im*other.re)
129129
}
130130
}
131131

132132
// (a + i b) / (c + i d) == [(a + i b) * (c - i d)] / (c*c + d*d)
133133
// == [(a*c + b*d) / (c*c + d*d)] + i [(b*c - a*d) / (c*c + d*d)]
134-
impl<T: Clone + Num> Div<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
134+
impl<T: Clone + Num> Div<Complex<T>, Complex<T>> for Complex<T> {
135135
#[inline]
136-
fn div(&self, other: &Cmplx<T>) -> Cmplx<T> {
136+
fn div(&self, other: &Complex<T>) -> Complex<T> {
137137
let norm_sqr = other.norm_sqr();
138-
Cmplx::new((self.re*other.re + self.im*other.im) / norm_sqr,
138+
Complex::new((self.re*other.re + self.im*other.im) / norm_sqr,
139139
(self.im*other.re - self.re*other.im) / norm_sqr)
140140
}
141141
}
142142

143-
impl<T: Clone + Num> Neg<Cmplx<T>> for Cmplx<T> {
143+
impl<T: Clone + Num> Neg<Complex<T>> for Complex<T> {
144144
#[inline]
145-
fn neg(&self) -> Cmplx<T> {
146-
Cmplx::new(-self.re, -self.im)
145+
fn neg(&self) -> Complex<T> {
146+
Complex::new(-self.re, -self.im)
147147
}
148148
}
149149

150150
/* constants */
151-
impl<T: Clone + Num> Zero for Cmplx<T> {
151+
impl<T: Clone + Num> Zero for Complex<T> {
152152
#[inline]
153-
fn zero() -> Cmplx<T> {
154-
Cmplx::new(Zero::zero(), Zero::zero())
153+
fn zero() -> Complex<T> {
154+
Complex::new(Zero::zero(), Zero::zero())
155155
}
156156

157157
#[inline]
@@ -160,15 +160,15 @@ impl<T: Clone + Num> Zero for Cmplx<T> {
160160
}
161161
}
162162

163-
impl<T: Clone + Num> One for Cmplx<T> {
163+
impl<T: Clone + Num> One for Complex<T> {
164164
#[inline]
165-
fn one() -> Cmplx<T> {
166-
Cmplx::new(One::one(), Zero::zero())
165+
fn one() -> Complex<T> {
166+
Complex::new(One::one(), Zero::zero())
167167
}
168168
}
169169

170170
/* string conversions */
171-
impl<T: fmt::Show + Num + Ord> fmt::Show for Cmplx<T> {
171+
impl<T: fmt::Show + Num + Ord> fmt::Show for Complex<T> {
172172
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
173173
if self.im < Zero::zero() {
174174
write!(f.buf, "{}-{}i", self.re, -self.im)
@@ -178,7 +178,7 @@ impl<T: fmt::Show + Num + Ord> fmt::Show for Cmplx<T> {
178178
}
179179
}
180180

181-
impl<T: ToStrRadix + Num + Ord> ToStrRadix for Cmplx<T> {
181+
impl<T: ToStrRadix + Num + Ord> ToStrRadix for Complex<T> {
182182
fn to_str_radix(&self, radix: uint) -> ~str {
183183
if self.im < Zero::zero() {
184184
format!("{}-{}i", self.re.to_str_radix(radix), (-self.im).to_str_radix(radix))
@@ -192,22 +192,22 @@ impl<T: ToStrRadix + Num + Ord> ToStrRadix for Cmplx<T> {
192192
mod test {
193193
#![allow(non_uppercase_statics)]
194194

195-
use super::{Complex64, Cmplx};
195+
use super::{Complex64, Complex};
196196
use std::num::{Zero,One,Float};
197197

198-
pub static _0_0i : Complex64 = Cmplx { re: 0.0, im: 0.0 };
199-
pub static _1_0i : Complex64 = Cmplx { re: 1.0, im: 0.0 };
200-
pub static _1_1i : Complex64 = Cmplx { re: 1.0, im: 1.0 };
201-
pub static _0_1i : Complex64 = Cmplx { re: 0.0, im: 1.0 };
202-
pub static _neg1_1i : Complex64 = Cmplx { re: -1.0, im: 1.0 };
203-
pub static _05_05i : Complex64 = Cmplx { re: 0.5, im: 0.5 };
198+
pub static _0_0i : Complex64 = Complex { re: 0.0, im: 0.0 };
199+
pub static _1_0i : Complex64 = Complex { re: 1.0, im: 0.0 };
200+
pub static _1_1i : Complex64 = Complex { re: 1.0, im: 1.0 };
201+
pub static _0_1i : Complex64 = Complex { re: 0.0, im: 1.0 };
202+
pub static _neg1_1i : Complex64 = Complex { re: -1.0, im: 1.0 };
203+
pub static _05_05i : Complex64 = Complex { re: 0.5, im: 0.5 };
204204
pub static all_consts : [Complex64, .. 5] = [_0_0i, _1_0i, _1_1i, _neg1_1i, _05_05i];
205205

206206
#[test]
207207
fn test_consts() {
208-
// check our constants are what Cmplx::new creates
208+
// check our constants are what Complex::new creates
209209
fn test(c : Complex64, r : f64, i: f64) {
210-
assert_eq!(c, Cmplx::new(r,i));
210+
assert_eq!(c, Complex::new(r,i));
211211
}
212212
test(_0_0i, 0.0, 0.0);
213213
test(_1_0i, 1.0, 0.0);
@@ -246,7 +246,7 @@ mod test {
246246
#[test]
247247
fn test_conj() {
248248
for &c in all_consts.iter() {
249-
assert_eq!(c.conj(), Cmplx::new(c.re, -c.im));
249+
assert_eq!(c.conj(), Complex::new(c.re, -c.im));
250250
assert_eq!(c.conj().conj(), c);
251251
}
252252
}
@@ -280,7 +280,7 @@ mod test {
280280
fn test_polar_conv() {
281281
fn test(c: Complex64) {
282282
let (r, theta) = c.to_polar();
283-
assert!((c - Cmplx::from_polar(&r, &theta)).norm() < 1e-6);
283+
assert!((c - Complex::from_polar(&r, &theta)).norm() < 1e-6);
284284
}
285285
for &c in all_consts.iter() { test(c); }
286286
}

0 commit comments

Comments
 (0)