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.UnitTestUtils;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27 import org.hipparchus.util.FastMath;
28 import org.junit.jupiter.api.Test;
29
30 import static org.junit.jupiter.api.Assertions.assertEquals;
31 import static org.junit.jupiter.api.Assertions.assertThrows;
32
33
34
35 class ComplexUtilsTest {
36
37 private final double inf = Double.POSITIVE_INFINITY;
38 private final double negInf = Double.NEGATIVE_INFINITY;
39 private final double nan = Double.NaN;
40 private final double pi = FastMath.PI;
41
42 private final Complex negInfInf = new Complex(negInf, inf);
43 private final Complex infNegInf = new Complex(inf, negInf);
44 private final Complex infInf = new Complex(inf, inf);
45 private final Complex negInfNegInf = new Complex(negInf, negInf);
46 private final Complex infNaN = new Complex(inf, nan);
47
48 @Test
49 void testPolar2Complex() {
50 UnitTestUtils.customAssertEquals(Complex.ONE,
51 ComplexUtils.polar2Complex(1, 0), 10e-12);
52 UnitTestUtils.customAssertEquals(Complex.ZERO,
53 ComplexUtils.polar2Complex(0, 1), 10e-12);
54 UnitTestUtils.customAssertEquals(Complex.ZERO,
55 ComplexUtils.polar2Complex(0, -1), 10e-12);
56 UnitTestUtils.customAssertEquals(Complex.I,
57 ComplexUtils.polar2Complex(1, pi/2), 10e-12);
58 UnitTestUtils.customAssertEquals(Complex.I.negate(),
59 ComplexUtils.polar2Complex(1, -pi/2), 10e-12);
60 double r = 0;
61 for (int i = 0; i < 5; i++) {
62 r += i;
63 double theta = 0;
64 for (int j =0; j < 20; j++) {
65 theta += pi / 6;
66 UnitTestUtils.customAssertEquals(altPolar(r, theta),
67 ComplexUtils.polar2Complex(r, theta), 10e-12);
68 }
69 theta = -2 * pi;
70 for (int j =0; j < 20; j++) {
71 theta -= pi / 6;
72 UnitTestUtils.customAssertEquals(altPolar(r, theta),
73 ComplexUtils.polar2Complex(r, theta), 10e-12);
74 }
75 }
76 }
77
78 protected Complex altPolar(double r, double theta) {
79 return Complex.I.multiply(new Complex(theta, 0)).exp().multiply(new Complex(r, 0));
80 }
81
82 @Test
83 void testPolar2ComplexIllegalModulus() {
84 assertThrows(MathIllegalArgumentException.class, () -> ComplexUtils.polar2Complex(-1, 0));
85 }
86
87 @Test
88 void testPolar2ComplexNaN() {
89 UnitTestUtils.customAssertSame(Complex.NaN, ComplexUtils.polar2Complex(nan, 1));
90 UnitTestUtils.customAssertSame(Complex.NaN, ComplexUtils.polar2Complex(1, nan));
91 UnitTestUtils.customAssertSame(Complex.NaN,
92 ComplexUtils.polar2Complex(nan, nan));
93 }
94
95 @Test
96 void testPolar2ComplexInf() {
97 UnitTestUtils.customAssertSame(Complex.NaN, ComplexUtils.polar2Complex(1, inf));
98 UnitTestUtils.customAssertSame(Complex.NaN,
99 ComplexUtils.polar2Complex(1, negInf));
100 UnitTestUtils.customAssertSame(Complex.NaN, ComplexUtils.polar2Complex(inf, inf));
101 UnitTestUtils.customAssertSame(Complex.NaN,
102 ComplexUtils.polar2Complex(inf, negInf));
103 UnitTestUtils.customAssertSame(infInf, ComplexUtils.polar2Complex(inf, pi/4));
104 UnitTestUtils.customAssertSame(infNaN, ComplexUtils.polar2Complex(inf, 0));
105 UnitTestUtils.customAssertSame(infNegInf, ComplexUtils.polar2Complex(inf, -pi/4));
106 UnitTestUtils.customAssertSame(negInfInf, ComplexUtils.polar2Complex(inf, 3*pi/4));
107 UnitTestUtils.customAssertSame(negInfNegInf, ComplexUtils.polar2Complex(inf, 5*pi/4));
108 }
109
110 @Test
111 void testConvertToComplex() {
112 final double[] real = new double[] { negInf, -123.45, 0, 1, 234.56, pi, inf };
113 final Complex[] complex = ComplexUtils.convertToComplex(real);
114
115 for (int i = 0; i < real.length; i++) {
116 assertEquals(real[i], complex[i].getReal(), 0d);
117 }
118 }
119 }