Skip to content

Commit 075da9c

Browse files
committed
auto merge of #7050 : huonw/rust/extra-complex-work, r=Aatch
2 parents 6bdd4c8 + 19c31b6 commit 075da9c

File tree

6 files changed

+85
-36
lines changed

6 files changed

+85
-36
lines changed

doc/tutorial-tasks.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ be distributed on the available cores.
318318
fn partial_sum(start: uint) -> f64 {
319319
let mut local_sum = 0f64;
320320
for uint::range(start*100000, (start+1)*100000) |num| {
321-
local_sum += (num as f64 + 1.0).pow(-2.0);
321+
local_sum += (num as f64 + 1.0).pow(&-2.0);
322322
}
323323
local_sum
324324
}
@@ -355,7 +355,7 @@ a single large vector of floats. Each task needs the full vector to perform its
355355
use extra::arc::ARC;
356356
357357
fn pnorm(nums: &~[float], p: uint) -> float {
358-
nums.iter().fold(0.0, |a,b| a+(*b).pow(p as float) ).pow(1f / (p as float))
358+
nums.iter().fold(0.0, |a,b| a+(*b).pow(&(p as float)) ).pow(&(1f / (p as float)))
359359
}
360360
361361
fn main() {

src/libextra/num/complex.rs

+63-14
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub type Complex = Cmplx<float>;
3535
pub type Complex32 = Cmplx<f32>;
3636
pub type Complex64 = Cmplx<f64>;
3737

38-
impl<T: Copy + Num> Cmplx<T> {
38+
impl<T: Clone + Num> Cmplx<T> {
3939
/// Create a new Cmplx
4040
#[inline]
4141
pub fn new(re: T, im: T) -> Cmplx<T> {
@@ -55,7 +55,7 @@ impl<T: Copy + Num> Cmplx<T> {
5555
/// Returns the complex conjugate. i.e. `re - i im`
5656
#[inline]
5757
pub fn conj(&self) -> Cmplx<T> {
58-
Cmplx::new(self.re, -self.im)
58+
Cmplx::new(self.re.clone(), -self.im)
5959
}
6060

6161

@@ -80,62 +80,91 @@ impl<T: Copy + Num> Cmplx<T> {
8080
}
8181
}
8282

83+
#[cfg(not(stage0))] // Fixed by #4228
84+
impl<T: Clone + Algebraic + Num> Cmplx<T> {
85+
/// Calculate |self|
86+
#[inline(always)]
87+
pub fn norm(&self) -> T {
88+
self.re.hypot(&self.im)
89+
}
90+
}
91+
92+
#[cfg(not(stage0))] // Fixed by #4228
93+
impl<T: Clone + Trigonometric + Algebraic + Num> Cmplx<T> {
94+
/// Calculate the principal Arg of self.
95+
#[inline(always)]
96+
pub fn arg(&self) -> T {
97+
self.im.atan2(&self.re)
98+
}
99+
/// Convert to polar form (r, theta), such that `self = r * exp(i
100+
/// * theta)`
101+
#[inline]
102+
pub fn to_polar(&self) -> (T, T) {
103+
(self.norm(), self.arg())
104+
}
105+
/// Convert a polar representation into a complex number.
106+
#[inline]
107+
pub fn from_polar(r: &T, theta: &T) -> Cmplx<T> {
108+
Cmplx::new(r * theta.cos(), r * theta.sin())
109+
}
110+
}
111+
83112
/* arithmetic */
84113
// (a + i b) + (c + i d) == (a + c) + i (b + d)
85-
impl<T: Copy + Num> Add<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
114+
impl<T: Clone + Num> Add<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
86115
#[inline]
87116
fn add(&self, other: &Cmplx<T>) -> Cmplx<T> {
88117
Cmplx::new(self.re + other.re, self.im + other.im)
89118
}
90119
}
91120
// (a + i b) - (c + i d) == (a - c) + i (b - d)
92-
impl<T: Copy + Num> Sub<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
121+
impl<T: Clone + Num> Sub<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
93122
#[inline]
94123
fn sub(&self, other: &Cmplx<T>) -> Cmplx<T> {
95124
Cmplx::new(self.re - other.re, self.im - other.im)
96125
}
97126
}
98127
// (a + i b) * (c + i d) == (a*c - b*d) + i (a*d + b*c)
99-
impl<T: Copy + Num> Mul<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
128+
impl<T: Clone + Num> Mul<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
100129
#[inline]
101130
fn mul(&self, other: &Cmplx<T>) -> Cmplx<T> {
102131
Cmplx::new(self.re*other.re - self.im*other.im,
103-
self.re*other.im + self.im*other.re)
132+
self.re*other.im + self.im*other.re)
104133
}
105134
}
106135

