You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ce...@apache.org on 2012/01/04 04:47:46 UTC

svn commit: r1227042 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/transform/ main/java/org/apache/commons/math/util/ test/java/org/apache/commons/math/util/

Author: celestin
Date: Wed Jan  4 03:47:46 2012
New Revision: 1227042

URL: http://svn.apache.org/viewvc?rev=1227042&view=rev
Log:
Moved o.a.c.m.transform.FastFourierTransformer.isPowerOf2 to o.a.c.m.util.ArithmeticUtils.isPowerOfTwo (MATH-677).

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/ArithmeticUtils.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/ArithmeticUtilsTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java?rev=1227042&r1=1227041&r2=1227042&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastCosineTransformer.java Wed Jan  4 03:47:46 2012
@@ -22,6 +22,7 @@ import org.apache.commons.math.exception
 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 imple
         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));

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java?rev=1227042&r1=1227041&r2=1227042&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastFourierTransformer.java Wed Jan  4 03:47:46 2012
@@ -29,6 +29,7 @@ import org.apache.commons.math.exception
 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;
 
 /**
@@ -466,16 +467,6 @@ public class FastFourierTransformer impl
     }
 
     /**
-     * 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.
      *
      * @param d the data array
@@ -484,7 +475,7 @@ public class FastFourierTransformer impl
     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 impl
     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));

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java?rev=1227042&r1=1227041&r2=1227042&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/transform/FastHadamardTransformer.java Wed Jan  4 03:47:46 2012
@@ -21,6 +21,7 @@ import org.apache.commons.math.exception
 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 <a href="http://www.archive.chipcenter.com/dsp/DSP000517F1.html">Fast Hadamard Transform</a> (FHT).
@@ -259,7 +260,7 @@ public class FastHadamardTransformer imp
         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 imp
         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));

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/ArithmeticUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/ArithmeticUtils.java?rev=1227042&r1=1227041&r2=1227042&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/ArithmeticUtils.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/ArithmeticUtils.java Wed Jan  4 03:47:46 2012
@@ -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);
+    }
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/ArithmeticUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/ArithmeticUtilsTest.java?rev=1227042&r1=1227041&r2=1227042&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/ArithmeticUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/ArithmeticUtilsTest.java Wed Jan  4 03:47:46 2012
@@ -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
         }
-
     }
 }