@@ -23,21 +23,21 @@ use std::num::{Zero,One,ToStrRadix};
23
23
24
24
/// A complex number in Cartesian form.
25
25
#[ deriving( Eq , Clone ) ]
26
- pub struct Cmplx < T > {
26
+ pub struct Complex < T > {
27
27
/// Real portion of the complex number
28
28
pub re : T ,
29
29
/// Imaginary portion of the complex number
30
30
pub im : T
31
31
}
32
32
33
- pub type Complex32 = Cmplx < f32 > ;
34
- pub type Complex64 = Cmplx < f64 > ;
33
+ pub type Complex32 = Complex < f32 > ;
34
+ pub type Complex64 = Complex < f64 > ;
35
35
36
- impl < T : Clone + Num > Cmplx < T > {
37
- /// Create a new Cmplx
36
+ impl < T : Clone + Num > Complex < T > {
37
+ /// Create a new Complex
38
38
#[ 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 }
41
41
}
42
42
43
43
/**
@@ -52,41 +52,41 @@ impl<T: Clone + Num> Cmplx<T> {
52
52
53
53
/// Returns the complex conjugate. i.e. `re - i im`
54
54
#[ 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 )
57
57
}
58
58
59
59
60
60
/// Multiplies `self` by the scalar `t`.
61
61
#[ 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)
64
64
}
65
65
66
66
/// Divides `self` by the scalar `t`.
67
67
#[ 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)
70
70
}
71
71
72
72
/// Returns `1/self`
73
73
#[ inline]
74
- pub fn inv ( & self ) -> Cmplx < T > {
74
+ pub fn inv ( & self ) -> Complex < T > {
75
75
let norm_sqr = self . norm_sqr ( ) ;
76
- Cmplx :: new ( self . re / norm_sqr,
76
+ Complex :: new ( self . re / norm_sqr,
77
77
-self . im / norm_sqr)
78
78
}
79
79
}
80
80
81
- impl < T : Clone + Float > Cmplx < T > {
81
+ impl < T : Clone + Float > Complex < T > {
82
82
/// Calculate |self|
83
83
#[ inline]
84
84
pub fn norm ( & self ) -> T {
85
85
self . re . hypot ( self . im )
86
86
}
87
87
}
88
88
89
- impl < T : Clone + Float > Cmplx < T > {
89
+ impl < T : Clone + Float > Complex < T > {
90
90
/// Calculate the principal Arg of self.
91
91
#[ inline]
92
92
pub fn arg ( & self ) -> T {
@@ -100,58 +100,58 @@ impl<T: Clone + Float> Cmplx<T> {
100
100
}
101
101
/// Convert a polar representation into a complex number.
102
102
#[ 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 ( ) )
105
105
}
106
106
}
107
107
108
108
/* arithmetic */
109
109
// (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 > {
111
111
#[ 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 )
114
114
}
115
115
}
116
116
// (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 > {
118
118
#[ 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 )
121
121
}
122
122
}
123
123
// (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 > {
125
125
#[ 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 ,
128
128
self . re * other. im + self . im * other. re )
129
129
}
130
130
}
131
131
132
132
// (a + i b) / (c + i d) == [(a + i b) * (c - i d)] / (c*c + d*d)
133
133
// == [(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 > {
135
135
#[ inline]
136
- fn div ( & self , other : & Cmplx < T > ) -> Cmplx < T > {
136
+ fn div ( & self , other : & Complex < T > ) -> Complex < T > {
137
137
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,
139
139
( self . im * other. re - self . re * other. im ) / norm_sqr)
140
140
}
141
141
}
142
142
143
- impl < T : Clone + Num > Neg < Cmplx < T > > for Cmplx < T > {
143
+ impl < T : Clone + Num > Neg < Complex < T > > for Complex < T > {
144
144
#[ 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 )
147
147
}
148
148
}
149
149
150
150
/* constants */
151
- impl < T : Clone + Num > Zero for Cmplx < T > {
151
+ impl < T : Clone + Num > Zero for Complex < T > {
152
152
#[ 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 ( ) )
155
155
}
156
156
157
157
#[ inline]
@@ -160,15 +160,15 @@ impl<T: Clone + Num> Zero for Cmplx<T> {
160
160
}
161
161
}
162
162
163
- impl < T : Clone + Num > One for Cmplx < T > {
163
+ impl < T : Clone + Num > One for Complex < T > {
164
164
#[ 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 ( ) )
167
167
}
168
168
}
169
169
170
170
/* 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 > {
172
172
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
173
173
if self . im < Zero :: zero ( ) {
174
174
write ! ( f. buf, "{}-{}i" , self . re, -self . im)
@@ -178,7 +178,7 @@ impl<T: fmt::Show + Num + Ord> fmt::Show for Cmplx<T> {
178
178
}
179
179
}
180
180
181
- impl < T : ToStrRadix + Num + Ord > ToStrRadix for Cmplx < T > {
181
+ impl < T : ToStrRadix + Num + Ord > ToStrRadix for Complex < T > {
182
182
fn to_str_radix ( & self , radix : uint ) -> ~str {
183
183
if self . im < Zero :: zero ( ) {
184
184
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> {
192
192
mod test {
193
193
#![ allow( non_uppercase_statics) ]
194
194
195
- use super :: { Complex64 , Cmplx } ;
195
+ use super :: { Complex64 , Complex } ;
196
196
use std:: num:: { Zero , One , Float } ;
197
197
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 } ;
204
204
pub static all_consts : [ Complex64 , .. 5 ] = [ _0_0i, _1_0i, _1_1i, _neg1_1i, _05_05i] ;
205
205
206
206
#[ test]
207
207
fn test_consts ( ) {
208
- // check our constants are what Cmplx ::new creates
208
+ // check our constants are what Complex ::new creates
209
209
fn test ( c : Complex64 , r : f64 , i : f64 ) {
210
- assert_eq ! ( c, Cmplx :: new( r, i) ) ;
210
+ assert_eq ! ( c, Complex :: new( r, i) ) ;
211
211
}
212
212
test ( _0_0i, 0.0 , 0.0 ) ;
213
213
test ( _1_0i, 1.0 , 0.0 ) ;
@@ -246,7 +246,7 @@ mod test {
246
246
#[ test]
247
247
fn test_conj ( ) {
248
248
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) ) ;
250
250
assert_eq ! ( c. conj( ) . conj( ) , c) ;
251
251
}
252
252
}
@@ -280,7 +280,7 @@ mod test {
280
280
fn test_polar_conv ( ) {
281
281
fn test ( c : Complex64 ) {
282
282
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 ) ;
284
284
}
285
285
for & c in all_consts. iter ( ) { test ( c) ; }
286
286
}
0 commit comments