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  package org.hipparchus.complex;
23  
24  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.hipparchus.exception.MathIllegalStateException;
26  import org.hipparchus.util.FastMath;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  
31  /**
32   * Unit tests for the {@link RootsOfUnity} class.
33   *
34   */
35  public class RootsOfUnityTest {
36  
37      @Test(expected = MathIllegalStateException.class)
38      public void testMathIllegalState1() {
39          final RootsOfUnity roots = new RootsOfUnity();
40          roots.getReal(0);
41      }
42  
43      @Test(expected = MathIllegalStateException.class)
44      public void testMathIllegalState2() {
45          final RootsOfUnity roots = new RootsOfUnity();
46          roots.getImaginary(0);
47      }
48  
49      @Test(expected = MathIllegalStateException.class)
50      public void testMathIllegalState3() {
51          final RootsOfUnity roots = new RootsOfUnity();
52          roots.isCounterClockWise();
53      }
54  
55      @Test(expected = MathIllegalArgumentException.class)
56      public void testZeroNumberOfRoots() {
57          final RootsOfUnity roots = new RootsOfUnity();
58          roots.computeRoots(0);
59      }
60  
61      @Test
62      public void testGetNumberOfRoots() {
63          final RootsOfUnity roots = new RootsOfUnity();
64          Assert.assertEquals("", 0, roots.getNumberOfRoots());
65          roots.computeRoots(5);
66          Assert.assertEquals("", 5, roots.getNumberOfRoots());
67          /*
68           * Testing -5 right after 5 is important, as the roots in this case are
69           * not recomputed.
70           */
71          roots.computeRoots(-5);
72          Assert.assertEquals("", 5, roots.getNumberOfRoots());
73          roots.computeRoots(6);
74          Assert.assertEquals("", 6, roots.getNumberOfRoots());
75      }
76  
77      @Test
78      public void testComputeRoots() {
79          final RootsOfUnity roots = new RootsOfUnity();
80          for (int n = -10; n < 11; n++) {
81              /*
82               * Testing -n right after n is important, as the roots in this case
83               * are not recomputed.
84               */
85              if (n != 0) {
86                  roots.computeRoots(n);
87                  doTestComputeRoots(roots);
88                  roots.computeRoots(-n);
89                  doTestComputeRoots(roots);
90              }
91          }
92      }
93  
94      private void doTestComputeRoots(final RootsOfUnity roots) {
95          final int n = roots.isCounterClockWise() ? roots.getNumberOfRoots() :
96              -roots.getNumberOfRoots();
97          final double tol = 10 * Math.ulp(1.0);
98          for (int k = 0; k < n; k++) {
99              final double t = 2.0 * FastMath.PI * k / n;
100             final String msg = String.format("n = %d, k = %d", n, k);
101             Assert.assertEquals(msg, FastMath.cos(t), roots.getReal(k), tol);
102             Assert.assertEquals(msg, FastMath.sin(t), roots.getImaginary(k), tol);
103         }
104     }
105 }