From a4d7e7c86ddbcf9216de47b746bc7f0e2eb7f103 Mon Sep 17 00:00:00 2001 From: Sebastien Brisard Date: Wed, 4 Jan 2012 03:47:46 +0000 Subject: [PATCH] Moved o.a.c.m.transform.FastFourierTransformer.isPowerOf2 to o.a.c.m.util.ArithmeticUtils.isPowerOfTwo (MATH-677). git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1227042 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/transform/FastCosineTransformer.java | 3 ++- .../math/transform/FastFourierTransformer.java | 15 +++------------ .../math/transform/FastHadamardTransformer.java | 5 +++-- .../commons/math/util/ArithmeticUtils.java | 11 ++++++++++- .../commons/math/util/ArithmeticUtilsTest.java | 16 +++++++++++++++- 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java b/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java index f9d4e5ceb..94ade6e81 100644 --- a/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java +++ b/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java @@ -22,6 +22,7 @@ import org.apache.commons.math.exception.MathIllegalArgumentException; import org.apache.commons.math.exception.NonMonotonicSequenceException; import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.util.LocalizedFormats; +import org.apache.commons.math.util.ArithmeticUtils; import org.apache.commons.math.util.FastMath; /** @@ -238,7 +239,7 @@ public class FastCosineTransformer implements RealTransformer { final double[] transformed = new double[f.length]; final int n = f.length - 1; - if (!FastFourierTransformer.isPowerOf2(n)) { + if (!ArithmeticUtils.isPowerOfTwo(n)) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE, Integer.valueOf(f.length)); diff --git a/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java b/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java index 70458030c..eabdbffd5 100644 --- a/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java +++ b/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java @@ -29,6 +29,7 @@ import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.OutOfRangeException; import org.apache.commons.math.exception.ZeroException; import org.apache.commons.math.exception.util.LocalizedFormats; +import org.apache.commons.math.util.ArithmeticUtils; import org.apache.commons.math.util.FastMath; /** @@ -465,16 +466,6 @@ public class FastFourierTransformer implements Serializable { return f; } - /** - * Returns true if the argument is a power of 2. - * - * @param n the number to test - * @return true if the argument is a power of 2 - */ - public static boolean isPowerOf2(long n) { - return (n > 0) && ((n & (n - 1)) == 0); - } - /** * Verifies that the data set has length of power of 2. * @@ -484,7 +475,7 @@ public class FastFourierTransformer implements Serializable { public static void verifyDataSet(double[] d) throws MathIllegalArgumentException { - if (!isPowerOf2(d.length)) { + if (!ArithmeticUtils.isPowerOfTwo(d.length)) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING, Integer.valueOf(d.length)); @@ -500,7 +491,7 @@ public class FastFourierTransformer implements Serializable { public static void verifyDataSet(Object[] o) throws MathIllegalArgumentException { - if (!isPowerOf2(o.length)) { + if (!ArithmeticUtils.isPowerOfTwo(o.length)) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING, Integer.valueOf(o.length)); diff --git a/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java b/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java index 652e22bb5..386d9e10e 100644 --- a/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java +++ b/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java @@ -21,6 +21,7 @@ import org.apache.commons.math.exception.MathIllegalArgumentException; import org.apache.commons.math.exception.NonMonotonicSequenceException; import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.util.LocalizedFormats; +import org.apache.commons.math.util.ArithmeticUtils; /** * Implements the Fast Hadamard Transform (FHT). @@ -259,7 +260,7 @@ public class FastHadamardTransformer implements RealTransformer { final int n = x.length; final int halfN = n / 2; - if (!FastFourierTransformer.isPowerOf2(n)) { + if (!ArithmeticUtils.isPowerOfTwo(n)) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO, Integer.valueOf(n)); @@ -311,7 +312,7 @@ public class FastHadamardTransformer implements RealTransformer { final int n = x.length; final int halfN = n / 2; - if (!FastFourierTransformer.isPowerOf2(n)) { + if (!ArithmeticUtils.isPowerOfTwo(n)) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO, Integer.valueOf(n)); diff --git a/src/main/java/org/apache/commons/math/util/ArithmeticUtils.java b/src/main/java/org/apache/commons/math/util/ArithmeticUtils.java index 2bd9bec4c..a0a022710 100644 --- a/src/main/java/org/apache/commons/math/util/ArithmeticUtils.java +++ b/src/main/java/org/apache/commons/math/util/ArithmeticUtils.java @@ -18,7 +18,6 @@ package org.apache.commons.math.util; import java.math.BigInteger; import org.apache.commons.math.exception.MathArithmeticException; -import org.apache.commons.math.exception.MathIllegalNumberException; import org.apache.commons.math.exception.NotPositiveException; import org.apache.commons.math.exception.NumberIsTooLargeException; import org.apache.commons.math.exception.util.Localizable; @@ -943,4 +942,14 @@ public final class ArithmeticUtils { throw new NotPositiveException(LocalizedFormats.BINOMIAL_NEGATIVE_PARAMETER, n); } } + + /** + * Returns true if the argument is a power of two. + * + * @param n the number to test + * @return true if the argument is a power of two + */ + public static boolean isPowerOfTwo(long n) { + return (n > 0) && ((n & (n - 1)) == 0); + } } diff --git a/src/test/java/org/apache/commons/math/util/ArithmeticUtilsTest.java b/src/test/java/org/apache/commons/math/util/ArithmeticUtilsTest.java index 3a86997a2..d055500c5 100644 --- a/src/test/java/org/apache/commons/math/util/ArithmeticUtilsTest.java +++ b/src/test/java/org/apache/commons/math/util/ArithmeticUtilsTest.java @@ -17,6 +17,7 @@ package org.apache.commons.math.util; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -678,6 +679,20 @@ public class ArithmeticUtilsTest { } + @Test + public void testIsPowerOfTwo() { + final int n = 1025; + final boolean[] expected = new boolean[n]; + Arrays.fill(expected, false); + for (int i = 1; i < expected.length; i *= 2) { + expected[i] = true; + } + for (int i = 0; i < expected.length; i++) { + final boolean actual = ArithmeticUtils.isPowerOfTwo(i); + Assert.assertTrue(Integer.toString(i), actual == expected[i]); + } + } + /** * Exact (caching) recursive implementation to test against */ @@ -750,6 +765,5 @@ public class ArithmeticUtilsTest { } catch (MathArithmeticException ex) { // success } - } }