-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.rs
330 lines (330 loc) · 9.91 KB
/
complex.rs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
mod complex {
use std::ops::{Neg, Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign};
use std::fmt;
#[derive(Copy, Clone, Default)]
pub struct Complex {
pub real: f64,
pub imag: f64,
}
#[allow(dead_code)]
impl Complex {
pub const REAL_UNIT: Self = Self { real: 1.0_f64, imag: 0.0_f64 };
pub const IMAG_UNIT: Self = Self { real: 0.0_f64, imag: 1.0_f64 };
#[inline(always)]
pub fn new(real: f64, imag: f64) -> Self {
Self { real, imag }
}
#[inline(always)]
pub fn with_real(real: f64) -> Self {
Self::new(real, 0.0_f64)
}
#[inline(always)]
pub fn with_imag(imag: f64) -> Self {
Self::new(0.0_f64, imag)
}
#[inline(always)]
pub fn abs(self) -> f64 {
self.real.hypot(self.imag)
}
#[inline(always)]
pub fn arg(self) -> f64 {
self.imag.atan2(self.real)
}
#[inline(always)]
pub fn norm(self) -> f64 {
self.real * self.real + self.imag * self.imag
}
#[inline(always)]
pub fn conj(self) -> Self {
Self::new(self.real, -self.imag)
}
#[inline(always)]
pub fn exp(self) -> Self {
let exp_real: f64 = self.real.exp();
Self::new(
exp_real * self.imag.cos(),
exp_real * self.imag.sin(),
)
}
#[inline(always)]
pub fn ln(self) -> Self {
Self::new(self.abs().ln(), self.arg())
}
#[inline(always)]
pub fn log2(self) -> Self {
Self::new(self.abs().log2(), self.arg())
}
#[inline(always)]
pub fn log10(self) -> Self {
Self::new(self.abs().log10(), self.arg())
}
#[inline(always)]
pub fn log(self, base: f64) -> Self {
Self::new(self.abs().log(base), self.arg())
}
#[inline(always)]
pub fn sqrt(self) -> Self {
let abs_sqrt: f64 = self.abs().sqrt();
let arg_half: f64 = self.arg() * 0.5_f64;
Self::new(
abs_sqrt * arg_half.cos(),
abs_sqrt * arg_half.sin(),
)
}
#[inline(always)]
pub fn powi(self, exp: i32) -> Self {
let abs_pow: f64 = self.abs().powi(exp);
let arg: f64 = self.arg();
Self::new(
abs_pow * (exp as f64 * arg).cos(),
abs_pow * (exp as f64 * arg).sin(),
)
}
#[inline(always)]
pub fn powf(self, exp: f64) -> Self {
let abs_pow: f64 = self.abs().powf(exp);
let arg: f64 = self.arg();
Self::new(
abs_pow * (exp * arg).cos(),
abs_pow * (exp * arg).sin(),
)
}
#[inline(always)]
pub fn pow(self, exp: Self) -> Self {
let ln: Self = self.ln();
Self::new(
(exp.real * ln.real - exp.imag * ln.imag).exp(),
exp.real * ln.imag + exp.imag * ln.real,
)
}
#[inline(always)]
pub fn sin(self) -> Self {
Self::new(
self.real.sin() * self.imag.cosh(),
self.real.cos() * self.imag.sinh(),
)
}
#[inline(always)]
pub fn cos(self) -> Self {
Self::new(
self.real.cos() * self.imag.cosh(),
-(self.real.sin() * self.imag.sinh()),
)
}
#[inline(always)]
pub fn tan(&self) -> Self {
self.sin() / self.cos()
}
#[inline(always)]
pub fn sinh(self) -> Self {
Self::new(
self.real.sinh() * self.imag.cos(),
self.real.cosh() * self.imag.sin(),
)
}
#[inline(always)]
pub fn cosh(self) -> Self {
Self::new(
self.real.cosh() * self.imag.cos(),
self.real.sinh() * self.imag.sin(),
)
}
#[inline(always)]
pub fn tanh(self) -> Self {
self.sinh() / self.cosh()
}
#[inline(always)]
pub fn asin(self) -> Self {
-Self::IMAG_UNIT * (Self::IMAG_UNIT * self + (-(self * self) + 1.0_f64).sqrt()).ln()
}
#[inline(always)]
pub fn acos(self) -> Self {
-Self::IMAG_UNIT * (self + Self::IMAG_UNIT * (-(self * self) + 1.0_f64).sqrt()).ln()
}
#[inline(always)]
pub fn atan(self) -> Self {
Self::IMAG_UNIT * Self::with_real(0.5_f64) * (
(-(Self::IMAG_UNIT * self) + 1.0_f64) / (Self::IMAG_UNIT * self + 1.0_f64)
).ln()
}
#[inline(always)]
pub fn asinh(self) -> Self {
(self + (self * self + 1.0_f64).sqrt()).ln()
}
#[inline(always)]
pub fn acosh(self) -> Self {
(self + (self * self - 1.0_f64).sqrt()).ln()
}
#[inline(always)]
pub fn atanh(self) -> Self {
Self::with_real(0.5_f64) * ((self + 1.0_f64) / (-self + 1.0_f64)).ln()
}
}
impl Neg for Complex {
type Output = Self;
#[inline(always)]
fn neg(self) -> Self::Output {
Self::new(
-self.real,
-self.imag,
)
}
}
impl Add for Complex {
type Output = Self;
#[inline(always)]
fn add(self, other: Self) -> Self::Output {
Self::new(
self.real + other.real,
self.imag + other.imag,
)
}
}
impl AddAssign for Complex {
#[inline(always)]
fn add_assign(&mut self, other: Self) -> () {
self.real += other.real;
self.imag += other.imag;
}
}
impl Add<f64> for Complex {
type Output = Self;
#[inline(always)]
fn add(self, rhs: f64) -> Self::Output {
Self::new(
self.real + rhs,
self.imag,
)
}
}
impl AddAssign<f64> for Complex {
#[inline(always)]
fn add_assign(&mut self, rhs: f64) -> () {
self.real += rhs;
}
}
impl Sub for Complex {
type Output = Self;
#[inline(always)]
fn sub(self, other: Self) -> Self::Output {
Self::new(
self.real - other.real,
self.imag - other.imag,
)
}
}
impl SubAssign for Complex {
#[inline(always)]
fn sub_assign(&mut self, other: Self) -> () {
self.real -= other.real;
self.imag -= other.imag;
}
}
impl Sub<f64> for Complex {
type Output = Self;
#[inline(always)]
fn sub(self, rhs: f64) -> Self::Output {
Self::new(
self.real - rhs,
self.imag,
)
}
}
impl SubAssign<f64> for Complex {
#[inline(always)]
fn sub_assign(&mut self, rhs: f64) -> () {
self.real -= rhs;
}
}
impl Mul for Complex {
type Output = Self;
#[inline(always)]
fn mul(self, other: Self) -> Self::Output {
Self::new(
self.real * other.real - self.imag * other.imag,
self.real * other.imag + self.imag * other.real,
)
}
}
impl MulAssign for Complex {
#[inline(always)]
fn mul_assign(&mut self, other: Self) -> () {
(self.real, self.imag) = (
self.real * other.real - self.imag * other.imag,
self.real * other.imag + self.imag * other.real,
);
}
}
impl Mul<f64> for Complex {
type Output = Self;
#[inline(always)]
fn mul(self, rhs: f64) -> Self::Output {
Self::new(
self.real * rhs,
self.imag * rhs,
)
}
}
impl MulAssign<f64> for Complex {
#[inline(always)]
fn mul_assign(&mut self, rhs: f64) -> () {
self.real *= rhs;
self.imag *= rhs;
}
}
impl Div for Complex {
type Output = Self;
#[inline(always)]
fn div(self, other: Self) -> Self::Output {
let denom: f64 = other.norm();
Self::new(
(self.real * other.real + self.imag * other.imag) / denom,
(self.imag * other.real - self.real * other.imag) / denom,
)
}
}
impl DivAssign for Complex {
#[inline(always)]
fn div_assign(&mut self, other: Self) -> () {
let denom: f64 = other.norm();
(self.real, self.imag) = (
(self.real * other.real + self.imag * other.imag) / denom,
(self.imag * other.real - self.real * other.imag) / denom,
);
}
}
impl Div<f64> for Complex {
type Output = Self;
#[inline(always)]
fn div(self, rhs: f64) -> Self::Output {
Self::new(
self.real / rhs,
self.imag / rhs,
)
}
}
impl DivAssign<f64> for Complex {
#[inline(always)]
fn div_assign(&mut self, rhs: f64) -> () {
self.real /= rhs;
self.imag /= rhs;
}
}
impl fmt::Display for Complex {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(precision) = f.precision() {
write!(f, "{:.precision$}{:+.precision$}i", self.real, self.imag, precision = precision)
} else {
write!(f, "{}{:+}i", self.real, self.imag)
}
}
}
impl fmt::Debug for Complex {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}{:+}i", self.real, self.imag)
}
}
}
use complex::Complex;