107136
// (a + i b) / (c + i d) == [(a + i b) * (c - i d)] / (c*c + d*d)
108137
// == [(a*c + b*d) / (c*c + d*d)] + i [(b*c - a*d) / (c*c + d*d)]
109-
impl<T: Copy + Num> Div<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
138+
impl<T: Clone + Num> Div<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
110139
#[inline]
111140
fn div(&self, other: &Cmplx<T>) -> Cmplx<T> {
112141
let norm_sqr = other.norm_sqr();
113142
Cmplx::new((self.re*other.re + self.im*other.im) / norm_sqr,
114-
(self.im*other.re - self.re*other.im) / norm_sqr)
143+
(self.im*other.re - self.re*other.im) / norm_sqr)
115144
}
116145
}
117146

118-
impl<T: Copy + Num> Neg<Cmplx<T>> for Cmplx<T> {
147+
impl<T: Clone + Num> Neg<Cmplx<T>> for Cmplx<T> {
119148
#[inline]
120149
fn neg(&self) -> Cmplx<T> {
121150
Cmplx::new(-self.re, -self.im)
122151
}
123152
}
124153

125154
/* constants */
126-
impl<T: Copy + Num> Zero for Cmplx<T> {
155+
impl<T: Clone + Num> Zero for Cmplx<T> {
127156
#[inline]
128157
fn zero() -> Cmplx<T> {
129158
Cmplx::new(Zero::zero(), Zero::zero())
130159
}
131160

132161
#[inline]
133162
fn is_zero(&self) -> bool {
134-
*self == Zero::zero()
163+
self.re.is_zero() && self.im.is_zero()
135164
}
136165
}
137166

138-
impl<T: Copy + Num> One for Cmplx<T> {
167+
impl<T: Clone + Num> One for Cmplx<T> {
139168
#[inline]
140169
fn one() -> Cmplx<T> {
141170
Cmplx::new(One::one(), Zero::zero())
@@ -166,7 +195,7 @@ impl<T: ToStrRadix + Num + Ord> ToStrRadix for Cmplx<T> {
166195
#[cfg(test)]
167196
mod test {
168197
use super::*;
169-
use core::num::{Zero,One};
198+
use core::num::{Zero,One,Real};
170199

171200
pub static _0_0i : Complex = Cmplx { re: 0f, im: 0f };
172201
pub static _1_0i : Complex = Cmplx { re: 1f, im: 0f };
@@ -193,9 +222,10 @@ mod test {
193222
}
194223

195224
#[test]
196-
fn test_norm_sqr() {
225+
fn test_norm() {
197226
fn test(c: Complex, ns: float) {
198227
assert_eq!(c.norm_sqr(), ns);
228+
assert_eq!(c.norm(), ns.sqrt())
199229
}
200230
test(_0_0i, 0f);
201231
test(_1_0i, 1f);
@@ -235,6 +265,25 @@ mod test {
235265
_0_0i.inv();
236266
}
237267

268+
#[test]
269+
fn test_arg() {
270+
fn test(c: Complex, arg: float) {
271+
assert!(c.arg().approx_eq(&arg))
272+
}
273+
test(_1_0i, 0f);
274+
test(_1_1i, 0.25f * Real::pi());
275+
test(_neg1_1i, 0.75f * Real::pi());
276+
test(_05_05i, 0.25f * Real::pi());
277+
}
278+
279+
#[test]
280+
fn test_polar_conv() {
281+
fn test(c: Complex) {
282+
let (r, theta) = c.to_polar();
283+
assert!((c - Cmplx::from_polar(&r, &theta)).norm() < 1e-6);
284+
}
285+
for all_consts.each |&c| { test(c); }
286+
}
238287

239288
mod arith {
240289
use super::*;

src/libstd/num/f32.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ impl Fractional for f32 {
391391

392392
impl Algebraic for f32 {
393393
#[inline(always)]
394-
fn pow(&self, n: f32) -> f32 { pow(*self, n) }
394+
fn pow(&self, n: &f32) -> f32 { pow(*self, *n) }
395395

396396
#[inline(always)]
397397
fn sqrt(&self) -> f32 { sqrt(*self) }
@@ -403,7 +403,7 @@ impl Algebraic for f32 {
403403
fn cbrt(&self) -> f32 { cbrt(*self) }
404404

405405
#[inline(always)]
406-
fn hypot(&self, other: f32) -> f32 { hypot(*self, other) }
406+
fn hypot(&self, other: &f32) -> f32 { hypot(*self, *other) }
407407
}
408408

409409
impl Trigonometric for f32 {
@@ -426,7 +426,7 @@ impl Trigonometric for f32 {
426426
fn atan(&self) -> f32 { atan(*self) }
427427

428428
#[inline(always)]
429-
fn atan2(&self, other: f32) -> f32 { atan2(*self, other) }
429+
fn atan2(&self, other: &f32) -> f32 { atan2(*self, *other) }
430430

431431
/// Simultaneously computes the sine and cosine of the number
432432
#[inline(always)]
@@ -450,7 +450,7 @@ impl Exponential for f32 {
450450

451451
/// Returns the logarithm of the number with respect to an arbitrary base
452452
#[inline(always)]
453-
fn log(&self, base: f32) -> f32 { self.ln() / base.ln() }
453+
fn log(&self, base: &f32) -> f32 { self.ln() / base.ln() }
454454

455455
/// Returns the base 2 logarithm of the number
456456
#[inline(always)]

src/libstd/num/f64.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ impl Fractional for f64 {
403403

404404
impl Algebraic for f64 {
405405
#[inline(always)]
406-
fn pow(&self, n: f64) -> f64 { pow(*self, n) }
406+
fn pow(&self, n: &f64) -> f64 { pow(*self, *n) }
407407

408408
#[inline(always)]
409409
fn sqrt(&self) -> f64 { sqrt(*self) }
@@ -415,7 +415,7 @@ impl Algebraic for f64 {
415415
fn cbrt(&self) -> f64 { cbrt(*self) }
416416

417417
#[inline(always)]
418-
fn hypot(&self, other: f64) -> f64 { hypot(*self, other) }
418+
fn hypot(&self, other: &f64) -> f64 { hypot(*self, *other) }
419419
}
420420

421421
impl Trigonometric for f64 {
@@ -438,7 +438,7 @@ impl Trigonometric for f64 {
438438
fn atan(&self) -> f64 { atan(*self) }
439439

440440
#[inline(always)]
441-
fn atan2(&self, other: f64) -> f64 { atan2(*self, other) }
441+
fn atan2(&self, other: &f64) -> f64 { atan2(*self, *other) }
442442

443443
/// Simultaneously computes the sine and cosine of the number
444444
#[inline(always)]
@@ -462,7 +462,7 @@ impl Exponential for f64 {
462462

463463
/// Returns the logarithm of the number with respect to an arbitrary base
464464
#[inline(always)]
465-
fn log(&self, base: f64) -> f64 { self.ln() / base.ln() }
465+
fn log(&self, base: &f64) -> f64 { self.ln() / base.ln() }
466466

467467
/// Returns the base 2 logarithm of the number
468468
#[inline(always)]

src/libstd/num/float.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,8 @@ impl Fractional for float {
475475

476476
impl Algebraic for float {
477477
#[inline(always)]
478-
fn pow(&self, n: float) -> float {
479-
(*self as f64).pow(n as f64) as float
478+
fn pow(&self, n: &float) -> float {
479+
(*self as f64).pow(&(*n as f64)) as float
480480
}
481481

482482
#[inline(always)]
@@ -495,8 +495,8 @@ impl Algebraic for float {
495495
}
496496

497497
#[inline(always)]
498-
fn hypot(&self, other: float) -> float {
499-
(*self as f64).hypot(other as f64) as float
498+
fn hypot(&self, other: &float) -> float {
499+
(*self as f64).hypot(&(*other as f64)) as float
500500
}
501501
}
502502

@@ -532,8 +532,8 @@ impl Trigonometric for float {
532532
}
533533

534534
#[inline(always)]
535-
fn atan2(&self, other: float) -> float {
536-
(*self as f64).atan2(other as f64) as float
535+
fn atan2(&self, other: &float) -> float {
536+
(*self as f64).atan2(&(*other as f64)) as float
537537
}
538538

539539
/// Simultaneously computes the sine and cosine of the number
@@ -566,8 +566,8 @@ impl Exponential for float {
566566

567567
/// Returns the logarithm of the number with respect to an arbitrary base
568568
#[inline(always)]
569-
fn log(&self, base: float) -> float {
570-
(*self as f64).log(base as f64) as float
569+
fn log(&self, base: &float) -> float {
570+
(*self as f64).log(&(*base as f64)) as float
571571
}
572572

573573
/// Returns the base 2 logarithm of the number

src/libstd/num/num.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ pub trait Fractional: Num
106106
}
107107

108108
pub trait Algebraic {
109-
fn pow(&self, n: Self) -> Self;
109+
fn pow(&self, n: &Self) -> Self;
110110
fn sqrt(&self) -> Self;
111111
fn rsqrt(&self) -> Self;
112112
fn cbrt(&self) -> Self;
113-
fn hypot(&self, other: Self) -> Self;
113+
fn hypot(&self, other: &Self) -> Self;
114114
}
115115

116116
pub trait Trigonometric {
@@ -120,15 +120,15 @@ pub trait Trigonometric {
120120
fn asin(&self) -> Self;
121121
fn acos(&self) -> Self;
122122
fn atan(&self) -> Self;
123-
fn atan2(&self, other: Self) -> Self;
123+
fn atan2(&self, other: &Self) -> Self;
124124
fn sin_cos(&self) -> (Self, Self);
125125
}
126126

127127
pub trait Exponential {
128128
fn exp(&self) -> Self;
129129
fn exp2(&self) -> Self;
130130
fn ln(&self) -> Self;
131-
fn log(&self, base: Self) -> Self;
131+
fn log(&self, base: &Self) -> Self;
132132
fn log2(&self) -> Self;
133133
fn log10(&self) -> Self;
134134
}

0 commit comments

Comments
 (0)