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.LocalizedCoreFormats;
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  import org.hipparchus.exception.MathIllegalStateException;
27  import org.hipparchus.util.FastMath;
28  import org.junit.jupiter.api.Assertions;
29  import org.junit.jupiter.api.Test;
30  
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  import static org.junit.jupiter.api.Assertions.assertThrows;
33  
34  
35  /**
36   * Unit tests for the {@link RootsOfUnity} class.
37   *
38   */
39  class RootsOfUnityTest {
40  
41      @Test
42      void testMathIllegalState1() {
43          assertThrows(MathIllegalStateException.class, () -> {
44              final RootsOfUnity roots = new RootsOfUnity();
45              roots.getReal(0);
46          });
47      }
48  
49      @Test
50      void testMathIllegalState2() {
51          assertThrows(MathIllegalStateException.class, () -> {
52              final RootsOfUnity roots = new RootsOfUnity();
53              roots.getImaginary(0);
54          });
55      }
56  
57      @Test
58      void testMathIllegalState3() {
59          assertThrows(MathIllegalStateException.class, () -> {
60              final RootsOfUnity roots = new RootsOfUnity();
61              roots.isCounterClockWise();
62          });
63      }
64  
65      @Test
66      void testZeroNumberOfRoots() {
67          assertThrows(MathIllegalArgumentException.class, () -> {
68              final RootsOfUnity roots = new RootsOfUnity();
69              roots.computeRoots(0);
70          });
71      }
72  
73      @Test
74      void testGetNumberOfRoots() {
75          final RootsOfUnity roots = new RootsOfUnity();
76          assertEquals(0, roots.getNumberOfRoots(), "");
77          roots.computeRoots(5);
78          assertEquals(5, roots.getNumberOfRoots(), "");
79          /*
80           * Testing -5 right after 5 is important, as the roots in this case are
81           * not recomputed.
82           */
83          roots.computeRoots(-5);
84          assertEquals(5, roots.getNumberOfRoots(), "");
85          roots.computeRoots(6);
86          assertEquals(6, roots.getNumberOfRoots(), "");
87      }
88  
89      @Test
90      public void testErrorConditions() {
91          final RootsOfUnity roots = new RootsOfUnity();
92          roots.computeRoots(5);
93          try {
94              roots.getReal(-2);
95              Assertions.fail("an exception should have been thrown");
96          } catch (MathIllegalArgumentException e) {
97              assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_ROOT_OF_UNITY_INDEX,
98                           e.getSpecifier());
99              assertEquals(-2, e.getParts()[0]);
100             assertEquals( 0, e.getParts()[1]);
101             assertEquals( 4, e.getParts()[2]);
102         }
103         try {
104             roots.getImaginary(-2);
105             Assertions.fail("an exception should have been thrown");
106         } catch (MathIllegalArgumentException e) {
107             assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_ROOT_OF_UNITY_INDEX,
108                          e.getSpecifier());
109             assertEquals(-2, e.getParts()[0]);
110             assertEquals( 0, e.getParts()[1]);
111             assertEquals( 4, e.getParts()[2]);
112         }
113         try {
114             roots.getReal(5);
115             Assertions.fail("an exception should have been thrown");
116         } catch (MathIllegalArgumentException e) {
117             assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_ROOT_OF_UNITY_INDEX,
118                          e.getSpecifier());
119             assertEquals( 5, e.getParts()[0]);
120             assertEquals( 0, e.getParts()[1]);
121             assertEquals( 4, e.getParts()[2]);
122         }
123         try {
124             roots.getImaginary(5);
125             Assertions.fail("an exception should have been thrown");
126         } catch (MathIllegalArgumentException e) {
127             assertEquals(LocalizedCoreFormats.OUT_OF_RANGE_ROOT_OF_UNITY_INDEX,
128                          e.getSpecifier());
129             assertEquals( 5, e.getParts()[0]);
130             assertEquals( 0, e.getParts()[1]);
131             assertEquals( 4, e.getParts()[2]);
132         }
133     }
134 
135     @Test
136     void testComputeRoots() {
137         final RootsOfUnity roots = new RootsOfUnity();
138         for (int n = -10; n < 11; n++) {
139             /*
140              * Testing -n right after n is important, as the roots in this case
141              * are not recomputed.
142              */
143             if (n != 0) {
144                 roots.computeRoots(n);
145                 doTestComputeRoots(roots);
146                 roots.computeRoots(-n);
147                 doTestComputeRoots(roots);
148             }
149         }
150     }
151 
152     private void doTestComputeRoots(final RootsOfUnity roots) {
153         final int n = roots.isCounterClockWise() ? roots.getNumberOfRoots() :
154             -roots.getNumberOfRoots();
155         final double tol = 10 * Math.ulp(1.0);
156         for (int k = 0; k < n; k++) {
157             final double t = 2.0 * FastMath.PI * k / n;
158             final String msg = String.format("n = %d, k = %d", n, k);
159             assertEquals(FastMath.cos(t), roots.getReal(k), tol, msg);
160             assertEquals(FastMath.sin(t), roots.getImaginary(k), tol, msg);
161         }
162     }
163 }