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 package ffx.numerics.math;
39
40 import static org.apache.commons.math3.util.FastMath.atan2;
41 import static org.apache.commons.math3.util.FastMath.cosh;
42 import static org.apache.commons.math3.util.FastMath.hypot;
43 import static org.apache.commons.math3.util.FastMath.sinh;
44
45 import org.apache.commons.math3.util.FastMath;
46
47
48
49
50
51
52
53 public class ComplexNumber {
54
55 private double re;
56 private double im;
57
58
59
60
61 public ComplexNumber() {
62 }
63
64
65
66
67
68
69
70 public ComplexNumber(double real, double imag) {
71 re = real;
72 im = imag;
73 }
74
75
76
77
78
79
80
81
82 public static ComplexNumber phaseShift(ComplexNumber a, double s) {
83 ComplexNumber sc = new ComplexNumber(FastMath.cos(s), FastMath.sin(s));
84 return a.times(sc);
85 }
86
87
88
89
90
91
92 public double abs() {
93 return hypot(re, im);
94 }
95
96
97
98
99
100
101 public ComplexNumber conjugate() {
102 return new ComplexNumber(re, -im);
103 }
104
105
106
107
108 public void conjugateIP() {
109 this.im = -this.im;
110 }
111
112
113
114
115
116
117 public void copy(ComplexNumber b) {
118 ComplexNumber a = this;
119 a.re = b.re;
120 a.im = b.im;
121 }
122
123
124
125
126
127
128 public ComplexNumber cos() {
129 return new ComplexNumber(FastMath.cos(re) * cosh(im), -FastMath.sin(re) * sinh(im));
130 }
131
132
133
134
135
136
137
138 public ComplexNumber divides(ComplexNumber b) {
139 ComplexNumber a = this;
140 return a.times(b.reciprocal());
141 }
142
143
144
145
146
147
148 public ComplexNumber exp() {
149 return new ComplexNumber(
150 FastMath.exp(re) * FastMath.cos(im), FastMath.exp(re) * FastMath.sin(im));
151 }
152
153
154
155
156
157
158 public double im() {
159 return im;
160 }
161
162
163
164
165
166
167 public void im(double im) {
168 this.im = im;
169 }
170
171
172
173
174
175
176
177 public ComplexNumber minus(ComplexNumber b) {
178 ComplexNumber a = this;
179 var real = a.re - b.re;
180 var imag = a.im - b.im;
181 return new ComplexNumber(real, imag);
182 }
183
184
185
186
187
188
189 public void minusIP(ComplexNumber b) {
190 ComplexNumber a = this;
191 a.re -= b.re;
192 a.im -= b.im;
193 }
194
195
196
197
198
199
200 public double phase() {
201 return atan2(im, re);
202 }
203
204
205
206
207
208
209
210 public ComplexNumber phaseShift(double s) {
211 ComplexNumber sc = new ComplexNumber(FastMath.cos(s), FastMath.sin(s));
212 return this.times(sc);
213 }
214
215
216
217
218
219
220 public void phaseShiftIP(double s) {
221 ComplexNumber a = this;
222 var sr = FastMath.cos(s);
223 var si = FastMath.sin(s);
224 var real = a.re * sr - a.im * si;
225 var imag = a.re * si + a.im * sr;
226 a.re = real;
227 a.im = imag;
228 }
229
230
231
232
233
234
235
236 public ComplexNumber plus(ComplexNumber b) {
237 ComplexNumber a = this;
238 var real = a.re + b.re;
239 var imag = a.im + b.im;
240 return new ComplexNumber(real, imag);
241 }
242
243
244
245
246
247
248 public void plusIP(ComplexNumber b) {
249 ComplexNumber a = this;
250 a.re += b.re;
251 a.im += b.im;
252 }
253
254
255
256
257
258
259 public double re() {
260 return re;
261 }
262
263
264
265
266
267
268 public void re(double re) {
269 this.re = re;
270 }
271
272
273
274
275
276
277 public ComplexNumber reciprocal() {
278 var scale = re * re + im * im;
279 var iScale = 1.0 / scale;
280 return new ComplexNumber(re * iScale, -im * iScale);
281 }
282
283
284
285
286 public void reciprocalIP() {
287 var scale = re * re + im * im;
288 var iScale = 1.0 / scale;
289 re *= iScale;
290 im *= -iScale;
291 }
292
293
294
295
296
297
298 public ComplexNumber sin() {
299 return new ComplexNumber(FastMath.sin(re) * cosh(im), FastMath.cos(re) * sinh(im));
300 }
301
302
303
304
305
306
307 public ComplexNumber tan() {
308 return sin().divides(cos());
309 }
310
311
312
313
314
315
316
317 public ComplexNumber times(ComplexNumber b) {
318 ComplexNumber a = this;
319 var real = a.re * b.re - a.im * b.im;
320 var imag = a.re * b.im + a.im * b.re;
321 return new ComplexNumber(real, imag);
322 }
323
324
325
326
327
328
329
330 public ComplexNumber times(double alpha) {
331 return new ComplexNumber(alpha * re, alpha * im);
332 }
333
334
335
336
337
338
339 public void timesIP(ComplexNumber b) {
340 ComplexNumber a = this;
341 var real = a.re * b.re - a.im * b.im;
342 var imag = a.re * b.im + a.im * b.re;
343 a.re = real;
344 a.im = imag;
345 }
346
347
348
349
350
351
352 public void timesIP(double alpha) {
353 ComplexNumber a = this;
354 a.re *= alpha;
355 a.im *= alpha;
356 }
357
358
359
360
361 @Override
362 public String toString() {
363 if (im == 0) {
364 return re + "";
365 }
366 if (re == 0) {
367 return im + "i";
368 }
369 if (im < 0) {
370 return re + " - " + (-im) + "i";
371 }
372 return re + " + " + im + "i";
373 }
374 }