MATH-1416: Remove RootsOfUnity from commons-math as it have been moved to commons-numbers
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/9a3b7e47 Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/9a3b7e47 Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/9a3b7e47 Branch: refs/heads/master Commit: 9a3b7e4797155f3a0d087f37e83a6dba94dc3305 Parents: e415b2f Author: Ray DeCampo <[email protected]> Authored: Sat Sep 16 11:39:18 2017 -0400 Committer: Ray DeCampo <[email protected]> Committed: Sat Sep 16 11:39:18 2017 -0400 ---------------------------------------------------------------------- .../commons/math4/complex/RootsOfUnity.java | 218 ------------------- .../commons/math4/complex/RootsOfUnityTest.java | 102 --------- 2 files changed, 320 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/9a3b7e47/src/main/java/org/apache/commons/math4/complex/RootsOfUnity.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/complex/RootsOfUnity.java b/src/main/java/org/apache/commons/math4/complex/RootsOfUnity.java deleted file mode 100644 index 9d4079e..0000000 --- a/src/main/java/org/apache/commons/math4/complex/RootsOfUnity.java +++ /dev/null @@ -1,218 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.complex; - -import java.io.Serializable; - -import org.apache.commons.math4.exception.MathIllegalArgumentException; -import org.apache.commons.math4.exception.MathIllegalStateException; -import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.math4.exception.ZeroException; -import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.util.FastMath; - -/** - * A helper class for the computation and caching of the {@code n}-th roots of - * unity. - * - * @since 3.0 - */ -public class RootsOfUnity implements Serializable { - - /** Serializable version id. */ - private static final long serialVersionUID = 20120201L; - - /** Number of roots of unity. */ - private int omegaCount; - - /** Real part of the roots. */ - private double[] omegaReal; - - /** - * Imaginary part of the {@code n}-th roots of unity, for positive values - * of {@code n}. In this array, the roots are stored in counter-clockwise - * order. - */ - private double[] omegaImaginaryCounterClockwise; - - /** - * Imaginary part of the {@code n}-th roots of unity, for negative values - * of {@code n}. In this array, the roots are stored in clockwise order. - */ - private double[] omegaImaginaryClockwise; - - /** - * {@code true} if {@link #computeRoots(int)} was called with a positive - * value of its argument {@code n}. In this case, counter-clockwise ordering - * of the roots of unity should be used. - */ - private boolean isCounterClockWise; - - /** - * Build an engine for computing the {@code n}-th roots of unity. - */ - public RootsOfUnity() { - - omegaCount = 0; - omegaReal = null; - omegaImaginaryCounterClockwise = null; - omegaImaginaryClockwise = null; - isCounterClockWise = true; - } - - /** - * Returns {@code true} if {@link #computeRoots(int)} was called with a - * positive value of its argument {@code n}. If {@code true}, then - * counter-clockwise ordering of the roots of unity should be used. - * - * @return {@code true} if the roots of unity are stored in - * counter-clockwise order - * @throws MathIllegalStateException if no roots of unity have been computed - * yet - */ - public synchronized boolean isCounterClockWise() - throws MathIllegalStateException { - - if (omegaCount == 0) { - throw new MathIllegalStateException( - LocalizedFormats.ROOTS_OF_UNITY_NOT_COMPUTED_YET); - } - return isCounterClockWise; - } - - /** - * <p> - * Computes the {@code n}-th roots of unity. The roots are stored in - * {@code omega[]}, such that {@code omega[k] = w ^ k}, where - * {@code k = 0, ..., n - 1}, {@code w = exp(2 * pi * i / n)} and - * {@code i = sqrt(-1)}. - * </p> - * <p> - * Note that {@code n} can be positive of negative - * </p> - * <ul> - * <li>{@code abs(n)} is always the number of roots of unity.</li> - * <li>If {@code n > 0}, then the roots are stored in counter-clockwise order.</li> - * <li>If {@code n < 0}, then the roots are stored in clockwise order. - * </ul> - * - * @param n the (signed) number of roots of unity to be computed - * @throws ZeroException if {@code n = 0} - */ - public synchronized void computeRoots(int n) throws ZeroException { - - if (n == 0) { - throw new ZeroException( - LocalizedFormats.CANNOT_COMPUTE_0TH_ROOT_OF_UNITY); - } - - isCounterClockWise = n > 0; - - // avoid repetitive calculations - final int absN = FastMath.abs(n); - - if (absN == omegaCount) { - return; - } - - // calculate everything from scratch - final double t = 2.0 * FastMath.PI / absN; - final double cosT = FastMath.cos(t); - final double sinT = FastMath.sin(t); - omegaReal = new double[absN]; - omegaImaginaryCounterClockwise = new double[absN]; - omegaImaginaryClockwise = new double[absN]; - omegaReal[0] = 1.0; - omegaImaginaryCounterClockwise[0] = 0.0; - omegaImaginaryClockwise[0] = 0.0; - for (int i = 1; i < absN; i++) { - omegaReal[i] = omegaReal[i - 1] * cosT - - omegaImaginaryCounterClockwise[i - 1] * sinT; - omegaImaginaryCounterClockwise[i] = omegaReal[i - 1] * sinT + - omegaImaginaryCounterClockwise[i - 1] * cosT; - omegaImaginaryClockwise[i] = -omegaImaginaryCounterClockwise[i]; - } - omegaCount = absN; - } - - /** - * Get the real part of the {@code k}-th {@code n}-th root of unity. - * - * @param k index of the {@code n}-th root of unity - * @return real part of the {@code k}-th {@code n}-th root of unity - * @throws MathIllegalStateException if no roots of unity have been - * computed yet - * @throws MathIllegalArgumentException if {@code k} is out of range - */ - public synchronized double getReal(int k) - throws MathIllegalStateException, MathIllegalArgumentException { - - if (omegaCount == 0) { - throw new MathIllegalStateException( - LocalizedFormats.ROOTS_OF_UNITY_NOT_COMPUTED_YET); - } - if ((k < 0) || (k >= omegaCount)) { - throw new OutOfRangeException( - LocalizedFormats.OUT_OF_RANGE_ROOT_OF_UNITY_INDEX, - Integer.valueOf(k), - Integer.valueOf(0), - Integer.valueOf(omegaCount - 1)); - } - - return omegaReal[k]; - } - - /** - * Get the imaginary part of the {@code k}-th {@code n}-th root of unity. - * - * @param k index of the {@code n}-th root of unity - * @return imaginary part of the {@code k}-th {@code n}-th root of unity - * @throws MathIllegalStateException if no roots of unity have been - * computed yet - * @throws OutOfRangeException if {@code k} is out of range - */ - public synchronized double getImaginary(int k) - throws MathIllegalStateException, OutOfRangeException { - - if (omegaCount == 0) { - throw new MathIllegalStateException( - LocalizedFormats.ROOTS_OF_UNITY_NOT_COMPUTED_YET); - } - if ((k < 0) || (k >= omegaCount)) { - throw new OutOfRangeException( - LocalizedFormats.OUT_OF_RANGE_ROOT_OF_UNITY_INDEX, - Integer.valueOf(k), - Integer.valueOf(0), - Integer.valueOf(omegaCount - 1)); - } - - return isCounterClockWise ? omegaImaginaryCounterClockwise[k] : - omegaImaginaryClockwise[k]; - } - - /** - * Returns the number of roots of unity currently stored. If - * {@link #computeRoots(int)} was called with {@code n}, then this method - * returns {@code abs(n)}. If no roots of unity have been computed yet, this - * method returns 0. - * - * @return the number of roots of unity currently stored - */ - public synchronized int getNumberOfRoots() { - return omegaCount; - } -} http://git-wip-us.apache.org/repos/asf/commons-math/blob/9a3b7e47/src/test/java/org/apache/commons/math4/complex/RootsOfUnityTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/complex/RootsOfUnityTest.java b/src/test/java/org/apache/commons/math4/complex/RootsOfUnityTest.java deleted file mode 100644 index 6c0f91b..0000000 --- a/src/test/java/org/apache/commons/math4/complex/RootsOfUnityTest.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math4.complex; - -import org.apache.commons.math4.complex.RootsOfUnity; -import org.apache.commons.math4.exception.MathIllegalStateException; -import org.apache.commons.math4.exception.ZeroException; -import org.apache.commons.math4.util.FastMath; -import org.junit.Assert; -import org.junit.Test; - - -/** - * Unit tests for the {@link RootsOfUnity} class. - * - */ -public class RootsOfUnityTest { - - @Test(expected = MathIllegalStateException.class) - public void testMathIllegalState1() { - final RootsOfUnity roots = new RootsOfUnity(); - roots.getReal(0); - } - - @Test(expected = MathIllegalStateException.class) - public void testMathIllegalState2() { - final RootsOfUnity roots = new RootsOfUnity(); - roots.getImaginary(0); - } - - @Test(expected = MathIllegalStateException.class) - public void testMathIllegalState3() { - final RootsOfUnity roots = new RootsOfUnity(); - roots.isCounterClockWise(); - } - - @Test(expected = ZeroException.class) - public void testZeroNumberOfRoots() { - final RootsOfUnity roots = new RootsOfUnity(); - roots.computeRoots(0); - } - - @Test - public void testGetNumberOfRoots() { - final RootsOfUnity roots = new RootsOfUnity(); - Assert.assertEquals("", 0, roots.getNumberOfRoots()); - roots.computeRoots(5); - Assert.assertEquals("", 5, roots.getNumberOfRoots()); - /* - * Testing -5 right after 5 is important, as the roots in this case are - * not recomputed. - */ - roots.computeRoots(-5); - Assert.assertEquals("", 5, roots.getNumberOfRoots()); - roots.computeRoots(6); - Assert.assertEquals("", 6, roots.getNumberOfRoots()); - } - - @Test - public void testComputeRoots() { - final RootsOfUnity roots = new RootsOfUnity(); - for (int n = -10; n < 11; n++) { - /* - * Testing -n right after n is important, as the roots in this case - * are not recomputed. - */ - if (n != 0) { - roots.computeRoots(n); - doTestComputeRoots(roots); - roots.computeRoots(-n); - doTestComputeRoots(roots); - } - } - } - - private void doTestComputeRoots(final RootsOfUnity roots) { - final int n = roots.isCounterClockWise() ? roots.getNumberOfRoots() : - -roots.getNumberOfRoots(); - final double tol = 10 * Math.ulp(1.0); - for (int k = 0; k < n; k++) { - final double t = 2.0 * FastMath.PI * k / n; - @SuppressWarnings("boxing") - final String msg = String.format("n = %d, k = %d", n, k); - Assert.assertEquals(msg, FastMath.cos(t), roots.getReal(k), tol); - Assert.assertEquals(msg, FastMath.sin(t), roots.getImaginary(k), tol); - } - } -}
