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