1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.complex;
24
25 import org.hipparchus.CalculusFieldElementAbstractTest;
26 import org.hipparchus.UnitTestUtils;
27 import org.hipparchus.exception.LocalizedCoreFormats;
28 import org.hipparchus.exception.MathIllegalArgumentException;
29 import org.hipparchus.exception.NullArgumentException;
30 import org.hipparchus.util.Binary64;
31 import org.hipparchus.util.FastMath;
32 import org.hipparchus.util.MathUtils;
33 import org.hipparchus.util.Precision;
34 import org.junit.jupiter.api.Assertions;
35 import org.junit.jupiter.api.Test;
36
37 import java.util.Arrays;
38 import java.util.List;
39
40 import static org.junit.jupiter.api.Assertions.assertEquals;
41 import static org.junit.jupiter.api.Assertions.assertFalse;
42 import static org.junit.jupiter.api.Assertions.assertNotEquals;
43 import static org.junit.jupiter.api.Assertions.assertSame;
44 import static org.junit.jupiter.api.Assertions.assertThrows;
45 import static org.junit.jupiter.api.Assertions.assertTrue;
46 import static org.junit.jupiter.api.Assertions.fail;
47
48
49
50
51 class ComplexTest extends CalculusFieldElementAbstractTest<Complex> {
52
53
54 private final double inf = Double.POSITIVE_INFINITY;
55 private final double neginf = Double.NEGATIVE_INFINITY;
56 private final double nan = Double.NaN;
57 private final double pi = FastMath.PI;
58 private final Complex oneInf = new Complex(1, inf);
59 private final Complex oneNegInf = new Complex(1, neginf);
60 private final Complex infOne = new Complex(inf, 1);
61 private final Complex infZero = new Complex(inf, 0);
62 private final Complex infNaN = new Complex(inf, nan);
63 private final Complex infNegInf = new Complex(inf, neginf);
64 private final Complex infInf = new Complex(inf, inf);
65 private final Complex negInfInf = new Complex(neginf, inf);
66 private final Complex negInfZero = new Complex(neginf, 0);
67 private final Complex negInfOne = new Complex(neginf, 1);
68 private final Complex negInfNaN = new Complex(neginf, nan);
69 private final Complex negInfNegInf = new Complex(neginf, neginf);
70 private final Complex oneNaN = new Complex(1, nan);
71 private final Complex zeroInf = new Complex(0, inf);
72 private final Complex zeroNaN = new Complex(0, nan);
73 private final Complex nanInf = new Complex(nan, inf);
74 private final Complex nanNegInf = new Complex(nan, neginf);
75 private final Complex nanZero = new Complex(nan, 0);
76
77 @Override
78 protected Complex build(final double x) {
79 return new Complex(x, 0.0);
80 }
81
82 @Test
83 void testConstructor() {
84 Complex z = new Complex(3.0, 4.0);
85 assertEquals(3.0, z.getReal(), 1.0e-5);
86 assertEquals(4.0, z.getImaginary(), 1.0e-5);
87 assertEquals(3.0, z.getRealPart(), 1.0e-5);
88 assertEquals(4.0, z.getImaginaryPart(), 1.0e-5);
89 }
90
91 @Test
92 void testConstructorNaN() {
93 Complex z = new Complex(3.0, Double.NaN);
94 assertTrue(z.isNaN());
95
96 z = new Complex(nan, 4.0);
97 assertTrue(z.isNaN());
98
99 z = new Complex(3.0, 4.0);
100 assertFalse(z.isNaN());
101 }
102
103 @Test
104 void testNorm() {
105 Complex z = new Complex(3.0, 4.0);
106 assertEquals(5.0, z.norm(), 1.0e-5);
107 }
108
109 @Test
110 void testNormNaN() {
111 assertTrue(Double.isNaN(Complex.NaN.norm()));
112 Complex z = new Complex(inf, nan);
113 assertTrue(Double.isNaN(z.norm()));
114 }
115
116 @Test
117 void testNormInfinite() {
118 Complex z = Complex.NaN.newInstance(inf);
119 assertEquals(inf, z.norm(), 0);
120 z = new Complex(0, neginf);
121 assertEquals(inf, z.norm(), 0);
122 z = new Complex(inf, neginf);
123 assertEquals(inf, z.norm(), 0);
124 }
125
126 @Test
127 void testAdd() {
128 Complex x = new Complex(3.0, 4.0);
129 Complex y = new Complex(5.0, 6.0);
130 Complex z = x.add(y);
131 assertEquals(8.0, z.getReal(), 1.0e-5);
132 assertEquals(10.0, z.getImaginary(), 1.0e-5);
133 }
134
135 @Test
136 void testAddNaN() {
137 Complex x = new Complex(3.0, 4.0);
138 Complex z = x.add(Complex.NaN);
139 assertSame(Complex.NaN, z);
140 z = new Complex(1, nan);
141 Complex w = x.add(z);
142 assertSame(Complex.NaN, w);
143 assertSame(Complex.NaN, Complex.NaN.add(Double.NaN));
144 }
145
146 @Test
147 void testAddInf() {
148 Complex x = new Complex(1, 1);
149 Complex z = new Complex(inf, 0);
150 Complex w = x.add(z);
151 assertEquals(1, w.getImaginary(), 0);
152 assertEquals(inf, w.getReal(), 0);
153
154 x = new Complex(neginf, 0);
155 assertTrue(Double.isNaN(x.add(z).getReal()));
156 }
157
158 @Test
159 void testScalarAdd() {
160 Complex x = new Complex(3.0, 4.0);
161 double yDouble = 2.0;
162 Complex yComplex = new Complex(yDouble);
163 assertEquals(x.add(yComplex), x.add(yDouble));
164 }
165
166 @Test
167 void testScalarAddNaN() {
168 Complex x = new Complex(3.0, 4.0);
169 double yDouble = Double.NaN;
170 Complex yComplex = new Complex(yDouble);
171 assertEquals(x.add(yComplex), x.add(yDouble));
172 }
173
174 @Test
175 void testScalarAddInf() {
176 Complex x = new Complex(1, 1);
177 double yDouble = Double.POSITIVE_INFINITY;
178
179 Complex yComplex = new Complex(yDouble);
180 assertEquals(x.add(yComplex), x.add(yDouble));
181
182 x = new Complex(neginf, 0);
183 assertEquals(x.add(yComplex), x.add(yDouble));
184 }
185
186 @Test
187 void testConjugate() {
188 Complex x = new Complex(3.0, 4.0);
189 Complex z = x.conjugate();
190 assertEquals(3.0, z.getReal(), 1.0e-5);
191 assertEquals(-4.0, z.getImaginary(), 1.0e-5);
192 }
193
194 @Test
195 void testConjugateNaN() {
196 Complex z = Complex.NaN.conjugate();
197 assertTrue(z.isNaN());
198 }
199
200 @Test
201 void testConjugateInfiinite() {
202 Complex z = new Complex(0, inf);
203 assertEquals(neginf, z.conjugate().getImaginary(), 0);
204 z = new Complex(0, neginf);
205 assertEquals(inf, z.conjugate().getImaginary(), 0);
206 }
207
208 @Test
209 void testDivide() {
210 Complex x = new Complex(3.0, 4.0);
211 Complex y = new Complex(5.0, 6.0);
212 Complex z = x.divide(y);
213 assertEquals(39.0 / 61.0, z.getReal(), 1.0e-5);
214 assertEquals(2.0 / 61.0, z.getImaginary(), 1.0e-5);
215 }
216
217 @Test
218 void testDivideReal() {
219 Complex x = new Complex(2d, 3d);
220 Complex y = new Complex(2d, 0d);
221 assertEquals(new Complex(1d, 1.5), x.divide(y));
222
223 }
224
225 @Test
226 void testDivideImaginary() {
227 Complex x = new Complex(2d, 3d);
228 Complex y = new Complex(0d, 2d);
229 assertEquals(new Complex(1.5d, -1d), x.divide(y));
230 }
231
232 @Test
233 void testDivideInf() {
234 Complex x = new Complex(3, 4);
235 Complex w = new Complex(neginf, inf);
236 assertEquals(Complex.ZERO, x.divide(w));
237
238 Complex z = w.divide(x);
239 assertTrue(Double.isNaN(z.getReal()));
240 assertEquals(inf, z.getImaginary(), 0);
241
242 w = new Complex(inf, inf);
243 z = w.divide(x);
244 assertTrue(Double.isNaN(z.getImaginary()));
245 assertEquals(inf, z.getReal(), 0);
246
247 w = new Complex(1, inf);
248 z = w.divide(w);
249 assertTrue(Double.isNaN(z.getReal()));
250 assertTrue(Double.isNaN(z.getImaginary()));
251 }
252
253 @Test
254 void testDivideZero() {
255 Complex x = new Complex(3.0, 4.0);
256 Complex z = x.divide(Complex.ZERO);
257
258 assertEquals(Complex.NaN, z);
259 }
260
261 @Test
262 void testDivideZeroZero() {
263 Complex x = new Complex(0.0, 0.0);
264 Complex z = x.divide(Complex.ZERO);
265 assertEquals(Complex.NaN, z);
266 }
267
268 @Test
269 void testDivideNaN() {
270 Complex x = new Complex(3.0, 4.0);
271 Complex z = x.divide(Complex.NaN);
272 assertTrue(z.isNaN());
273 assertTrue(Complex.NaN.divide(Complex.NaN).isNaN());
274 }
275
276 @Test
277 void testDivideNaNInf() {
278 Complex z = oneInf.divide(Complex.ONE);
279 assertTrue(Double.isNaN(z.getReal()));
280 assertEquals(inf, z.getImaginary(), 0);
281
282 z = negInfNegInf.divide(oneNaN);
283 assertTrue(Double.isNaN(z.getReal()));
284 assertTrue(Double.isNaN(z.getImaginary()));
285
286 z = negInfInf.divide(Complex.ONE);
287 assertTrue(Double.isNaN(z.getReal()));
288 assertTrue(Double.isNaN(z.getImaginary()));
289 }
290
291 @Test
292 void testScalarDivide() {
293 Complex x = new Complex(3.0, 4.0);
294 double yDouble = 2.0;
295 Complex yComplex = new Complex(yDouble);
296 assertEquals(x.divide(yComplex), x.divide(yDouble));
297 }
298
299 @Test
300 void testScalarDivideNaN() {
301 Complex x = new Complex(3.0, 4.0);
302 double yDouble = Double.NaN;
303 Complex yComplex = new Complex(yDouble);
304 assertEquals(x.divide(yComplex), x.divide(yDouble));
305 assertTrue(Complex.NaN.divide(Double.NaN).isNaN());
306 }
307
308 @Test
309 void testScalarDivideInf() {
310 Complex x = new Complex(1,1);
311 double yDouble = Double.POSITIVE_INFINITY;
312 Complex yComplex = new Complex(yDouble);
313 UnitTestUtils.customAssertEquals(x.divide(yComplex), x.divide(yDouble), 0);
314
315 yDouble = Double.NEGATIVE_INFINITY;
316 yComplex = new Complex(yDouble);
317 UnitTestUtils.customAssertEquals(x.divide(yComplex), x.divide(yDouble), 0);
318
319 x = new Complex(1, Double.NEGATIVE_INFINITY);
320 UnitTestUtils.customAssertEquals(x.divide(yComplex), x.divide(yDouble), 0);
321 }
322
323 @Test
324 void testScalarDivideZero() {
325 Complex x = new Complex(1,1);
326 UnitTestUtils.customAssertEquals(x.divide(Complex.ZERO), x.divide(0), 0);
327 }
328
329 @Test
330 void testReciprocal() {
331 Complex z = new Complex(5.0, 6.0);
332 Complex act = z.reciprocal();
333 double expRe = 5.0 / 61.0;
334 double expIm = -6.0 / 61.0;
335 assertEquals(expRe, act.getReal(), FastMath.ulp(expRe));
336 assertEquals(expIm, act.getImaginary(), FastMath.ulp(expIm));
337 }
338
339 @Test
340 void testReciprocalReal() {
341 Complex z = new Complex(-2.0, 0.0);
342 assertTrue(Complex.equals(new Complex(-0.5, 0.0), z.reciprocal()));
343 }
344
345 @Test
346 void testReciprocalImaginary() {
347 Complex z = new Complex(0.0, -2.0);
348 assertEquals(new Complex(0.0, 0.5), z.reciprocal());
349 }
350
351 @Test
352 void testReciprocalInf() {
353 Complex z = new Complex(neginf, inf);
354 assertEquals(Complex.ZERO, z.reciprocal());
355
356 z = new Complex(1, inf).reciprocal();
357 assertEquals(Complex.ZERO, z);
358 }
359
360 @Test
361 void testReciprocalZero() {
362 assertEquals(Complex.INF, Complex.ZERO.reciprocal());
363 }
364
365 @Test
366 void testReciprocalNaN() {
367 assertTrue(Complex.NaN.reciprocal().isNaN());
368 }
369
370 @Test
371 void testMultiply() {
372 Complex x = new Complex(3.0, 4.0);
373 Complex y = new Complex(5.0, 6.0);
374 Complex z = x.multiply(y);
375 assertEquals(-9.0, z.getReal(), 1.0e-5);
376 assertEquals(38.0, z.getImaginary(), 1.0e-5);
377 }
378
379 @Test
380 void testMultiplyNaN() {
381 Complex x = new Complex(3.0, 4.0);
382 Complex z = x.multiply(Complex.NaN);
383 assertSame(Complex.NaN, z);
384 z = Complex.NaN.multiply(5);
385 assertSame(Complex.NaN, z);
386 }
387
388 @Test
389 void testMultiplyInfInf() {
390
391 assertTrue(infInf.multiply(infInf).isInfinite());
392 }
393
394 @Test
395 void testMultiplyNaNInf() {
396 Complex z = new Complex(1,1);
397 Complex w = z.multiply(infOne);
398 assertEquals(w.getReal(), inf, 0);
399 assertEquals(w.getImaginary(), inf, 0);
400
401
402 assertEquals(Complex.INF, new Complex( 1, 0).multiply(infInf));
403 assertEquals(Complex.INF, new Complex(-1, 0).multiply(infInf));
404 assertEquals(Complex.INF, new Complex( 1, 0).multiply(negInfZero));
405
406 w = oneInf.multiply(oneNegInf);
407 assertEquals(w.getReal(), inf, 0);
408 assertEquals(w.getImaginary(), inf, 0);
409
410 w = negInfNegInf.multiply(oneNaN);
411 assertTrue(Double.isNaN(w.getReal()));
412 assertTrue(Double.isNaN(w.getImaginary()));
413
414 z = new Complex(1, neginf);
415 assertSame(Complex.INF, z.square());
416 }
417
418 @Test
419 void testScalarMultiply() {
420 Complex x = new Complex(3.0, 4.0);
421 double yDouble = 2.0;
422 Complex yComplex = new Complex(yDouble);
423 assertEquals(x.multiply(yComplex), x.multiply(yDouble));
424 int zInt = -5;
425 Complex zComplex = new Complex(zInt);
426 assertEquals(x.multiply(zComplex), x.multiply(zInt));
427 }
428
429 @Test
430 void testScalarMultiplyNaN() {
431 Complex x = new Complex(3.0, 4.0);
432 double yDouble = Double.NaN;
433 Complex yComplex = new Complex(yDouble);
434 assertEquals(x.multiply(yComplex), x.multiply(yDouble));
435 assertTrue(new Complex(Double.NaN, 0).multiply(5).isNaN());
436 assertTrue(new Complex(0, Double.NaN).multiply(5).isNaN());
437 assertTrue(Complex.NaN.multiply(5).isNaN());
438 assertTrue(new Complex(Double.NaN, 0).multiply(5.0).isNaN());
439 assertTrue(new Complex(0, Double.NaN).multiply(5.0).isNaN());
440 assertTrue(Complex.NaN.multiply(5.0).isNaN());
441 assertTrue(Complex.ONE.multiply(Double.NaN).isNaN());
442 }
443
444 @Test
445 void testScalarMultiplyInf() {
446 Complex x = new Complex(1, 1);
447 double yDouble = Double.POSITIVE_INFINITY;
448 Complex yComplex = new Complex(yDouble);
449 assertEquals(x.multiply(yComplex), x.multiply(yDouble));
450
451 yDouble = Double.NEGATIVE_INFINITY;
452 yComplex = new Complex(yDouble);
453 assertEquals(x.multiply(yComplex), x.multiply(yDouble));
454
455 assertTrue(new Complex(Double.POSITIVE_INFINITY, 0).multiply(5).isInfinite());
456 assertTrue(new Complex(0, Double.POSITIVE_INFINITY).multiply(5).isInfinite());
457 assertTrue(Complex.INF.multiply(5).isInfinite());
458 assertTrue(new Complex(Double.POSITIVE_INFINITY, 0).multiply(5.0).isInfinite());
459 assertTrue(new Complex(0, Double.POSITIVE_INFINITY).multiply(5.0).isInfinite());
460 assertTrue(Complex.INF.multiply(5.0).isInfinite());
461 assertTrue(Complex.ONE.multiply(Double.POSITIVE_INFINITY).isInfinite());
462 }
463
464 @Test
465 void testNegate() {
466 Complex x = new Complex(3.0, 4.0);
467 Complex z = x.negate();
468 assertEquals(-3.0, z.getReal(), 1.0e-5);
469 assertEquals(-4.0, z.getImaginary(), 1.0e-5);
470 }
471
472 @Test
473 void testNegateNaN() {
474 Complex z = Complex.NaN.negate();
475 assertTrue(z.isNaN());
476 }
477
478 @Test
479 void testSubtract() {
480 Complex x = new Complex(3.0, 4.0);
481 Complex y = new Complex(5.0, 6.0);
482 Complex z = x.subtract(y);
483 assertEquals(-2.0, z.getReal(), 1.0e-5);
484 assertEquals(-2.0, z.getImaginary(), 1.0e-5);
485 }
486
487 @Test
488 void testSubtractNaN() {
489 Complex x = new Complex(3.0, 4.0);
490 Complex z = x.subtract(Complex.NaN);
491 assertSame(Complex.NaN, z);
492 z = new Complex(1, nan);
493 Complex w = x.subtract(z);
494 assertSame(Complex.NaN, w);
495 assertSame(Complex.NaN, Complex.NaN.subtract(Complex.NaN));
496 assertSame(Complex.NaN, Complex.NaN.subtract(Double.NaN));
497 }
498
499 @Test
500 void testSubtractInf() {
501 Complex x = new Complex(1, 1);
502 Complex z = new Complex(neginf, 0);
503 Complex w = x.subtract(z);
504 assertEquals(1, w.getImaginary(), 0);
505 assertEquals(inf, w.getReal(), 0);
506
507 x = new Complex(neginf, 0);
508 assertTrue(Double.isNaN(x.subtract(z).getReal()));
509 }
510
511 @Test
512 void testScalarSubtract() {
513 Complex x = new Complex(3.0, 4.0);
514 double yDouble = 2.0;
515 Complex yComplex = new Complex(yDouble);
516 assertEquals(x.subtract(yComplex), x.subtract(yDouble));
517 }
518
519 @Test
520 void testScalarSubtractNaN() {
521 Complex x = new Complex(3.0, 4.0);
522 double yDouble = Double.NaN;
523 Complex yComplex = new Complex(yDouble);
524 assertEquals(x.subtract(yComplex), x.subtract(yDouble));
525 }
526
527 @Test
528 void testScalarSubtractInf() {
529 Complex x = new Complex(1, 1);
530 double yDouble = Double.POSITIVE_INFINITY;
531 Complex yComplex = new Complex(yDouble);
532 assertEquals(x.subtract(yComplex), x.subtract(yDouble));
533
534 x = new Complex(neginf, 0);
535 assertEquals(x.subtract(yComplex), x.subtract(yDouble));
536 }
537
538 @Test
539 void testEqualsNull() {
540 Complex x = new Complex(3.0, 4.0);
541 assertNotEquals(null, x);
542 }
543
544 @Test
545 void testFloatingPointEqualsPrecondition1() {
546 assertThrows(NullPointerException.class, () -> Complex.equals(new Complex(3.0, 4.0), null, 3));
547 }
548
549 @Test
550 void testFloatingPointEqualsPrecondition2() {
551 assertThrows(NullPointerException.class, () -> Complex.equals(null, new Complex(3.0, 4.0), 3));
552 }
553
554 @SuppressWarnings("unlikely-arg-type")
555 @Test
556 void testEqualsClass() {
557 Complex x = new Complex(3.0, 4.0);
558 assertNotEquals(this, x);
559 }
560
561 @Test
562 void testEqualsSame() {
563 Complex x = new Complex(3.0, 4.0);
564 assertEquals(x, x);
565 }
566
567 @Test
568 void testFloatingPointEquals() {
569 double re = -3.21;
570 double im = 456789e10;
571
572 final Complex x = new Complex(re, im);
573 Complex y = new Complex(re, im);
574
575 assertEquals(x, y);
576 assertTrue(Complex.equals(x, y));
577
578 final int maxUlps = 5;
579 for (int i = 0; i < maxUlps; i++) {
580 re = FastMath.nextUp(re);
581 im = FastMath.nextUp(im);
582 }
583 y = new Complex(re, im);
584 assertTrue(Complex.equals(x, y, maxUlps));
585
586 re = FastMath.nextUp(re);
587 im = FastMath.nextUp(im);
588 y = new Complex(re, im);
589 assertFalse(Complex.equals(x, y, maxUlps));
590 }
591
592 @Test
593 void testFloatingPointEqualsNaN() {
594 Complex c = new Complex(Double.NaN, 1);
595 assertFalse(Complex.equals(c, c));
596
597 c = new Complex(1, Double.NaN);
598 assertFalse(Complex.equals(c, c));
599 }
600
601 @Test
602 void testFloatingPointEqualsWithAllowedDelta() {
603 final double re = 153.0000;
604 final double im = 152.9375;
605 final double tol1 = 0.0625;
606 final Complex x = new Complex(re, im);
607 final Complex y = new Complex(re + tol1, im + tol1);
608 assertTrue(Complex.equals(x, y, tol1));
609
610 final double tol2 = 0.0624;
611 assertFalse(Complex.equals(x, y, tol2));
612 }
613
614 @Test
615 void testFloatingPointEqualsWithAllowedDeltaNaN() {
616 final Complex x = new Complex(0, Double.NaN);
617 final Complex y = new Complex(Double.NaN, 0);
618 assertFalse(Complex.equals(x, Complex.ZERO, 0.1));
619 assertFalse(Complex.equals(x, x, 0.1));
620 assertFalse(Complex.equals(x, y, 0.1));
621 }
622
623 @Test
624 void testFloatingPointEqualsWithRelativeTolerance() {
625 final double tol = 1e-4;
626 final double re = 1;
627 final double im = 1e10;
628
629 final double f = 1 + tol;
630 final Complex x = new Complex(re, im);
631 final Complex y = new Complex(re * f, im * f);
632 assertTrue(Complex.equalsWithRelativeTolerance(x, y, tol));
633 }
634
635 @Test
636 void testFloatingPointEqualsWithRelativeToleranceNaN() {
637 final Complex x = new Complex(0, Double.NaN);
638 final Complex y = new Complex(Double.NaN, 0);
639 assertFalse(Complex.equalsWithRelativeTolerance(x, Complex.ZERO, 0.1));
640 assertFalse(Complex.equalsWithRelativeTolerance(x, x, 0.1));
641 assertFalse(Complex.equalsWithRelativeTolerance(x, y, 0.1));
642 }
643
644 @Test
645 void testEqualsTrue() {
646 Complex x = new Complex(3.0, 4.0);
647 Complex y = new Complex(3.0, 4.0);
648 assertEquals(x, y);
649 }
650
651 @Test
652 void testEqualsRealDifference() {
653 Complex x = new Complex(0.0, 0.0);
654 Complex y = new Complex(0.0 + Double.MIN_VALUE, 0.0);
655 assertNotEquals(x, y);
656 }
657
658 @Test
659 void testEqualsImaginaryDifference() {
660 Complex x = new Complex(0.0, 0.0);
661 Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE);
662 assertNotEquals(x, y);
663 }
664
665 @Test
666 void testEqualsNaN() {
667 Complex realNaN = new Complex(Double.NaN, 0.0);
668 Complex imaginaryNaN = new Complex(0.0, Double.NaN);
669 Complex complexNaN = Complex.NaN;
670 assertEquals(realNaN, imaginaryNaN);
671 assertEquals(imaginaryNaN, complexNaN);
672 assertEquals(realNaN, complexNaN);
673 }
674
675 @Test
676 void testHashCode() {
677 Complex x = new Complex(0.0, 0.0);
678 Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE);
679 assertNotEquals(x.hashCode(), y.hashCode());
680 y = new Complex(0.0 + Double.MIN_VALUE, 0.0);
681 assertNotEquals(x.hashCode(), y.hashCode());
682 Complex realNaN = new Complex(Double.NaN, 0.0);
683 Complex imaginaryNaN = new Complex(0.0, Double.NaN);
684 assertEquals(realNaN.hashCode(), imaginaryNaN.hashCode());
685 assertEquals(imaginaryNaN.hashCode(), Complex.NaN.hashCode());
686
687
688
689
690 final String msg = "'equals' not compatible with 'hashCode'";
691
692 x = new Complex(0.0, 0.0);
693 y = new Complex(0.0, -0.0);
694 assertTrue(x.hashCode() != y.hashCode());
695 assertNotEquals(x, y, msg);
696
697 x = new Complex(0.0, 0.0);
698 y = new Complex(-0.0, 0.0);
699 assertTrue(x.hashCode() != y.hashCode());
700 assertNotEquals(x, y, msg);
701 }
702
703 @Test
704 void testToDegreesComplex() {
705 Complex z = new Complex(3, 4);
706 Complex expected = new Complex(FastMath.toDegrees(z.getReal()), FastMath.toDegrees(z.getImaginary()));
707 UnitTestUtils.customAssertEquals(expected, z.toDegrees(), 1.0e-15);
708 }
709
710 @Test
711 void testToRadiansComplex() {
712 Complex z = new Complex(3, 4);
713 Complex expected = new Complex(FastMath.toRadians(z.getReal()), FastMath.toRadians(z.getImaginary()));
714 UnitTestUtils.customAssertEquals(expected, z.toRadians(), 1.0e-15);
715 }
716
717 @Test
718 void testAcosComplex() {
719 Complex z = new Complex(3, 4);
720 Complex expected = new Complex(0.936812, -2.30551);
721 UnitTestUtils.customAssertEquals(expected, z.acos(), 1.0e-5);
722 UnitTestUtils.customAssertEquals(new Complex(FastMath.acos(0), 0),
723 Complex.ZERO.acos(), 1.0e-12);
724 }
725
726 @Test
727 void testAcosNaN() {
728 assertTrue(Complex.NaN.acos().isNaN());
729 }
730
731 @Test
732 void testAcosInf() {
733 UnitTestUtils.customAssertSame(Complex.NaN, oneInf.acos());
734 UnitTestUtils.customAssertSame(Complex.NaN, oneNegInf.acos());
735 UnitTestUtils.customAssertSame(Complex.NaN, infOne.acos());
736 UnitTestUtils.customAssertSame(Complex.NaN, negInfOne.acos());
737 UnitTestUtils.customAssertSame(Complex.NaN, infInf.acos());
738 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.acos());
739 UnitTestUtils.customAssertSame(Complex.NaN, negInfInf.acos());
740 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.acos());
741 }
742
743 @Test
744 void testAcosBranchCuts() {
745 UnitTestUtils.customAssertEquals(new Complex(3.141592653589793238462, -0.76103968373182660633),
746 FastMath.acos(new Complex(-1.3038404810405297, +0.0)),
747 1.0e-14);
748 UnitTestUtils.customAssertEquals(new Complex(3.141592653589793238462, +0.76103968373182660633),
749 FastMath.acos(new Complex(-1.3038404810405297, -0.0)),
750 1.0e-14);
751 UnitTestUtils.customAssertEquals(new Complex(0.0, -0.76103968373182660633),
752 FastMath.acos(new Complex(1.3038404810405297, +0.0)),
753 1.0e-14);
754 UnitTestUtils.customAssertEquals(new Complex(0.0, +0.76103968373182660633),
755 FastMath.acos(new Complex(1.3038404810405297, -0.0)),
756 1.0e-14);
757 }
758
759 @Test
760 void testAsinComplex() {
761 Complex z = new Complex(3, 4);
762 Complex expected = new Complex(0.633984, 2.30551);
763 UnitTestUtils.customAssertEquals(expected, z.asin(), 1.0e-5);
764 }
765
766 @Test
767 void testAsinNaN() {
768 assertTrue(Complex.NaN.asin().isNaN());
769 }
770
771 @Test
772 void testAsinInf() {
773 UnitTestUtils.customAssertSame(Complex.NaN, oneInf.asin());
774 UnitTestUtils.customAssertSame(Complex.NaN, oneNegInf.asin());
775 UnitTestUtils.customAssertSame(Complex.NaN, infOne.asin());
776 UnitTestUtils.customAssertSame(Complex.NaN, negInfOne.asin());
777 UnitTestUtils.customAssertSame(Complex.NaN, infInf.asin());
778 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.asin());
779 UnitTestUtils.customAssertSame(Complex.NaN, negInfInf.asin());
780 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.asin());
781 }
782
783 @Test
784 void testAsinBranchCuts() {
785 UnitTestUtils.customAssertEquals(new Complex(-1.57079632679489661923, +0.76103968373182660633),
786 FastMath.asin(new Complex(-1.3038404810405297, +0.0)),
787 1.0e-14);
788 UnitTestUtils.customAssertEquals(new Complex(-1.57079632679489661923, -0.76103968373182660633),
789 FastMath.asin(new Complex(-1.3038404810405297, -0.0)),
790 1.0e-14);
791 UnitTestUtils.customAssertEquals(new Complex(1.57079632679489661923, +0.76103968373182660633),
792 FastMath.asin(new Complex(1.3038404810405297, +0.0)),
793 1.0e-14);
794 UnitTestUtils.customAssertEquals(new Complex(1.57079632679489661923, -0.76103968373182660633),
795 FastMath.asin(new Complex(1.3038404810405297, -0.0)),
796 1.0e-14);
797 }
798
799 @Test
800 void testAtanComplex() {
801 Complex z = new Complex(3, 4);
802 Complex expected = new Complex(1.44831, 0.158997);
803 UnitTestUtils.customAssertEquals(expected, z.atan(), 1.0e-5);
804 }
805
806 @Test
807 void testAtanInf() {
808 UnitTestUtils.customAssertSame(Complex.NaN, oneInf.atan());
809 UnitTestUtils.customAssertSame(Complex.NaN, oneNegInf.atan());
810 UnitTestUtils.customAssertSame(Complex.NaN, infOne.atan());
811 UnitTestUtils.customAssertSame(Complex.NaN, negInfOne.atan());
812 UnitTestUtils.customAssertSame(Complex.NaN, infInf.atan());
813 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.atan());
814 UnitTestUtils.customAssertSame(Complex.NaN, negInfInf.atan());
815 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.atan());
816 }
817
818 @Test
819 void testAtanI() {
820 assertTrue(Complex.I.atan().isNaN());
821 }
822
823 @Test
824 void testAtanNaN() {
825 assertTrue(Complex.NaN.atan().isNaN());
826 }
827
828 @Test
829 void testAtanBranchCuts() {
830 UnitTestUtils.customAssertEquals(new Complex(+1.5707963267948966192, +1.0986122886681096913),
831 FastMath.atan(new Complex(+0.0, 1.25)),
832 1.0e-14);
833 UnitTestUtils.customAssertEquals(new Complex(-1.5707963267948966192, +1.0986122886681096913),
834 FastMath.atan(new Complex(-0.0, 1.25)),
835 1.0e-14);
836 UnitTestUtils.customAssertEquals(new Complex(+1.5707963267948966192, -1.0986122886681096913),
837 FastMath.atan(new Complex(+0.0, -1.25)),
838 1.0e-14);
839 UnitTestUtils.customAssertEquals(new Complex(-1.5707963267948966192, -1.0986122886681096913),
840 FastMath.atan(new Complex(-0.0, -1.25)),
841 1.0e-14);
842 UnitTestUtils.customAssertEquals(new Complex(0.0, +0.25541281188299534160),
843 FastMath.atan(new Complex(+0.0, 0.25)),
844 1.0e-14);
845 assertTrue(FastMath.copySign(1.0, FastMath.atan(new Complex(+0.0, 0.25)).getReal()) > 0.0);
846 UnitTestUtils.customAssertEquals(new Complex(0.0, +0.25541281188299534160),
847 FastMath.atan(new Complex(-0.0, 0.25)),
848 1.0e-14);
849 assertTrue(FastMath.copySign(1.0, FastMath.atan(new Complex(-0.0, 0.25)).getReal()) < 0.0);
850 UnitTestUtils.customAssertEquals(new Complex(0.0, -0.25541281188299534160),
851 FastMath.atan(new Complex(+0.0, -0.25)),
852 1.0e-14);
853 assertTrue(FastMath.copySign(1.0, FastMath.atan(new Complex(+0.0, -0.25)).getReal()) > 0.0);
854 UnitTestUtils.customAssertEquals(new Complex(0.0, -0.25541281188299534160),
855 FastMath.atan(new Complex(-0.0, -0.25)),
856 1.0e-14);
857 assertTrue(FastMath.copySign(1.0, FastMath.atan(new Complex(-0.0, -0.25)).getReal()) < 0.0);
858 }
859
860 @Test
861 public void testAtanReal() {
862 final Complex zP = new Complex(0.8734729023516287, 0.0);
863 final Complex aP = new Complex(0.717964439926383, 0.0);
864 Assertions.assertEquals(aP, zP.atan());
865 Assertions.assertEquals(1.0, FastMath.copySign(1.0, zP.atan().getImaginary()), 1.0e-15);
866 final Complex zM = new Complex(0.8734729023516287, -0.0);
867 final Complex aM = new Complex(0.717964439926383, -0.0);
868 Assertions.assertEquals(aM, zM.atan());
869 Assertions.assertEquals(-1.0, FastMath.copySign(1.0, zM.atan().getImaginary()), 1.0e-15);
870 }
871
872 @Test
873 @Override
874 public void testAtan2() {
875 for (double x = -3; x < 3; x += 0.2) {
876 for (double y = -3; y < 3; y += 0.2) {
877 final Complex z = build(x).atan2(build(y));
878 final double r = FastMath.atan2(x, y);
879 checkRelative(r, new Complex(MathUtils.normalizeAngle(z.getReal(), r), z.getImaginary()));
880 }
881 }
882 }
883
884 @Test
885 void testAtan2Complex() {
886 for (double r1 : Arrays.asList(-3, 3)) {
887 for (double i1 : Arrays.asList(-2, 0, 2)) {
888 final Complex c1 = new Complex(r1, i1);
889 for (double r2 : Arrays.asList(-1, 1)) {
890 for (double i2 : Arrays.asList(-5, 0, 5)) {
891 final Complex c2 = new Complex(r2, i2);
892 UnitTestUtils.customAssertEquals(c1.divide(c2), c1.atan2(c2).tan(), 1.0e-14);
893 final Complex atan = c1.divide(c2).atan();
894 final Complex atan2 = c1.atan2(c2);
895 final double deltaR = FastMath.abs(atan.getReal() - atan2.getReal()) / FastMath.PI;
896 assertTrue(FastMath.abs(deltaR - FastMath.rint(deltaR)) < 1.0e-14);
897 assertEquals(atan.getImaginary(), atan2.getImaginary(), 1.0e-14);
898 }
899 }
900 }
901 }
902 }
903
904 @Test
905 void testAtan2Real() {
906 for (double r1 : Arrays.asList(-3, 3)) {
907 final Complex c1 = new Complex(r1, 0);
908 for (double r2 : Arrays.asList(-1, 1)) {
909 final Complex c2 = new Complex(r2, 0);
910 assertEquals(FastMath.atan2(r1, r2),
911 MathUtils.normalizeAngle(c1.atan2(c2).getReal(), 0.0),
912 1.0e-14);
913 }
914 }
915 }
916
917 @Override
918 @Test
919 public void testAtan2SpecialCases() {
920 assertTrue(build(+0.0).atan2(build(+0.0)).isNaN());
921 assertTrue(build(-0.0).atan2(build(+0.0)).isNaN());
922 assertTrue(build(+0.0).atan2(build(-0.0)).isNaN());
923 assertTrue(build(-0.0).atan2(build(-0.0)).isNaN());
924 }
925
926 @Test
927 void testCosComplex() {
928 Complex z = new Complex(3, 4);
929 Complex expected = new Complex(-27.03495, -3.851153);
930 UnitTestUtils.customAssertEquals(expected, z.cos(), 1.0e-5);
931 }
932
933 @Test
934 void testCosNaN() {
935 assertTrue(Complex.NaN.cos().isNaN());
936 }
937
938 @Test
939 void testCosInf() {
940 UnitTestUtils.customAssertSame(infNegInf, oneInf.cos());
941 UnitTestUtils.customAssertSame(infInf, oneNegInf.cos());
942 UnitTestUtils.customAssertSame(Complex.NaN, infOne.cos());
943 UnitTestUtils.customAssertSame(Complex.NaN, negInfOne.cos());
944 UnitTestUtils.customAssertSame(Complex.NaN, infInf.cos());
945 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.cos());
946 UnitTestUtils.customAssertSame(Complex.NaN, negInfInf.cos());
947 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.cos());
948 }
949
950 @Test
951 void testCoshComplex() {
952 Complex z = new Complex(3, 4);
953 Complex expected = new Complex(-6.58066, -7.58155);
954 UnitTestUtils.customAssertEquals(expected, z.cosh(), 1.0e-5);
955 }
956
957 @Test
958 void testCoshNaN() {
959 assertTrue(Complex.NaN.cosh().isNaN());
960 }
961
962 @Test
963 void testCoshInf() {
964 UnitTestUtils.customAssertSame(Complex.NaN, oneInf.cosh());
965 UnitTestUtils.customAssertSame(Complex.NaN, oneNegInf.cosh());
966 UnitTestUtils.customAssertSame(infInf, infOne.cosh());
967 UnitTestUtils.customAssertSame(infNegInf, negInfOne.cosh());
968 UnitTestUtils.customAssertSame(Complex.NaN, infInf.cosh());
969 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.cosh());
970 UnitTestUtils.customAssertSame(Complex.NaN, negInfInf.cosh());
971 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.cosh());
972 }
973
974 @Test
975 void testExpComplex() {
976 Complex z = new Complex(3, 4);
977 Complex expected = new Complex(-13.12878, -15.20078);
978 UnitTestUtils.customAssertEquals(expected, z.exp(), 1.0e-5);
979 UnitTestUtils.customAssertEquals(Complex.ONE,
980 Complex.ZERO.exp(), 10e-12);
981 Complex iPi = Complex.I.multiply(new Complex(pi,0));
982 UnitTestUtils.customAssertEquals(Complex.ONE.negate(),
983 iPi.exp(), 10e-12);
984 }
985
986 @Test
987 void testExpNaN() {
988 assertTrue(Complex.NaN.exp().isNaN());
989 }
990
991 @Test
992 void testExpInf1() {
993 UnitTestUtils.customAssertSame(Complex.NaN, oneInf.exp());
994 }
995
996 @Test
997 void testExpInf2() {
998 UnitTestUtils.customAssertSame(Complex.NaN, oneNegInf.exp());
999 }
1000
1001 @Test
1002 void testExpInf3() {
1003 UnitTestUtils.customAssertSame(infInf, infOne.exp());
1004 }
1005
1006 @Test
1007 void testExpInf4() {
1008 final Complex exp = negInfOne.exp();
1009 UnitTestUtils.customAssertSame(Complex.ZERO, exp);
1010 }
1011
1012 @Test
1013 void testExpInf5() {
1014 UnitTestUtils.customAssertSame(Complex.NaN, infInf.exp());
1015 }
1016
1017 @Test
1018 void testExpInf6() {
1019 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.exp());
1020 }
1021
1022 @Test
1023 void testExpInf7() {
1024 UnitTestUtils.customAssertSame(Complex.NaN, negInfInf.exp());
1025 }
1026
1027 @Test
1028 void testExpInf8() {
1029 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.exp());
1030 }
1031
1032 @Test
1033 void testExpM1() {
1034 UnitTestUtils.customAssertSame(Complex.NaN, Complex.NaN.expm1());
1035 final double testValue = FastMath.scalb(1.0, -30);
1036 assertEquals(FastMath.expm1(testValue), new Complex(testValue, 0).expm1().getReal(), 1.0e-30);
1037 assertTrue(FastMath.expm1(testValue) - new Complex(testValue).exp().subtract(1.0).getReal() > 4.0e-19);
1038 assertEquals(0.0, new Complex(0, testValue).expm1().getReal(), 1.0e-30);
1039 assertEquals(0.0, new Complex(0, testValue).expm1().getImaginary(), 1.0e-30);
1040 }
1041
1042 @Test
1043 void testLogComplex() {
1044 Complex z = new Complex(3, 4);
1045 Complex expected = new Complex(1.60944, 0.927295);
1046 UnitTestUtils.customAssertEquals(expected, z.log(), 1.0e-5);
1047 }
1048
1049 @Test
1050 void testLogNaN() {
1051 assertTrue(Complex.NaN.log().isNaN());
1052 }
1053
1054 @Test
1055 void testLogInf() {
1056 UnitTestUtils.customAssertEquals(new Complex(inf, pi / 2),
1057 oneInf.log(), 10e-12);
1058 UnitTestUtils.customAssertEquals(new Complex(inf, -pi / 2),
1059 oneNegInf.log(), 10e-12);
1060 UnitTestUtils.customAssertEquals(infZero, infOne.log(), 10e-12);
1061 UnitTestUtils.customAssertEquals(new Complex(inf, pi),
1062 negInfOne.log(), 10e-12);
1063 UnitTestUtils.customAssertEquals(new Complex(inf, pi / 4),
1064 infInf.log(), 10e-12);
1065 UnitTestUtils.customAssertEquals(new Complex(inf, -pi / 4),
1066 infNegInf.log(), 10e-12);
1067 UnitTestUtils.customAssertEquals(new Complex(inf, 3d * pi / 4),
1068 negInfInf.log(), 10e-12);
1069 UnitTestUtils.customAssertEquals(new Complex(inf, - 3d * pi / 4),
1070 negInfNegInf.log(), 10e-12);
1071 }
1072
1073 @Test
1074 void testLogBranchCut() {
1075 UnitTestUtils.customAssertEquals(new Complex(0.6931471805599453094, +3.1415926535897932384),
1076 FastMath.log(new Complex(-2.0, +0.0)),
1077 10e-12);
1078 UnitTestUtils.customAssertEquals(new Complex(0.6931471805599453094, -3.1415926535897932384),
1079 FastMath.log(new Complex(-2.0, -0.0)),
1080 10e-12);
1081 }
1082
1083 @Test
1084 void testLogZero() {
1085 UnitTestUtils.customAssertSame(negInfZero, Complex.ZERO.log());
1086 }
1087
1088 @Test
1089 void testLog1P() {
1090 Complex z = new Complex(2, 4);
1091 Complex expected = new Complex(1.60944, 0.927295);
1092 UnitTestUtils.customAssertEquals(expected, z.log1p(), 1.0e-5);
1093 }
1094
1095 @Test
1096 void testLog10Complex() {
1097 UnitTestUtils.customAssertEquals(new Complex(2.0, 0.0), new Complex(100, 0).log10(), 1.0e-15);
1098 UnitTestUtils.customAssertEquals(new Complex(2.0, 0.5 * FastMath.PI / FastMath.log(10)), new Complex(0, 100).log10(), 1.0e-15);
1099 }
1100
1101 @Test
1102 @Override
1103 public void testLog10() {
1104 for (double x = -0.9; x < 0.9; x += 0.05) {
1105 if (x < 0) {
1106
1107 assertTrue(Double.isNaN(FastMath.log10(x)));
1108 assertFalse(build(x).log10().isNaN());
1109 } else {
1110 checkRelative(FastMath.log10(x), build(x).log10());
1111 }
1112 }
1113 }
1114
1115 @Test
1116 @Override
1117 public void testPowField() {
1118 for (double x = -0.9; x < 0.9; x += 0.05) {
1119 for (double y = 0.1; y < 4; y += 0.2) {
1120 if ( x < 0) {
1121
1122 assertTrue(Double.isNaN(FastMath.pow(x, y)));
1123 assertFalse(build(x).pow(build(y)).isNaN());
1124 } else {
1125 checkRelative(FastMath.pow(x, y), build(x).pow(build(y)));
1126 }
1127 }
1128 }
1129 }
1130
1131 @Test
1132 @Override
1133 public void testPowDouble() {
1134 for (double x = -0.9; x < 0.9; x += 0.05) {
1135 for (double y = 0.1; y < 4; y += 0.2) {
1136 if ( x < 0) {
1137
1138 assertTrue(Double.isNaN(FastMath.pow(x, y)));
1139 assertFalse(build(x).pow(y).isNaN());
1140 } else {
1141 checkRelative(FastMath.pow(x, y), build(x).pow(y));
1142 }
1143 }
1144 }
1145 }
1146
1147 @Test
1148 void testPow() {
1149 Complex x = new Complex(3, 4);
1150 Complex y = new Complex(5, 6);
1151 Complex expected = new Complex(-1.860893, 11.83677);
1152 UnitTestUtils.customAssertEquals(expected, x.pow(y), 1.0e-5);
1153 UnitTestUtils.customAssertEquals(new Complex(-46, 9).divide(2197), new Complex(2, -3).pow(new Complex(-3, 0)), 1.0e-15);
1154 UnitTestUtils.customAssertEquals(new Complex(-1, 0).divide(8), new Complex(-2, 0).pow(new Complex(-3, 0)), 1.0e-15);
1155 UnitTestUtils.customAssertEquals(new Complex(0, 2),
1156 new Complex(-4, 0).pow(new Complex(0.5, 0)),
1157 1.0e-15);
1158 }
1159
1160 @Test
1161 void testPowNaNBase() {
1162 Complex x = new Complex(3, 4);
1163 assertTrue(Complex.NaN.pow(x).isNaN());
1164 }
1165
1166 @Test
1167 void testPowNaNExponent() {
1168 Complex x = new Complex(3, 4);
1169 assertTrue(x.pow(Complex.NaN).isNaN());
1170 }
1171
1172 @Test
1173 void testPowInf() {
1174 UnitTestUtils.customAssertSame(Complex.NaN, Complex.ONE.pow(oneInf));
1175 UnitTestUtils.customAssertSame(Complex.NaN, Complex.ONE.pow(oneNegInf));
1176 UnitTestUtils.customAssertSame(Complex.NaN, Complex.ONE.pow(infOne));
1177 UnitTestUtils.customAssertSame(Complex.NaN, Complex.ONE.pow(infInf));
1178 UnitTestUtils.customAssertSame(Complex.NaN, Complex.ONE.pow(infNegInf));
1179 UnitTestUtils.customAssertSame(Complex.NaN, Complex.ONE.pow(negInfInf));
1180 UnitTestUtils.customAssertSame(Complex.NaN, Complex.ONE.pow(negInfNegInf));
1181 UnitTestUtils.customAssertSame(Complex.INF, infOne.pow(Complex.ONE));
1182 UnitTestUtils.customAssertSame(Complex.INF, negInfOne.pow(Complex.ONE));
1183 UnitTestUtils.customAssertSame(Complex.INF, infInf.pow(Complex.ONE));
1184 UnitTestUtils.customAssertSame(Complex.INF, infNegInf.pow(Complex.ONE));
1185 UnitTestUtils.customAssertSame(Complex.INF, negInfInf.pow(Complex.ONE));
1186 UnitTestUtils.customAssertSame(Complex.INF, negInfNegInf.pow(Complex.ONE));
1187 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.pow(infNegInf));
1188 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.pow(negInfNegInf));
1189 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.pow(infInf));
1190 UnitTestUtils.customAssertSame(Complex.NaN, infInf.pow(infNegInf));
1191 UnitTestUtils.customAssertSame(Complex.NaN, infInf.pow(negInfNegInf));
1192 UnitTestUtils.customAssertSame(Complex.NaN, infInf.pow(infInf));
1193 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.pow(infNegInf));
1194 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.pow(negInfNegInf));
1195 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.pow(infInf));
1196 }
1197
1198 @Test
1199 void testPowZero() {
1200 UnitTestUtils.customAssertEquals(Complex.ZERO,
1201 Complex.ZERO.pow(Complex.ONE), 1.0e-12);
1202 UnitTestUtils.customAssertSame(Complex.ONE,
1203 Complex.ZERO.pow(Complex.ZERO));
1204 UnitTestUtils.customAssertSame(Complex.NaN,
1205 Complex.ZERO.pow(Complex.I));
1206 UnitTestUtils.customAssertEquals(Complex.ONE,
1207 Complex.ONE.pow(Complex.ZERO), 10e-12);
1208 UnitTestUtils.customAssertEquals(Complex.ONE,
1209 Complex.I.pow(Complex.ZERO), 10e-12);
1210 UnitTestUtils.customAssertEquals(Complex.ONE,
1211 new Complex(-1, 3).pow(Complex.ZERO), 10e-12);
1212 }
1213
1214 @Test
1215 void testZeroPow() {
1216 UnitTestUtils.customAssertEquals(Complex.ZERO, Complex.ZERO.pow(2.0), 1.0e-5);
1217 }
1218
1219 @Test
1220 void testScalarPow() {
1221 Complex x = new Complex(3, 4);
1222 double yDouble = 5.0;
1223 Complex yComplex = new Complex(yDouble);
1224 assertEquals(x.pow(yComplex), x.pow(yDouble));
1225 UnitTestUtils.customAssertEquals(Complex.ONE.negate(), Complex.ONE.negate().pow(0.5).pow(2), 1.0e-15);
1226 UnitTestUtils.customAssertEquals(new Complex(2, 0), new Complex(4, 0).pow(0.5), 1.0e-15);
1227 UnitTestUtils.customAssertEquals(new Complex(2, 0), new Complex(4, 0).pow(new Complex(0.5, 0)), 1.0e-15);
1228 }
1229
1230 @Test
1231 void testScalarPowNaNBase() {
1232 Complex x = Complex.NaN;
1233 double yDouble = 5.0;
1234 Complex yComplex = new Complex(yDouble);
1235 assertEquals(x.pow(yComplex), x.pow(yDouble));
1236 }
1237
1238 @Test
1239 void testScalarPowNaNExponent() {
1240 Complex x = new Complex(3, 4);
1241 double yDouble = Double.NaN;
1242 Complex yComplex = new Complex(yDouble);
1243 assertEquals(x.pow(yComplex), x.pow(yDouble));
1244 }
1245
1246 @Test
1247 void testScalarPowInf() {
1248 UnitTestUtils.customAssertSame(Complex.NaN, Complex.ONE.pow(Double.POSITIVE_INFINITY));
1249 UnitTestUtils.customAssertSame(Complex.NaN, Complex.ONE.pow(Double.NEGATIVE_INFINITY));
1250 UnitTestUtils.customAssertSame(Complex.INF, infOne.pow(1.0));
1251 UnitTestUtils.customAssertSame(Complex.INF, negInfOne.pow(1.0));
1252 UnitTestUtils.customAssertSame(Complex.INF, infInf.pow(1.0));
1253 UnitTestUtils.customAssertSame(Complex.INF, infNegInf.pow(1.0));
1254 UnitTestUtils.customAssertSame(Complex.INF, negInfInf.pow(10));
1255 UnitTestUtils.customAssertSame(Complex.INF, negInfNegInf.pow(1.0));
1256 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.pow(Double.POSITIVE_INFINITY));
1257 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.pow(Double.POSITIVE_INFINITY));
1258 UnitTestUtils.customAssertSame(Complex.NaN, infInf.pow(Double.POSITIVE_INFINITY));
1259 UnitTestUtils.customAssertSame(Complex.NaN, infInf.pow(Double.NEGATIVE_INFINITY));
1260 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.pow(Double.NEGATIVE_INFINITY));
1261 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.pow(Double.POSITIVE_INFINITY));
1262 }
1263
1264 @Test
1265 void testScalarPowZero() {
1266 UnitTestUtils.customAssertEquals(Complex.ZERO, Complex.ZERO.pow(1.0), 1.0e-12);
1267 UnitTestUtils.customAssertSame(Complex.ONE, Complex.ZERO.pow(0.0));
1268 UnitTestUtils.customAssertEquals(Complex.ONE, Complex.ONE.pow(0.0), 10e-12);
1269 UnitTestUtils.customAssertEquals(Complex.ONE, Complex.I.pow(0.0), 10e-12);
1270 UnitTestUtils.customAssertEquals(Complex.ONE, new Complex(-1, 3).pow(0.0), 10e-12);
1271 }
1272
1273 @Test
1274 void testpowNull() {
1275 assertThrows(NullArgumentException.class, () -> Complex.ONE.pow(null));
1276 }
1277
1278 @Test
1279 void testSinComplex() {
1280 Complex z = new Complex(3, 4);
1281 Complex expected = new Complex(3.853738, -27.01681);
1282 UnitTestUtils.customAssertEquals(expected, z.sin(), 1.0e-5);
1283 }
1284
1285 @Test
1286 void testSinInf() {
1287 UnitTestUtils.customAssertSame(infInf, oneInf.sin());
1288 UnitTestUtils.customAssertSame(infNegInf, oneNegInf.sin());
1289 UnitTestUtils.customAssertSame(Complex.NaN, infOne.sin());
1290 UnitTestUtils.customAssertSame(Complex.NaN, negInfOne.sin());
1291 UnitTestUtils.customAssertSame(Complex.NaN, infInf.sin());
1292 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.sin());
1293 UnitTestUtils.customAssertSame(Complex.NaN, negInfInf.sin());
1294 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.sin());
1295 }
1296
1297 @Test
1298 void testSinNaN() {
1299 assertTrue(Complex.NaN.sin().isNaN());
1300 }
1301
1302 @Test
1303 void testSinhComplex() {
1304 Complex z = new Complex(3, 4);
1305 Complex expected = new Complex(-6.54812, -7.61923);
1306 UnitTestUtils.customAssertEquals(expected, z.sinh(), 1.0e-5);
1307 }
1308
1309 @Test
1310 void testSinhNaN() {
1311 assertTrue(Complex.NaN.sinh().isNaN());
1312 }
1313
1314 @Test
1315 void testSinhInf() {
1316 UnitTestUtils.customAssertSame(Complex.NaN, oneInf.sinh());
1317 UnitTestUtils.customAssertSame(Complex.NaN, oneNegInf.sinh());
1318 UnitTestUtils.customAssertSame(infInf, infOne.sinh());
1319 UnitTestUtils.customAssertSame(negInfInf, negInfOne.sinh());
1320 UnitTestUtils.customAssertSame(Complex.NaN, infInf.sinh());
1321 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.sinh());
1322 UnitTestUtils.customAssertSame(Complex.NaN, negInfInf.sinh());
1323 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.sinh());
1324 }
1325
1326 @Test
1327 void testAsinhComplex() {
1328 for (double x = -2; x <= 2; x += 0.125) {
1329 for (double y = -2; y <= 2; y += 0.125) {
1330 final Complex z = new Complex(x, y);
1331 UnitTestUtils.customAssertEquals(z, z.asinh().sinh(), 1.0e-14);
1332 }
1333 }
1334 }
1335
1336 @Test
1337 void testAsinhBranchCuts() {
1338 UnitTestUtils.customAssertEquals(new Complex(FastMath.log(2 + FastMath.sqrt(3)), 0.5 * FastMath.PI),
1339 new Complex(+0.0, 2.0).asinh(),
1340 1.0e-14);
1341 UnitTestUtils.customAssertEquals(new Complex(-FastMath.log(2 + FastMath.sqrt(3)), 0.5 * FastMath.PI),
1342 new Complex(-0.0, 2.0).asinh(),
1343 1.0e-14);
1344 }
1345
1346 @Test
1347 void testAcoshComplex() {
1348 for (double x = -2; x <= 2; x += 0.125) {
1349 for (double y = -2; y <= 2; y += 0.125) {
1350 final Complex z = new Complex(x, y);
1351 UnitTestUtils.customAssertEquals(z, z.acosh().cosh(), 1.0e-14);
1352 }
1353 }
1354 }
1355
1356 @Test
1357 void testAcoshBranchCuts() {
1358 UnitTestUtils.customAssertEquals(new Complex(FastMath.log(2 + FastMath.sqrt(3)), +FastMath.PI),
1359 new Complex(-2.0, +0.0).acosh(),
1360 1.0e-14);
1361 UnitTestUtils.customAssertEquals(new Complex(FastMath.log(2 + FastMath.sqrt(3)), -FastMath.PI),
1362 new Complex(-2.0, -0.0).acosh(),
1363 1.0e-14);
1364 }
1365
1366 @Test
1367 void testAtanhComplex() {
1368 for (double x = -2; x <= 2; x += 0.125) {
1369 for (double y = -2; y <= 2; y += 0.125) {
1370 final Complex z = new Complex(x, y);
1371 if (FastMath.abs(x) == 1.0 && y == 0.0) {
1372 assertTrue(z.atanh().isInfinite());
1373 } else {
1374 UnitTestUtils.customAssertEquals(z, z.atanh().tanh(), 1.0e-14);
1375 }
1376 }
1377 }
1378 }
1379
1380 @Test
1381 void testAtanhBranchCuts() {
1382 UnitTestUtils.customAssertEquals(new Complex(-0.5 * FastMath.log(3), +0.5 * FastMath.PI),
1383 new Complex(-2.0, +0.0).atanh(),
1384 1.0e-14);
1385 UnitTestUtils.customAssertEquals(new Complex(-0.5 * FastMath.log(3), -0.5 * FastMath.PI),
1386 new Complex(-2.0, -0.0).atanh(),
1387 1.0e-14);
1388 }
1389
1390 @Test
1391 void testSqrtRealPositive() {
1392 Complex z = new Complex(3, 4);
1393 Complex expected = new Complex(2, 1);
1394 UnitTestUtils.customAssertEquals(expected, z.sqrt(), 1.0e-5);
1395 }
1396
1397 @Test
1398 void testSqrtRealZero() {
1399 Complex z = new Complex(0.0, 4);
1400 Complex expected = new Complex(1.41421, 1.41421);
1401 UnitTestUtils.customAssertEquals(expected, z.sqrt(), 1.0e-5);
1402 }
1403
1404 @Test
1405 void testSqrtZero() {
1406 UnitTestUtils.customAssertEquals(Complex.ZERO, Complex.ZERO.sqrt(), 1.0e-15);
1407 }
1408
1409 @Test
1410 void testSqrtRealNegative() {
1411 Complex z = new Complex(-3.0, 4);
1412 Complex expected = new Complex(1, 2);
1413 UnitTestUtils.customAssertEquals(expected, z.sqrt(), 1.0e-5);
1414 }
1415
1416 @Test
1417 void testSqrtImaginaryZero() {
1418 Complex z = new Complex(-3.0, 0.0);
1419 Complex expected = new Complex(0.0, 1.73205);
1420 UnitTestUtils.customAssertEquals(expected, z.sqrt(), 1.0e-5);
1421 }
1422
1423 @Test
1424 void testSqrtImaginaryNegative() {
1425 Complex z = new Complex(-3.0, -4.0);
1426 Complex expected = new Complex(1.0, -2.0);
1427 UnitTestUtils.customAssertEquals(expected, z.sqrt(), 1.0e-5);
1428 }
1429
1430 @Test
1431 void testSqrtPolar() {
1432 double r = 1;
1433 for (int i = 0; i < 5; i++) {
1434 r += i;
1435 double theta = 0;
1436 for (int j =0; j < 11; j++) {
1437 theta += pi /12;
1438 Complex z = ComplexUtils.polar2Complex(r, theta);
1439 Complex sqrtz = ComplexUtils.polar2Complex(FastMath.sqrt(r), theta / 2);
1440 UnitTestUtils.customAssertEquals(sqrtz, z.sqrt(), 10e-12);
1441 }
1442 }
1443 }
1444
1445 @Test
1446 void testSqrtNaN() {
1447 assertTrue(Complex.NaN.sqrt().isNaN());
1448 }
1449
1450 @Test
1451 void testSqrtInf() {
1452 UnitTestUtils.customAssertSame(infNaN, oneInf.sqrt());
1453 UnitTestUtils.customAssertSame(infNaN, oneNegInf.sqrt());
1454 UnitTestUtils.customAssertSame(infZero, infOne.sqrt());
1455 UnitTestUtils.customAssertSame(zeroInf, negInfOne.sqrt());
1456 UnitTestUtils.customAssertSame(infNaN, infInf.sqrt());
1457 UnitTestUtils.customAssertSame(infNaN, infNegInf.sqrt());
1458 UnitTestUtils.customAssertSame(nanInf, negInfInf.sqrt());
1459 UnitTestUtils.customAssertSame(nanNegInf, negInfNegInf.sqrt());
1460 }
1461
1462 @Test
1463 void testSqrt1z() {
1464 Complex z = new Complex(3, 4);
1465 Complex expected = new Complex(4.08033, -2.94094);
1466 UnitTestUtils.customAssertEquals(expected, z.sqrt1z(), 1.0e-5);
1467 }
1468
1469 @Test
1470 void testSqrt1zNaN() {
1471 assertTrue(Complex.NaN.sqrt1z().isNaN());
1472 }
1473
1474 @Test
1475 @Override
1476 public void testCbrt() {
1477 for (double x = -0.9; x < 0.9; x += 0.05) {
1478 if ( x < 0) {
1479
1480 assertTrue(FastMath.cbrt(x) < 0);
1481 assertEquals(FastMath.PI / 3, build(x).cbrt().getArgument(), 1.0e-15);
1482 } else {
1483 checkRelative(FastMath.cbrt(x), build(x).cbrt());
1484 }
1485 }
1486 }
1487
1488 @Test
1489 void testCbrtComplex() {
1490 Complex z = new Complex(15, 2);
1491 UnitTestUtils.customAssertEquals(z, z.square().multiply(z).cbrt(), 1.0e-14);
1492 Complex branchCutPlus = new Complex(-8.0, +0.0);
1493 Complex cbrtPlus = branchCutPlus.cbrt();
1494 UnitTestUtils.customAssertEquals(branchCutPlus, cbrtPlus.multiply(cbrtPlus).multiply(cbrtPlus), 1.0e-14);
1495 assertEquals(1.0, cbrtPlus.getReal(), 1.0e-15);
1496 assertEquals(FastMath.sqrt(3.0), cbrtPlus.getImaginary(), 1.0e-15);
1497 Complex branchCutMinus = new Complex(-8.0, -0.0);
1498 Complex cbrtMinus = branchCutMinus.cbrt();
1499 UnitTestUtils.customAssertEquals(branchCutMinus, cbrtMinus.multiply(cbrtMinus).multiply(cbrtMinus), 1.0e-14);
1500 assertEquals(1.0, cbrtMinus.getReal(), 1.0e-15);
1501 assertEquals(-FastMath.sqrt(3.0), cbrtMinus.getImaginary(), 1.0e-15);
1502 }
1503
1504 @Test
1505 @Override
1506 public void testRootN() {
1507 for (double x = -0.9; x < 0.9; x += 0.05) {
1508 for (int n = 1; n < 5; ++n) {
1509 if (x < 0) {
1510
1511 final double doubleRoot = new Binary64(x).rootN(n).getReal();
1512 if (n % 2 == 0) {
1513 assertTrue(Double.isNaN(doubleRoot));
1514 } else {
1515 assertTrue(doubleRoot < 0);
1516 }
1517 assertEquals(FastMath.PI / n, build(x).rootN(n).getArgument(), 1.0e-15);
1518 } else {
1519 checkRelative(FastMath.pow(x, 1.0 / n), build(x).rootN(n));
1520 }
1521 }
1522 }
1523 }
1524
1525 @Test
1526 void testRootNComplex() {
1527 Complex z = new Complex(15, 2);
1528 UnitTestUtils.customAssertEquals(z, z.square().multiply(z).rootN(3), 1.0e-14);
1529 Complex branchCutPlus = new Complex(-8.0, +0.0);
1530 Complex cbrtPlus = branchCutPlus.rootN(3);
1531 UnitTestUtils.customAssertEquals(branchCutPlus, cbrtPlus.multiply(cbrtPlus).multiply(cbrtPlus), 1.0e-14);
1532 assertEquals(1.0, cbrtPlus.getReal(), 1.0e-15);
1533 assertEquals(FastMath.sqrt(3.0), cbrtPlus.getImaginary(), 1.0e-15);
1534 Complex branchCutMinus = new Complex(-8.0, -0.0);
1535 Complex cbrtMinus = branchCutMinus.rootN(3);
1536 UnitTestUtils.customAssertEquals(branchCutMinus, cbrtMinus.multiply(cbrtMinus).multiply(cbrtMinus), 1.0e-14);
1537 assertEquals(1.0, cbrtMinus.getReal(), 1.0e-15);
1538 assertEquals(-FastMath.sqrt(3.0), cbrtMinus.getImaginary(), 1.0e-15);
1539 }
1540
1541 @Test
1542 void testTanComplex() {
1543 Complex z = new Complex(3, 4);
1544 Complex expected = new Complex(-0.000187346, 0.999356);
1545 UnitTestUtils.customAssertEquals(expected, z.tan(), 1.0e-5);
1546
1547 Complex actual = new Complex(3.0, 1E10).tan();
1548 expected = new Complex(0, 1);
1549 UnitTestUtils.customAssertEquals(expected, actual, 1.0e-5);
1550 actual = new Complex(3.0, -1E10).tan();
1551 expected = new Complex(0, -1);
1552 UnitTestUtils.customAssertEquals(expected, actual, 1.0e-5);
1553 }
1554
1555 @Test
1556 void testTanNaN() {
1557 assertTrue(Complex.NaN.tan().isNaN());
1558 }
1559
1560 @Test
1561 void testTanInf() {
1562 UnitTestUtils.customAssertSame(Complex.valueOf(0.0, 1.0), oneInf.tan());
1563 UnitTestUtils.customAssertSame(Complex.valueOf(0.0, -1.0), oneNegInf.tan());
1564 UnitTestUtils.customAssertSame(Complex.NaN, infOne.tan());
1565 UnitTestUtils.customAssertSame(Complex.NaN, negInfOne.tan());
1566 UnitTestUtils.customAssertSame(Complex.NaN, infInf.tan());
1567 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.tan());
1568 UnitTestUtils.customAssertSame(Complex.NaN, negInfInf.tan());
1569 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.tan());
1570 }
1571
1572 @Test
1573 void testTanCritical() {
1574 UnitTestUtils.customAssertSame(infNaN, new Complex(pi/2, 0).tan());
1575 UnitTestUtils.customAssertSame(negInfNaN, new Complex(-pi/2, 0).tan());
1576 }
1577
1578 @Test
1579 void testTanhComplex() {
1580 Complex z = new Complex(3, 4);
1581 Complex expected = new Complex(1.00071, 0.00490826);
1582 UnitTestUtils.customAssertEquals(expected, z.tanh(), 1.0e-5);
1583
1584 Complex actual = new Complex(1E10, 3.0).tanh();
1585 expected = new Complex(1, 0);
1586 UnitTestUtils.customAssertEquals(expected, actual, 1.0e-5);
1587 actual = new Complex(-1E10, 3.0).tanh();
1588 expected = new Complex(-1, 0);
1589 UnitTestUtils.customAssertEquals(expected, actual, 1.0e-5);
1590 }
1591
1592 @Test
1593 void testTanhNaN() {
1594 assertTrue(Complex.NaN.tanh().isNaN());
1595 }
1596
1597 @Test
1598 void testTanhInf() {
1599 UnitTestUtils.customAssertSame(Complex.NaN, oneInf.tanh());
1600 UnitTestUtils.customAssertSame(Complex.NaN, oneNegInf.tanh());
1601 UnitTestUtils.customAssertSame(Complex.valueOf(1.0, 0.0), infOne.tanh());
1602 UnitTestUtils.customAssertSame(Complex.valueOf(-1.0, 0.0), negInfOne.tanh());
1603 UnitTestUtils.customAssertSame(Complex.NaN, infInf.tanh());
1604 UnitTestUtils.customAssertSame(Complex.NaN, infNegInf.tanh());
1605 UnitTestUtils.customAssertSame(Complex.NaN, negInfInf.tanh());
1606 UnitTestUtils.customAssertSame(Complex.NaN, negInfNegInf.tanh());
1607 }
1608
1609 @Test
1610 void testTanhCritical() {
1611 UnitTestUtils.customAssertSame(nanInf, new Complex(0, pi/2).tanh());
1612 }
1613
1614
1615 @Test
1616 void testMath221() {
1617 assertTrue(Complex.equals(new Complex(0,-1),
1618 new Complex(0,1).multiply(new Complex(-1,0))));
1619 }
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632 @Test
1633 void testNthRoot_normal_thirdRoot() {
1634
1635 Complex z = new Complex(-2,2);
1636
1637 List<Complex> thirdRootsOfZ = z.nthRoot(3);
1638
1639 assertEquals(3, thirdRootsOfZ.size());
1640
1641 assertEquals(1.0, thirdRootsOfZ.get(0).getReal(), 1.0e-5);
1642 assertEquals(1.0, thirdRootsOfZ.get(0).getImaginary(), 1.0e-5);
1643
1644 assertEquals(-1.3660254037844386, thirdRootsOfZ.get(1).getReal(), 1.0e-5);
1645 assertEquals(0.36602540378443843, thirdRootsOfZ.get(1).getImaginary(), 1.0e-5);
1646
1647 assertEquals(0.366025403784439, thirdRootsOfZ.get(2).getReal(), 1.0e-5);
1648 assertEquals(-1.3660254037844384, thirdRootsOfZ.get(2).getImaginary(), 1.0e-5);
1649 }
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664 @Test
1665 void testNthRoot_normal_fourthRoot() {
1666
1667 Complex z = new Complex(5,-2);
1668
1669 List<Complex> fourthRootsOfZ = z.nthRoot(4);
1670
1671 assertEquals(4, fourthRootsOfZ.size());
1672
1673 assertEquals(1.5164629308487783, fourthRootsOfZ.get(0).getReal(), 1.0e-5);
1674 assertEquals(-0.14469266210702247, fourthRootsOfZ.get(0).getImaginary(), 1.0e-5);
1675
1676 assertEquals(0.14469266210702256, fourthRootsOfZ.get(1).getReal(), 1.0e-5);
1677 assertEquals(1.5164629308487783, fourthRootsOfZ.get(1).getImaginary(), 1.0e-5);
1678
1679 assertEquals(-1.5164629308487783, fourthRootsOfZ.get(2).getReal(), 1.0e-5);
1680 assertEquals(0.14469266210702267, fourthRootsOfZ.get(2).getImaginary(), 1.0e-5);
1681
1682 assertEquals(-0.14469266210702275, fourthRootsOfZ.get(3).getReal(), 1.0e-5);
1683 assertEquals(-1.5164629308487783, fourthRootsOfZ.get(3).getImaginary(), 1.0e-5);
1684 }
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697 @Test
1698 void testNthRoot_cornercase_thirdRoot_imaginaryPartEmpty() {
1699
1700
1701 Complex z = new Complex(8,0);
1702
1703 List<Complex> thirdRootsOfZ = z.nthRoot(3);
1704
1705 assertEquals(3, thirdRootsOfZ.size());
1706
1707 assertEquals(2.0, thirdRootsOfZ.get(0).getReal(), 1.0e-5);
1708 assertEquals(0.0, thirdRootsOfZ.get(0).getImaginary(), 1.0e-5);
1709
1710 assertEquals(-1.0, thirdRootsOfZ.get(1).getReal(), 1.0e-5);
1711 assertEquals(1.7320508075688774, thirdRootsOfZ.get(1).getImaginary(), 1.0e-5);
1712
1713 assertEquals(-1.0, thirdRootsOfZ.get(2).getReal(), 1.0e-5);
1714 assertEquals(-1.732050807568877, thirdRootsOfZ.get(2).getImaginary(), 1.0e-5);
1715 }
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729 @Test
1730 void testNthRoot_cornercase_thirdRoot_realPartZero() {
1731
1732 Complex z = new Complex(0,2);
1733
1734 List<Complex> thirdRootsOfZ = z.nthRoot(3);
1735
1736 assertEquals(3, thirdRootsOfZ.size());
1737
1738 assertEquals(1.0911236359717216, thirdRootsOfZ.get(0).getReal(), 1.0e-5);
1739 assertEquals(0.6299605249474365, thirdRootsOfZ.get(0).getImaginary(), 1.0e-5);
1740
1741 assertEquals(-1.0911236359717216, thirdRootsOfZ.get(1).getReal(), 1.0e-5);
1742 assertEquals(0.6299605249474365, thirdRootsOfZ.get(1).getImaginary(), 1.0e-5);
1743
1744 assertEquals(-2.3144374213981936E-16, thirdRootsOfZ.get(2).getReal(), 1.0e-5);
1745 assertEquals(-1.2599210498948732, thirdRootsOfZ.get(2).getImaginary(), 1.0e-5);
1746 }
1747
1748
1749
1750
1751 @Test
1752 void testNthRoot_cornercase_NAN_Inf() {
1753
1754 List<Complex> roots = oneNaN.nthRoot(3);
1755 assertEquals(1,roots.size());
1756 assertEquals(Complex.NaN, roots.get(0));
1757
1758 roots = nanZero.nthRoot(3);
1759 assertEquals(1,roots.size());
1760 assertEquals(Complex.NaN, roots.get(0));
1761
1762
1763 roots = nanInf.nthRoot(3);
1764 assertEquals(1,roots.size());
1765 assertEquals(Complex.NaN, roots.get(0));
1766
1767
1768 roots = oneInf.nthRoot(3);
1769 assertEquals(1,roots.size());
1770 assertEquals(Complex.INF, roots.get(0));
1771
1772
1773 roots = negInfInf.nthRoot(3);
1774 assertEquals(1,roots.size());
1775 assertEquals(Complex.INF, roots.get(0));
1776 }
1777
1778 @Test
1779 void testNthRootError() {
1780 try {
1781 Complex.ONE.nthRoot(-1);
1782 fail("an exception should have been thrown");
1783 } catch (MathIllegalArgumentException miae) {
1784 assertEquals(LocalizedCoreFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N,
1785 miae.getSpecifier());
1786 }
1787 }
1788
1789 @Test
1790 void testIsMathematicalInteger() {
1791 doTestIsMathmeaticalInteger(-0.0, true);
1792 doTestIsMathmeaticalInteger(+0.0, true);
1793 doTestIsMathmeaticalInteger(-0.5, false);
1794 doTestIsMathmeaticalInteger(+0.5, false);
1795 doTestIsMathmeaticalInteger(Double.NaN, false);
1796 doTestIsMathmeaticalInteger(Double.POSITIVE_INFINITY, false);
1797 doTestIsMathmeaticalInteger(Double.NEGATIVE_INFINITY, false);
1798 doTestIsMathmeaticalInteger(Double.MIN_NORMAL, false);
1799 doTestIsMathmeaticalInteger(Double.MIN_VALUE, false);
1800 }
1801
1802 private void doTestIsMathmeaticalInteger(double imaginary, boolean expectedForInteger) {
1803 assertFalse(new Complex(Double.NaN, imaginary).isMathematicalInteger());
1804 assertFalse(new Complex(Double.POSITIVE_INFINITY, imaginary).isMathematicalInteger());
1805 assertFalse(new Complex(Double.NEGATIVE_INFINITY, imaginary).isMathematicalInteger());
1806 assertFalse(new Complex(Double.MIN_NORMAL, imaginary).isMathematicalInteger());
1807 assertFalse(new Complex(Double.MIN_VALUE, imaginary).isMathematicalInteger());
1808
1809 assertEquals(expectedForInteger, new Complex(-0.0, imaginary).isMathematicalInteger());
1810 assertEquals(expectedForInteger, new Complex(+0.0, imaginary).isMathematicalInteger());
1811
1812 for (int i = -1000; i < 1000; ++i) {
1813 final double d = i;
1814 assertEquals(expectedForInteger, new Complex(d, imaginary).isMathematicalInteger());
1815 assertFalse(new Complex(FastMath.nextAfter(d, Double.POSITIVE_INFINITY), imaginary).isMathematicalInteger());
1816 assertFalse(new Complex(FastMath.nextAfter(d, Double.NEGATIVE_INFINITY), imaginary).isMathematicalInteger());
1817 }
1818
1819 double minNoFractional = 0x1L << 52;
1820 assertEquals(expectedForInteger, new Complex(minNoFractional, imaginary).isMathematicalInteger());
1821 assertFalse(new Complex(minNoFractional - 0.5, imaginary).isMathematicalInteger());
1822 assertEquals(expectedForInteger, new Complex(minNoFractional + 0.5, imaginary).isMathematicalInteger());
1823
1824 }
1825
1826
1827
1828
1829 @Test
1830 void testGetArgument() {
1831 Complex z = new Complex(1, 0);
1832 assertEquals(0.0, z.getArgument(), 1.0e-12);
1833
1834 z = new Complex(1, 1);
1835 assertEquals(FastMath.PI/4, z.getArgument(), 1.0e-12);
1836
1837 z = new Complex(0, 1);
1838 assertEquals(FastMath.PI/2, z.getArgument(), 1.0e-12);
1839
1840 z = new Complex(-1, 1);
1841 assertEquals(3 * FastMath.PI/4, z.getArgument(), 1.0e-12);
1842
1843 z = new Complex(-1, 0);
1844 assertEquals(FastMath.PI, z.getArgument(), 1.0e-12);
1845
1846 z = new Complex(-1, -1);
1847 assertEquals(-3 * FastMath.PI/4, z.getArgument(), 1.0e-12);
1848
1849 z = new Complex(0, -1);
1850 assertEquals(-FastMath.PI/2, z.getArgument(), 1.0e-12);
1851
1852 z = new Complex(1, -1);
1853 assertEquals(-FastMath.PI/4, z.getArgument(), 1.0e-12);
1854
1855 }
1856
1857
1858
1859
1860 @Test
1861 void testGetArgumentInf() {
1862 assertEquals(FastMath.PI/4, infInf.getArgument(), 1.0e-12);
1863 assertEquals(FastMath.PI/2, oneInf.getArgument(), 1.0e-12);
1864 assertEquals(0.0, infOne.getArgument(), 1.0e-12);
1865 assertEquals(FastMath.PI/2, zeroInf.getArgument(), 1.0e-12);
1866 assertEquals(0.0, infZero.getArgument(), 1.0e-12);
1867 assertEquals(FastMath.PI, negInfOne.getArgument(), 1.0e-12);
1868 assertEquals(-3.0*FastMath.PI/4, negInfNegInf.getArgument(), 1.0e-12);
1869 assertEquals(-FastMath.PI/2, oneNegInf.getArgument(), 1.0e-12);
1870 }
1871
1872
1873
1874
1875 @Test
1876 void testGetArgumentNaN() {
1877 assertTrue(Double.isNaN(nanZero.getArgument()));
1878 assertTrue(Double.isNaN(zeroNaN.getArgument()));
1879 assertTrue(Double.isNaN(Complex.NaN.getArgument()));
1880 }
1881
1882 @Test
1883 void testValueOf() {
1884 assertEquals(2.0, Complex.valueOf(2.0).getReal(), 1.0e-15);
1885 assertEquals(0.0, Complex.valueOf(2.0).getImaginary(), 1.0e-15);
1886 assertTrue(Complex.valueOf(Double.NaN).isNaN());
1887 assertTrue(Complex.valueOf(Double.POSITIVE_INFINITY).isInfinite());
1888 assertEquals( 2.0, Complex.valueOf(2.0, -1.0).getReal(), 1.0e-15);
1889 assertEquals(-1.0, Complex.valueOf(2.0, -1.0).getImaginary(), 1.0e-15);
1890 assertTrue(Complex.valueOf(Double.NaN, 0.0).isNaN());
1891 assertTrue(Complex.valueOf(Double.POSITIVE_INFINITY, 0.0).isInfinite());
1892 assertTrue(Complex.valueOf(Double.NaN, -1.0).isNaN());
1893 assertTrue(Complex.valueOf(Double.POSITIVE_INFINITY, -1.0).isInfinite());
1894 assertTrue(Complex.valueOf(0.0, Double.NaN).isNaN());
1895 assertTrue(Complex.valueOf(0.0, Double.POSITIVE_INFINITY).isInfinite());
1896 assertTrue(Complex.valueOf(-1.0, Double.NaN).isNaN());
1897 assertTrue(Complex.valueOf(-1.0, Double.POSITIVE_INFINITY).isInfinite());
1898 }
1899
1900 @Test
1901 void testField() {
1902 assertEquals(ComplexField.getInstance(), Complex.ZERO.getField());
1903 }
1904
1905 @Test
1906 void testToString() {
1907 assertEquals("(1.0, -2.0)", new Complex(1, -2).toString());
1908 }
1909
1910 @Test
1911 void testScalbComplex() {
1912 assertEquals(0.125, new Complex(2.0, 1.0).scalb(-4).getReal(), 1.0e-15);
1913 assertEquals(0.0625, new Complex(2.0, 1.0).scalb(-4).getImaginary(), 1.0e-15);
1914 }
1915
1916 @Test
1917 void testHypotComplex() {
1918 assertEquals(5.8269600298808519855, new Complex(3, 4).hypot(new Complex(5, 6)).getReal(), 1.0e-15);
1919 assertEquals(7.2078750814528590485, new Complex(3, 4).hypot(new Complex(5, 6)).getImaginary(), 1.0e-15);
1920 }
1921
1922 @Test
1923 void testCeilComplex() {
1924 for (double x = -3.9; x < 3.9; x += 0.05) {
1925 for (double y = -3.9; y < 3.9; y += 0.05) {
1926 final Complex z = new Complex(x, y);
1927 assertEquals(FastMath.ceil(x), z.ceil().getReal(), 1.0e-15);
1928 assertEquals(FastMath.ceil(y), z.ceil().getImaginary(), 1.0e-15);
1929 }
1930 }
1931 }
1932
1933 @Test
1934 void testFloorComplex() {
1935 for (double x = -3.9; x < 3.9; x += 0.05) {
1936 for (double y = -3.9; y < 3.9; y += 0.05) {
1937 final Complex z = new Complex(x, y);
1938 assertEquals(FastMath.floor(x), z.floor().getReal(), 1.0e-15);
1939 assertEquals(FastMath.floor(y), z.floor().getImaginary(), 1.0e-15);
1940 }
1941 }
1942 }
1943
1944 @Test
1945 void testRintComplex() {
1946 for (double x = -3.9; x < 3.9; x += 0.05) {
1947 for (double y = -3.9; y < 3.9; y += 0.05) {
1948 final Complex z = new Complex(x, y);
1949 assertEquals(FastMath.rint(x), z.rint().getReal(), 1.0e-15);
1950 assertEquals(FastMath.rint(y), z.rint().getImaginary(), 1.0e-15);
1951 }
1952 }
1953 }
1954
1955 @Test
1956 void testRemainderComplexComplex() {
1957 for (double x1 = -3.9; x1 < 3.9; x1 += 0.125) {
1958 for (double y1 = -3.9; y1 < 3.9; y1 += 0.125) {
1959 final Complex z1 = new Complex(x1, y1);
1960 for (double x2 = -3.92; x2 < 3.9; x2 += 0.125) {
1961 for (double y2 = -3.92; y2 < 3.9; y2 += 0.125) {
1962 final Complex z2 = new Complex(x2, y2);
1963 final Complex r = z1.remainder(z2);
1964 final Complex q = z1.subtract(r).divide(z2);
1965 assertTrue(r.norm() <= z2.norm());
1966 assertEquals(FastMath.rint(q.getReal()), q.getReal(), 2.0e-14);
1967 assertEquals(FastMath.rint(q.getImaginary()), q.getImaginary(), 2.0e-14);
1968 }
1969 }
1970 }
1971 }
1972 }
1973
1974 @Test
1975 void testRemainderComplexDouble() {
1976 for (double x1 = -3.9; x1 < 3.9; x1 += 0.125) {
1977 for (double y1 = -3.9; y1 < 3.9; y1 += 0.125) {
1978 final Complex z1 = new Complex(x1, y1);
1979 for (double a = -3.92; a < 3.9; a += 0.125) {
1980 final Complex r = z1.remainder(a);
1981 final Complex q = z1.subtract(r).divide(a);
1982 assertTrue(r.norm() <= FastMath.abs(a));
1983 assertEquals(FastMath.rint(q.getReal()), q.getReal(), 2.0e-14);
1984 assertEquals(FastMath.rint(q.getImaginary()), q.getImaginary(), 2.0e-14);
1985 }
1986 }
1987 }
1988 }
1989
1990 @Test
1991 void testRemainderAxKr() {
1992 checkRemainder(new Complex(14, -5), new Complex(3, 4), new Complex(-1.0, 0.0));
1993 checkRemainder(new Complex(26, 120), new Complex(37, 226), new Complex(-11.0, -106.0));
1994 checkRemainder(new Complex(9.4, 6), new Complex(1.0, 1.0), new Complex(-0.6, 0.0));
1995 checkRemainder(new Complex(-5.89, 0.33), new Complex(2.4, -0.123), new Complex(-1.09, 0.084));
1996 }
1997
1998 private void checkRemainder(final Complex c1, final Complex c2, final Complex expectedRemainder) {
1999
2000 final Complex remainder = c1.remainder(c2);
2001 assertEquals(expectedRemainder.getReal(), remainder.getReal(), 1.0e-15);
2002 assertEquals(expectedRemainder.getImaginary(), remainder.getImaginary(), 1.0e-15);
2003
2004 final Complex crossCheck = c1.subtract(remainder).divide(c2);
2005 assertTrue(Precision.isMathematicalInteger(crossCheck.getReal()));
2006 assertTrue(Precision.isMathematicalInteger(crossCheck.getImaginary()));
2007
2008 }
2009
2010 @Test
2011 void testCopySignFieldComplex() {
2012 for (double x1 = -3.9; x1 < 3.9; x1 += 0.08) {
2013 for (double y1 = -3.9; y1 < 3.9; y1 += 0.08) {
2014 final Complex z1 = new Complex(x1, y1);
2015 for (double x2 = -3.9; x2 < 3.9; x2 += 0.08) {
2016 for (double y2 = -3.9; y2 < 3.9; y2 += 0.08) {
2017 final Complex z2 = new Complex(x2, y2);
2018 assertEquals(FastMath.copySign(x1, x2), z1.copySign(z2).getReal(), 1.0e-15);
2019 assertEquals(FastMath.copySign(y1, y2), z1.copySign(z2).getImaginary(), 1.0e-15);
2020 }
2021 }
2022 }
2023 }
2024 }
2025
2026 @Test
2027 void testCopySignDoubleComplex() {
2028 for (double x1 = -3.9; x1 < 3.9; x1 += 0.05) {
2029 for (double y1 = -3.9; y1 < 3.9; y1 += 0.05) {
2030 final Complex z1 = new Complex(x1, y1);
2031 for (double r = -3.9; r < 3.9; r += 0.05) {
2032 assertEquals(FastMath.copySign(x1, r), z1.copySign(r).getReal(), 1.0e-15);
2033 assertEquals(FastMath.copySign(y1, r), z1.copySign(r).getImaginary(), 1.0e-15);
2034 }
2035 }
2036 }
2037 }
2038
2039 @Test
2040 void testSignComplex() {
2041 for (double x = -3.9; x < 3.9; x += 0.05) {
2042 for (double y = -3.9; y < 3.9; y += 0.05) {
2043 final Complex z = new Complex(x, y);
2044 assertEquals(1.0, z.sign().norm(), 1.0e-15);
2045 assertEquals(FastMath.copySign(1, FastMath.signum(x)), FastMath.copySign(1, z.sign().getRealPart()), 1.0e-15);
2046 assertEquals(FastMath.copySign(1, FastMath.signum(y)), FastMath.copySign(1, z.sign().getImaginaryPart()), 1.0e-15);
2047 }
2048 }
2049 assertTrue(Complex.NaN.sign().isNaN());
2050 for (int sR : Arrays.asList(-1, +1)) {
2051 for (int sI : Arrays.asList(-1, +1)) {
2052 Complex z = new Complex(FastMath.copySign(0, sR), FastMath.copySign(0, sI));
2053 assertTrue(z.isZero());
2054 Complex zSign = z.sign();
2055 assertTrue(zSign.isZero());
2056 assertEquals(sR, FastMath.copySign(1, zSign.getRealPart()), 1.0e-15);
2057 assertEquals(sI, FastMath.copySign(1, zSign.getImaginaryPart()), 1.0e-15);
2058 }
2059 }
2060 }
2061
2062 @Test
2063 void testLinearCombination1() {
2064 final Complex[] a = new Complex[] {
2065 new Complex(-1321008684645961.0 / 268435456.0,
2066 +5774608829631843.0 / 268435456.0),
2067 new Complex(-7645843051051357.0 / 8589934592.0,
2068 0.0)
2069 };
2070 final Complex[] b = new Complex[] {
2071 new Complex(-5712344449280879.0 / 2097152.0,
2072 -4550117129121957.0 / 2097152.0),
2073 new Complex(8846951984510141.0 / 131072.0,
2074 0.0)
2075 };
2076
2077 final Complex abSumInline = Complex.ZERO.linearCombination(a[0], b[0],
2078 a[1], b[1]);
2079 final Complex abSumArray = Complex.ZERO.linearCombination(a, b);
2080
2081 UnitTestUtils.customAssertEquals(abSumInline, abSumArray, 0);
2082 UnitTestUtils.customAssertEquals(-1.8551294182586248737720779899, abSumInline.getReal(), 1.0e-15);
2083
2084 final Complex naive = a[0].multiply(b[0]).add(a[1].multiply(b[1]));
2085 assertTrue(naive.subtract(abSumInline).norm() > 1.5);
2086
2087 }
2088
2089 @Test
2090 void testSignedZeroEquality() {
2091
2092 assertFalse(new Complex(-0.0, 1.0).isZero());
2093 assertFalse(new Complex(+0.0, 1.0).isZero());
2094 assertFalse(new Complex( 1.0, -0.0).isZero());
2095 assertFalse(new Complex( 1.0, +0.0).isZero());
2096
2097 assertTrue(new Complex(-0.0, -0.0).isZero());
2098 assertTrue(new Complex(-0.0, +0.0).isZero());
2099 assertTrue(new Complex(+0.0, -0.0).isZero());
2100 assertTrue(new Complex(+0.0, +0.0).isZero());
2101
2102 assertNotEquals(Complex.ZERO, new Complex(-0.0, -0.0));
2103 assertNotEquals(Complex.ZERO, new Complex(-0.0, +0.0));
2104 assertNotEquals(Complex.ZERO, new Complex(+0.0, -0.0));
2105 assertEquals(Complex.ZERO, new Complex(+0.0, +0.0));
2106
2107 }
2108
2109 @Test
2110 void testSerial() {
2111 Complex z = new Complex(3.0, 4.0);
2112 assertEquals(z, UnitTestUtils.serializeAndRecover(z));
2113 Complex ncmplx = (Complex)UnitTestUtils.serializeAndRecover(oneNaN);
2114 assertEquals(nanZero, ncmplx);
2115 assertTrue(ncmplx.isNaN());
2116 Complex infcmplx = (Complex)UnitTestUtils.serializeAndRecover(infInf);
2117 assertEquals(infInf, infcmplx);
2118 assertTrue(infcmplx.isInfinite());
2119 TestComplex tz = new TestComplex(3.0, 4.0);
2120 assertEquals(tz, UnitTestUtils.serializeAndRecover(tz));
2121 TestComplex ntcmplx = (TestComplex)UnitTestUtils.serializeAndRecover(new TestComplex(oneNaN));
2122 assertEquals(nanZero, ntcmplx);
2123 assertTrue(ntcmplx.isNaN());
2124 TestComplex inftcmplx = (TestComplex)UnitTestUtils.serializeAndRecover(new TestComplex(infInf));
2125 assertEquals(infInf, inftcmplx);
2126 assertTrue(inftcmplx.isInfinite());
2127 }
2128
2129 @Test
2130 public void testIsSmall() {
2131 Assertions.assertTrue(new Complex(1.0e-13, 1.0e-13).isSmall(new Complex(1.0, 1.0), 1.0e-12));
2132 Assertions.assertFalse(new Complex(1.0e-11, 1.0e-13).isSmall(new Complex(1.0, 1.0), 1.0e-12));
2133 Assertions.assertFalse(new Complex(1.0e-13, 1.0e-11).isSmall(new Complex(1.0, 1.0), 1.0e-12));
2134 }
2135
2136
2137
2138
2139 public static class TestComplex extends Complex {
2140
2141
2142
2143
2144 private static final long serialVersionUID = 3268726724160389237L;
2145
2146 public TestComplex(double real, double imaginary) {
2147 super(real, imaginary);
2148 }
2149
2150 public TestComplex(Complex other) {
2151 this(other.getReal(), other.getImaginary());
2152 }
2153
2154 @Override
2155 protected TestComplex createComplex(double real, double imaginary) {
2156 return new TestComplex(real, imaginary);
2157 }
2158
2159 }
2160 }