View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
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, () -> {
85              ComplexUtils.polar2Complex(-1, 0);
86          });
87      }
88  
89      @Test
90      void testPolar2ComplexNaN() {
91          UnitTestUtils.customAssertSame(Complex.NaN, ComplexUtils.polar2Complex(nan, 1));
92          UnitTestUtils.customAssertSame(Complex.NaN, ComplexUtils.polar2Complex(1, nan));
93          UnitTestUtils.customAssertSame(Complex.NaN,
94                                         ComplexUtils.polar2Complex(nan, nan));
95      }
96  
97      @Test
98      void testPolar2ComplexInf() {
99          UnitTestUtils.customAssertSame(Complex.NaN, ComplexUtils.polar2Complex(1, inf));
100         UnitTestUtils.customAssertSame(Complex.NaN,
101                                        ComplexUtils.polar2Complex(1, negInf));
102         UnitTestUtils.customAssertSame(Complex.NaN, ComplexUtils.polar2Complex(inf, inf));
103         UnitTestUtils.customAssertSame(Complex.NaN,
104                                        ComplexUtils.polar2Complex(inf, negInf));
105         UnitTestUtils.customAssertSame(infInf, ComplexUtils.polar2Complex(inf, pi/4));
106         UnitTestUtils.customAssertSame(infNaN, ComplexUtils.polar2Complex(inf, 0));
107         UnitTestUtils.customAssertSame(infNegInf, ComplexUtils.polar2Complex(inf, -pi/4));
108         UnitTestUtils.customAssertSame(negInfInf, ComplexUtils.polar2Complex(inf, 3*pi/4));
109         UnitTestUtils.customAssertSame(negInfNegInf, ComplexUtils.polar2Complex(inf, 5*pi/4));
110     }
111 
112     @Test
113     void testConvertToComplex() {
114         final double[] real = new double[] { negInf, -123.45, 0, 1, 234.56, pi, inf };
115         final Complex[] complex = ComplexUtils.convertToComplex(real);
116 
117         for (int i = 0; i < real.length; i++) {
118             assertEquals(real[i], complex[i].getReal(), 0d);
119         }
120     }
121 }