1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::f64::consts::PI;
use std::fmt;
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Mul;
use std::ops::MulAssign;
use std::ops::Neg;
#[derive(Clone, Copy, PartialEq)]
pub struct Complex {
re: f64,
im: f64,
}
impl Complex {
pub fn new(re: f64, im: f64) -> Complex {
Complex { re: re, im: im }
}
pub fn new_euler(r: f64, phi: f64) -> Complex {
Complex {
re: r * phi.cos(),
im: r * phi.sin(),
}
}
pub fn nth_root_of_unity(n: u32) -> Complex {
if 0 == n {
Complex::one()
} else {
let angle = (2f64 * PI) / (n as f64);
Complex::new_euler(1f64, angle)
}
}
pub fn zero() -> Complex {
Complex::new(0f64, 0f64)
}
pub fn one() -> Complex {
Complex::new(1f64, 0f64)
}
pub fn i() -> Complex {
Complex::new(0f64, 1f64)
}
pub fn norm_sqr(&self) -> f64 {
self.re * self.re + self.im * self.im
}
pub fn pow(&self, n: u32) -> Complex {
let optimization = 5;
if 0 == n {
Complex::one()
} else if n < optimization {
let mut x = Complex::one();
for _ in 0..n {
x *= *self;
}
x
} else {
let (l, r) = if n.is_power_of_two() {
(n.trailing_zeros(), 0)
} else {
let p = n.checked_next_power_of_two().unwrap().trailing_zeros() - 1;
(p, n - 2u32.pow(p))
};
let mut x = *self;
for _ in 0..l {
x *= x;
}
self.pow(r) * x
}
}
pub fn re(&self) -> f64 {
self.re
}
pub fn im(&self) -> f64 {
self.im
}
pub fn approx_eq(&self, other: &Complex) -> bool {
let threshold = 0.000000000001;
let d1 = (self.re() - other.re()).abs();
let d2 = (self.im() - other.im()).abs();
d1 < threshold && d2 < threshold
}
}
impl Add<Complex> for Complex {
type Output = Complex;
fn add(self, rhs: Complex) -> Complex {
Complex::new(self.re + rhs.re, self.im + rhs.im)
}
}
impl Mul<Complex> for Complex {
type Output = Complex;
fn mul(self, rhs: Complex) -> Complex {
Complex::new(self.re * rhs.re - self.im * rhs.im,
self.re * rhs.im + self.im * rhs.re)
}
}
impl AddAssign for Complex {
fn add_assign(&mut self, rhs: Complex) {
*self = *self + rhs;
}
}
impl MulAssign for Complex {
fn mul_assign(&mut self, rhs: Complex) {
*self = *self * rhs;
}
}
impl Neg for Complex {
type Output = Complex;
fn neg(self) -> Complex {
c![-self.re, -self.im]
}
}
impl fmt::Debug for Complex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:+.3} + {:+.3}i", self.re, self.im)
}
}
#[test]
fn complex_test() {
assert_eq!(c![4f64, 6f64], c![1f64, 2f64] + c![3f64, 4f64]);
assert_eq!(c![-5f64, 10f64], c![1f64, 2f64] * c![3f64, 4f64]);
assert_eq!(5f64, c![1f64, 2f64].norm_sqr());
let mut z = c![1f64, 2f64];
z += c![3f64, 4f64];
assert_eq!(z, c![4f64, 6f64]);
let x = Complex::nth_root_of_unity(15);
assert!(Complex::one().approx_eq(&x.pow(15)));
assert_eq!(Complex::one(), c![7f64, 8f64].pow(0));
}