Skip to content

Commit 82d5460

Browse files
committed
libstd: add basic complex numbers
1 parent 7b0401d commit 82d5460

File tree

2 files changed

+318
-0
lines changed

2 files changed

+318
-0
lines changed

src/libstd/num/complex.rs

Lines changed: 316 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,316 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
//! Complex numbers.
13+
14+
use core::num::{Zero,One,ToStrRadix};
15+
use core::prelude::*;
16+
17+
// FIXME #1284: handle complex NaN & infinity etc. This
18+
// probably doesn't map to C's _Complex correctly.
19+
20+
// XXX: Need generic sqrt to implement .norm(). Need generic sin/cos
21+
// for .to/from_polar().
22+
23+
24+
/// A complex number in Cartesian form.
25+
#[deriving(Eq,Clone)]
26+
pub struct Cmplx<T> {
27+
re: T,
28+
im: T
29+
}
30+
31+
pub type Complex = Cmplx<float>;
32+
pub type Complex32 = Cmplx<f32>;
33+
pub type Complex64 = Cmplx<f64>;
34+
35+
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
36+
Cmplx<T> {
37+
/// Create a new Cmplx
38+
#[inline]
39+
pub fn new(re: T, im: T) -> Cmplx<T> {
40+
Cmplx { re: re, im: im }
41+
}
42+
43+
/**
44+
Returns the square of the norm (since `T` doesn't necessarily
45+
have a sqrt function), i.e. `re^2 + im^2`.
46+
*/
47+
#[inline]
48+
pub fn norm_sqr(&self) -> T {
49+
self.re * self.re + self.im * self.im
50+
}
51+
52+
53+
/// Returns the complex conjugate. i.e. `re - i im`
54+
#[inline]
55+
pub fn conj(&self) -> Cmplx<T> {
56+
Cmplx::new(self.re, -self.im)
57+
}
58+
59+
60+
/// Multiplies `self` by the scalar `t`.
61+
#[inline]
62+
pub fn scale(&self, t: T) -> Cmplx<T> {
63+
Cmplx::new(self.re * t, self.im * t)
64+
}
65+
66+
/// Divides `self` by the scalar `t`.
67+
#[inline]
68+
pub fn unscale(&self, t: T) -> Cmplx<T> {
69+
Cmplx::new(self.re / t, self.im / t)
70+
}
71+
72+
/// Returns `1/self`
73+
#[inline]
74+
pub fn inv(&self) -> Cmplx<T> {
75+
let norm_sqr = self.norm_sqr();
76+
Cmplx::new(self.re / norm_sqr,
77+
-self.im / norm_sqr)
78+
79+
}
80+
}
81+
82+
/* arithmetic */
83+
// (a + i b) + (c + i d) == (a + c) + i (b + d)
84+
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
85+
Add<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
86+
#[inline]
87+
fn add(&self, other: &Cmplx<T>) -> Cmplx<T> {
88+
Cmplx::new(self.re + other.re, self.im + other.im)
89+
}
90+
}
91+
// (a + i b) - (c + i d) == (a - c) + i (b - d)
92+
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
93+
Sub<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
94+
#[inline]
95+
fn sub(&self, other: &Cmplx<T>) -> Cmplx<T> {
96+
Cmplx::new(self.re - other.re, self.im - other.im)
97+
}
98+
}
99+
// (a + i b) * (c + i d) == (a*c - b*d) + i (a*d + b*c)
100+
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
101+
Mul<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
102+
#[inline]
103+
fn mul(&self, other: &Cmplx<T>) -> Cmplx<T> {
104+
Cmplx::new(self.re*other.re - self.im*other.im,
105+
self.re*other.im + self.im*other.re)
106+
}
107+
}
108+
109+
// (a + i b) / (c + i d) == [(a + i b) * (c - i d)] / (c*c + d*d)
110+
// == [(a*c + b*d) / (c*c + d*d)] + i [(b*c - a*d) / (c*c + d*d)]
111+
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
112+
Div<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
113+
#[inline]
114+
fn div(&self, other: &Cmplx<T>) -> Cmplx<T> {
115+
let norm_sqr = other.norm_sqr();
116+
Cmplx::new((self.re*other.re + self.im*other.im) / norm_sqr,
117+
(self.im*other.re - self.re*other.im) / norm_sqr)
118+
}
119+
}
120+
121+
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
122+
Neg<Cmplx<T>> for Cmplx<T> {
123+
#[inline]
124+
fn neg(&self) -> Cmplx<T> {
125+
Cmplx::new(-self.re, -self.im)
126+
}
127+
}
128+
129+
/* constants */
130+
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T> + Zero>
131+
Zero for Cmplx<T> {
132+
#[inline]
133+
fn zero() -> Cmplx<T> {
134+
Cmplx::new(Zero::zero(), Zero::zero())
135+
}
136+
}
137+
138+
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T> + Zero + One>
139+
One for Cmplx<T> {
140+
#[inline]
141+
fn one() -> Cmplx<T> {
142+
Cmplx::new(One::one(), Zero::zero())
143+
}
144+
}
145+
146+
/* string conversions */
147+
impl<T: ToStr + Zero + Ord + Neg<T>> ToStr for Cmplx<T> {
148+
fn to_str(&self) -> ~str {
149+
if self.im < Zero::zero() {
150+
fmt!("%s-%si", self.re.to_str(), (-self.im).to_str())
151+
} else {
152+
fmt!("%s+%si", self.re.to_str(), self.im.to_str())
153+
}
154+
}
155+
}
156+
157+
impl<T: ToStrRadix + Zero + Ord + Neg<T>> ToStrRadix for Cmplx<T> {
158+
fn to_str_radix(&self, radix: uint) -> ~str {
159+
if self.im < Zero::zero() {
160+
fmt!("%s-%si", self.re.to_str_radix(radix), (-self.im).to_str_radix(radix))
161+
} else {
162+
fmt!("%s+%si", self.re.to_str_radix(radix), self.im.to_str_radix(radix))
163+
}
164+
}
165+
}
166+
167+
#[cfg(test)]
168+
mod test {
169+
use core::prelude::*;
170+
use super::*;
171+
use core::num::{Zero,One};
172+
173+
pub static _0_0i : Complex = Cmplx { re: 0f, im: 0f };
174+
pub static _1_0i : Complex = Cmplx { re: 1f, im: 0f };
175+
pub static _1_1i : Complex = Cmplx { re: 1f, im: 1f };
176+
pub static _0_1i : Complex = Cmplx { re: 0f, im: 1f };
177+
pub static _neg1_1i : Complex = Cmplx { re: -1f, im: 1f };
178+
pub static _05_05i : Complex = Cmplx { re: 0.5f, im: 0.5f };
179+
pub static all_consts : [Complex, .. 5] = [_0_0i, _1_0i, _1_1i, _neg1_1i, _05_05i];
180+
181+
#[test]
182+
fn test_consts() {
183+
// check our constants are what Cmplx::new creates
184+
fn test(c : Complex, r : float, i: float) {
185+
assert_eq!(c, Cmplx::new(r,i));
186+
}
187+
test(_0_0i, 0f, 0f);
188+
test(_1_0i, 1f, 0f);
189+
test(_1_1i, 1f, 1f);
190+
test(_neg1_1i, -1f, 1f);
191+
test(_05_05i, 0.5f, 0.5f);
192+
193+
assert_eq!(_0_0i, Zero::zero());
194+
assert_eq!(_1_0i, One::one());
195+
}
196+
197+
#[test]
198+
fn test_norm_sqr() {
199+
fn test(c: Complex, ns: float) {
200+
assert_eq!(c.norm_sqr(), ns);
201+
}
202+
test(_0_0i, 0f);
203+
test(_1_0i, 1f);
204+
test(_1_1i, 2f);
205+
test(_neg1_1i, 2f);
206+
test(_05_05i, 0.5f);
207+
}
208+
209+
#[test]
210+
fn test_scale_unscale() {
211+
assert_eq!(_05_05i.scale(2f), _1_1i);
212+
assert_eq!(_1_1i.unscale(2f), _05_05i);
213+
for all_consts.each |&c| {
214+
assert_eq!(c.scale(2f).unscale(2f), c);
215+
}
216+
}
217+
218+
#[test]
219+
fn test_conj() {
220+
for all_consts.each |&c| {
221+
assert_eq!(c.conj(), Cmplx::new(c.re, -c.im));
222+
assert_eq!(c.conj().conj(), c);
223+
}
224+
}
225+
226+
#[test]
227+
fn test_inv() {
228+
assert_eq!(_1_1i.inv(), _05_05i.conj());
229+
assert_eq!(_1_0i.inv(), _1_0i.inv());
230+
}
231+
232+
#[test]
233+
#[should_fail]
234+
#[ignore]
235+
fn test_inv_zero() {
236+
// XXX: should this really fail, or just NaN?
237+
_0_0i.inv();
238+
}
239+
240+
241+
mod arith {
242+
use super::*;
243+
use super::super::*;
244+
use core::num::Zero;
245+
246+
#[test]
247+
fn test_add() {
248+
assert_eq!(_05_05i + _05_05i, _1_1i);
249+
assert_eq!(_0_1i + _1_0i, _1_1i);
250+
assert_eq!(_1_0i + _neg1_1i, _0_1i);
251+
252+
for all_consts.each |&c| {
253+
assert_eq!(_0_0i + c, c);
254+
assert_eq!(c + _0_0i, c);
255+
}
256+
}
257+
258+
#[test]
259+
fn test_sub() {
260+
assert_eq!(_05_05i - _05_05i, _0_0i);
261+
assert_eq!(_0_1i - _1_0i, _neg1_1i);
262+
assert_eq!(_0_1i - _neg1_1i, _1_0i);
263+
264+
for all_consts.each |&c| {
265+
assert_eq!(c - _0_0i, c);
266+
assert_eq!(c - c, _0_0i);
267+
}
268+
}
269+
270+
#[test]
271+
fn test_mul() {
272+
assert_eq!(_05_05i * _05_05i, _0_1i.unscale(2f));
273+
assert_eq!(_1_1i * _0_1i, _neg1_1i);
274+
275+
// i^2 & i^4
276+
assert_eq!(_0_1i * _0_1i, -_1_0i);
277+
assert_eq!(_0_1i * _0_1i * _0_1i * _0_1i, _1_0i);
278+
279+
for all_consts.each |&c| {
280+
assert_eq!(c * _1_0i, c);
281+
assert_eq!(_1_0i * c, c);
282+
}
283+
}
284+
#[test]
285+
fn test_div() {
286+
assert_eq!(_neg1_1i / _0_1i, _1_1i);
287+
for all_consts.each |&c| {
288+
if c != Zero::zero() {
289+
assert_eq!(c / c, _1_0i);
290+
}
291+
}
292+
}
293+
#[test]
294+
fn test_neg() {
295+
assert_eq!(-_1_0i + _0_1i, _neg1_1i);
296+
assert_eq!((-_0_1i) * _0_1i, _1_0i);
297+
for all_consts.each |&c| {
298+
assert_eq!(-(-c), c);
299+
}
300+
}
301+
}
302+
303+
#[test]
304+
fn test_to_str() {
305+
fn test(c : Complex, s: ~str) {
306+
assert_eq!(c.to_str(), s);
307+
}
308+
test(_0_0i, ~"0+0i");
309+
test(_1_0i, ~"1+0i");
310+
test(_0_1i, ~"0+1i");
311+
test(_1_1i, ~"1+1i");
312+
test(_neg1_1i, ~"-1+1i");
313+
test(-_neg1_1i, ~"1-1i");
314+
test(_05_05i, ~"0.5+0.5i");
315+
}
316+
}

src/libstd/std.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ pub mod workcache;
9999
pub mod bigint;
100100
#[path="num/rational.rs"]
101101
pub mod rational;
102+
#[path="num/complex.rs"]
103+
pub mod complex;
102104
pub mod stats;
103105
pub mod semver;
104106
pub mod fileinput;

0 commit comments

Comments
 (0)