Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2D9C9BA61 for ; Wed, 4 Jan 2012 03:48:27 +0000 (UTC) Received: (qmail 55732 invoked by uid 500); 4 Jan 2012 03:48:24 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 55380 invoked by uid 500); 4 Jan 2012 03:48:13 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 55358 invoked by uid 99); 4 Jan 2012 03:48:11 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 04 Jan 2012 03:48:11 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 04 Jan 2012 03:48:08 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id B9C762388A39 for ; Wed, 4 Jan 2012 03:47:46 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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/ Date: Wed, 04 Jan 2012 03:47:46 -0000 To: commits@commons.apache.org From: celestin@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120104034746.B9C762388A39@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 Fast Hadamard Transform (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 } - } }