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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
package ch.obermuhlner.math.big;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Objects;
/**
* Represents a complex number consisting of a real and an imaginary {@link BigDecimal} part in the form {@code a + bi}.
*
* <p>It generally follows the design of {@link BigDecimal} with some convenience improvements like overloaded operator methods.</p>
*
* <p>The biggest difference to {@link BigDecimal} is that {@link BigComplex#equals(Object) BigComplex.equals(Object)} implements the <strong>mathematical</strong> equality
* and <strong>not</strong> the strict technical equality.
* This was a difficult decision because it means that {@code BigComplex} behaves slightly different than {@link BigDecimal}
* but considering that the strange equality of {@link BigDecimal} is a major source of bugs we
* decided it was worth the slight inconsistency.
* If you need the strict equality use {@link BigComplex#strictEquals(Object)}`.</p>
*
* <p>This class is immutable and therefore inherently thread safe.</p>
*/
public final class BigComplex {
/**
* Zero represented as complex number.
*/
public static final BigComplex ZERO = new BigComplex(BigDecimal.ZERO, BigDecimal.ZERO);
/**
* Real 1 represented as complex number.
*/
public static final BigComplex ONE = new BigComplex(BigDecimal.ONE, BigDecimal.ZERO);
/**
* Imaginary 1 represented as complex number.
*/
public static final BigComplex I = new BigComplex(BigDecimal.ZERO, BigDecimal.ONE);
/**
* The real {@link BigDecimal} part of this complex number.
*/
public final BigDecimal re;
/**
* The imaginary {@link BigDecimal} part of this complex number.
*/
public final BigDecimal im;
private BigComplex(BigDecimal re, BigDecimal im) {
this.re = re;
this.im = im;
}
/**
* Calculates the addition of the given complex value to this complex number.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the {@link BigComplex} value to add
* @return the calculated {@link BigComplex} result
*/
public BigComplex add(BigComplex value) {
return valueOf(
re.add(value.re),
im.add(value.im));
}
/**
* Calculates the addition of the given complex value to this complex number using the specified {@link MathContext}.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the {@link BigComplex} value to add
* @param mathContext the {@link MathContext} used to calculate the result
* @return the calculated {@link BigComplex} result
*/
public BigComplex add(BigComplex value, MathContext mathContext) {
return valueOf(
re.add(value.re, mathContext),
im.add(value.im, mathContext));
}
/**
* Calculates the addition of the given real {@link BigDecimal} value to this complex number using the specified {@link MathContext}.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the real {@link BigDecimal} value to add
* @param mathContext the {@link MathContext} used to calculate the result
* @return the calculated {@link BigComplex} result
*/
public BigComplex add(BigDecimal value, MathContext mathContext) {
return valueOf(
re.add(value, mathContext),
im);
}
/**
* Calculates the addition of the given real {@link BigDecimal} value to this complex number.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the real {@link BigDecimal} value to add
* @return the calculated {@link BigComplex} result
*/
public BigComplex add(BigDecimal value) {
return valueOf(
re.add(value),
im);
}
/**
* Calculates the addition of the given real {@code double} value to this complex number.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the real {@code double} value to add
* @return the calculated {@link BigComplex} result
*/
public BigComplex add(double value) {
return add(BigDecimal.valueOf(value));
}
/**
* Calculates the subtraction of the given complex value from this complex number.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the {@link BigComplex} value to subtract
* @return the calculated {@link BigComplex} result
*/
public BigComplex subtract(BigComplex value) {
return valueOf(
re.subtract(value.re),
im.subtract(value.im));
}
/**
* Calculates the subtraction of the given complex value from this complex number using the specified {@link MathContext}.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the {@link BigComplex} value to subtract
* @param mathContext the {@link MathContext} used to calculate the result
* @return the calculated {@link BigComplex} result
*/
public BigComplex subtract(BigComplex value, MathContext mathContext) {
return valueOf(
re.subtract(value.re, mathContext),
im.subtract(value.im, mathContext));
}
/**
* Calculates the subtraction of the given real {@link BigDecimal} value from this complex number using the specified {@link MathContext}.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the real {@link BigDecimal} value to add
* @param mathContext the {@link MathContext} used to calculate the result
* @return the calculated {@link BigComplex} result
*/
public BigComplex subtract(BigDecimal value, MathContext mathContext) {
return valueOf(
re.subtract(value, mathContext),
im);
}
/**
* Calculates the subtraction of the given real {@link BigDecimal} value from this complex number.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the real {@link BigDecimal} value to subtract
* @return the calculated {@link BigComplex} result
*/
public BigComplex subtract(BigDecimal value) {
return valueOf(
re.subtract(value),
im);
}
/**
* Calculates the subtraction of the given real {@code double} value from this complex number.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the real {@code double} value to subtract
* @return the calculated {@link BigComplex} result
*/
public BigComplex subtract(double value) {
return subtract(BigDecimal.valueOf(value));
}
/**
* Calculates the multiplication of the given complex value to this complex number.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the {@link BigComplex} value to multiply
* @return the calculated {@link BigComplex} result
*/
public BigComplex multiply(BigComplex value) {
return valueOf(
re.multiply(value.re).subtract(im.multiply(value.im)),
re.multiply(value.im).add(im.multiply(value.re)));
}
/**
* Calculates the multiplication of the given complex value with this complex number using the specified {@link MathContext}.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the {@link BigComplex} value to multiply
* @param mathContext the {@link MathContext} used to calculate the result
* @return the calculated {@link BigComplex} result
*/
public BigComplex multiply(BigComplex value, MathContext mathContext) {
return valueOf(
re.multiply(value.re, mathContext).subtract(im.multiply(value.im, mathContext), mathContext),
re.multiply(value.im, mathContext).add(im.multiply(value.re, mathContext), mathContext));
}
/**
* Calculates the multiplication of the given real {@link BigDecimal} value with this complex number using the specified {@link MathContext}.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the real {@link BigDecimal} value to multiply
* @param mathContext the {@link MathContext} used to calculate the result
* @return the calculated {@link BigComplex} result
*/
public BigComplex multiply(BigDecimal value, MathContext mathContext) {
return valueOf(
re.multiply(value, mathContext),
im.multiply(value, mathContext));
}
/**
* Calculates the multiplication of the given real {@link BigDecimal} value with this complex number.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the real {@link BigDecimal} value to multiply
* @return the calculated {@link BigComplex} result
*/
public BigComplex multiply(BigDecimal value) {
return valueOf(
re.multiply(value),
im.multiply(value));
}
/**
* Calculates the multiplication of the given real {@code double} value with this complex number.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the real {@code double} value to multiply
* @return the calculated {@link BigComplex} result
*/
public BigComplex multiply(double value) {
return multiply(BigDecimal.valueOf(value));
}
/**
* Calculates this complex number divided by the given complex value using the specified {@link MathContext}.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the {@link BigComplex} value to divide by
* @param mathContext the {@link MathContext} used to calculate the result
* @return the calculated {@link BigComplex} result
*/
public BigComplex divide(BigComplex value, MathContext mathContext) {
return multiply(value.reciprocal(mathContext), mathContext);
}
/**
* Calculates this complex number divided by the given real {@link BigDecimal} value using the specified {@link MathContext}.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the {@link BigDecimal} value to divide by
* @param mathContext the {@link MathContext} used to calculate the result
* @return the calculated {@link BigComplex} result
*/
public BigComplex divide(BigDecimal value, MathContext mathContext) {
return valueOf(
re.divide(value, mathContext),
im.divide(value, mathContext));
}
/**
* Calculates this complex number divided by the given real {@code double} value using the specified {@link MathContext}.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param value the {@code double} value to divide by
* @param mathContext the {@link MathContext} used to calculate the result
* @return the calculated {@link BigComplex} result
*/
public BigComplex divide(double value, MathContext mathContext) {
return divide(BigDecimal.valueOf(value), mathContext);
}
/**
* Calculates the reciprocal of this complex number using the specified {@link MathContext}.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param mathContext the {@link MathContext} used to calculate the result
* @return the calculated {@link BigComplex} result
*/
public BigComplex reciprocal(MathContext mathContext) {
BigDecimal scale = absSquare(mathContext);
return valueOf(
re.divide(scale, mathContext),
im.negate().divide(scale, mathContext));
}
/**
* Calculates the conjugate {@code a - bi} of this complex number.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @return the calculated {@link BigComplex} result
*/
public BigComplex conjugate() {
return valueOf(re, im.negate());
}
/**
* Calculates the negation {@code -a - bi} of this complex number.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @return the calculated {@link BigComplex} result
*/
public BigComplex negate() {
return valueOf(re.negate(), im.negate());
}
/**
* Calculates the absolute value (also known as magnitude, length or radius) of this complex number.
*
* <p>This method is slower than {@link #absSquare(MathContext)} since it needs to calculate the {@link BigDecimalMath#sqrt(BigDecimal, MathContext)}.</p>
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param mathContext the {@link MathContext} used to calculate the result
* @return the calculated {@link BigComplex} result
* @see #absSquare(MathContext)
*/
public BigDecimal abs(MathContext mathContext) {
return BigDecimalMath.sqrt(absSquare(mathContext), mathContext);
}
/**
* Calculates the angle in radians (also known as argument) of this complex number.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param mathContext the {@link MathContext} used to calculate the result
* @return the calculated {@link BigComplex} result
*/
public BigDecimal angle(MathContext mathContext) {
return BigDecimalMath.atan2(im, re, mathContext);
}
/**
* Calculates the square of the absolute value of this complex number.
*
* <p>This method is faster than {@link #abs(MathContext)} since it does not need to calculate the {@link BigDecimalMath#sqrt(BigDecimal, MathContext)}.</p>
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param mathContext the {@link MathContext} used to calculate the result
* @return the calculated {@link BigComplex} result
* @see #abs(MathContext)
*/
public BigDecimal absSquare(MathContext mathContext) {
return re.multiply(re, mathContext).add(im.multiply(im, mathContext), mathContext);
}
/**
* Returns whether this complex number only has a real part (the imaginary part is 0).
*
* @return {@code true} if this complex number only has a real part, {@code false} if the imaginary part is not 0
*/
public boolean isReal() {
return im.signum() == 0;
}
/**
* Returns the real part of this complex number as {@link BigComplex} number.
*
* @return the real part as as {@link BigComplex} number
*/
public BigComplex re() {
return valueOf(re, BigDecimal.ZERO);
}
/**
* Returns the imaginary part of this complex number as {@link BigComplex} number.
*
* @return the imaginary part as as {@link BigComplex} number
*/
public BigComplex im() {
return valueOf(BigDecimal.ZERO, im);
}
/**
* Returns this complex nuber rounded to the specified precision.
*
* <p>This methods <strong>does not</strong> modify this instance.</p>
*
* @param mathContext the {@link MathContext} used to calculate the result
* @return the rounded {@link BigComplex} result
*/
public BigComplex round(MathContext mathContext) {
return valueOf(re.round(mathContext), im.round(mathContext));
}
@Override
public int hashCode() {
return Objects.hash(re, im);
}
/**
* {@inheritDoc}
*
* <p>Contrary to {@link BigDecimal#equals(Object)} this method implements <strong>mathematical</strong> equality
* (by calling {@link BigDecimal#compareTo(BigDecimal)} on the real and imaginary parts)
* instead of strict equality.</p>
*
* @see #strictEquals(Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BigComplex other = (BigComplex) obj;
return re.compareTo(other.re) == 0 && im.compareTo(other.im) == 0;
}
/**
* Returns whether the real and imaginary parts of this complex number are strictly equal.
*
* <p>This method uses the strict equality as defined by {@link BigDecimal#equals(Object)} on the real and imaginary parts.</p>
* <p>Please note that {@link #equals(Object) BigComplex.equals(Object)} implements <strong>mathematical</strong> equality instead
* (by calling {@link BigDecimal#compareTo(BigDecimal) on the real and imaginary parts}).</p>
*
* @param obj the object to compare for strict equality
* @return {@code true} if the specified object is strictly equal to this complex number
* @see #equals(Object)
*/
public boolean strictEquals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BigComplex other = (BigComplex) obj;
return re.equals(other.re) && im.equals(other.im);
}
@Override
public String toString() {
if (im.signum() >= 0) {
return "(" + re + " + " + im + " i)";
} else {
return "(" + re + " - " + im.negate() + " i)";
}
}
/**
* Returns a complex number with the specified real {@link BigDecimal} part.
*
* @param real the real {@link BigDecimal} part
* @return the complex number
*/
public static BigComplex valueOf(BigDecimal real) {
return valueOf(real, BigDecimal.ZERO);
}
/**
* Returns a complex number with the specified real {@code double} part.
*
* @param real the real {@code double} part
* @return the complex number
*/
public static BigComplex valueOf(double real) {
return valueOf(BigDecimal.valueOf(real), BigDecimal.ZERO);
}
/**
* Returns a complex number with the specified real and imaginary {@code double} parts.
*
* @param real the real {@code double} part
* @param imaginary the imaginary {@code double} part
* @return the complex number
*/
public static BigComplex valueOf(double real, double imaginary) {
return valueOf(BigDecimal.valueOf(real), BigDecimal.valueOf(imaginary));
}
/**
* Returns a complex number with the specified real and imaginary {@link BigDecimal} parts.
*
* @param real the real {@link BigDecimal} part
* @param imaginary the imaginary {@link BigDecimal} part
* @return the complex number
*/
public static BigComplex valueOf(BigDecimal real, BigDecimal imaginary) {
if (real.signum() == 0) {
if (imaginary.signum() == 0) {
return ZERO;
}
if (imaginary.compareTo(BigDecimal.ONE) == 0) {
return I;
}
}
if (imaginary.signum() == 0 && real.compareTo(BigDecimal.ONE) == 0) {
return ONE;
}
return new BigComplex(real, imaginary);
}
/**
* Returns a complex number with the specified polar {@link BigDecimal} radius and angle using the specified {@link MathContext}.
*
* @param radius the {@link BigDecimal} radius of the polar representation
* @param angle the {@link BigDecimal} angle in radians of the polar representation
* @param mathContext the {@link MathContext} used to calculate the result
* @return the complex number
*/
public static BigComplex valueOfPolar(BigDecimal radius, BigDecimal angle, MathContext mathContext) {
if (radius.signum() == 0) {
return ZERO;
}
return valueOf(
radius.multiply(BigDecimalMath.cos(angle, mathContext), mathContext),
radius.multiply(BigDecimalMath.sin(angle, mathContext), mathContext));
}
public static BigComplex valueOfPolar(double radius, double angle, MathContext mathContext) {
return valueOfPolar(BigDecimal.valueOf(radius), BigDecimal.valueOf(angle), mathContext);
}
}
|