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
This commit is contained in:
parent
7f6912730e
commit
a4d7e7c86d
|
@ -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));
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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 <a href="http://www.archive.chipcenter.com/dsp/DSP000517F1.html">Fast Hadamard Transform</a> (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));
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue