You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2011/11/25 15:48:55 UTC

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

Author: erans
Date: Fri Nov 25 14:48:54 2011
New Revision: 1206199

URL: http://svn.apache.org/viewvc?rev=1206199&view=rev
Log:
MATH-689
Moved "pow" (integer arguments) to "ArithmeticUtils".

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/BigFraction.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/ArithmeticUtils.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/ArithmeticUtilsTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/BigFraction.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/BigFraction.java?rev=1206199&r1=1206198&r2=1206199&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/BigFraction.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/fraction/BigFraction.java Fri Nov 25 14:48:54 2011
@@ -27,6 +27,7 @@ import org.apache.commons.math.exception
 import org.apache.commons.math.exception.util.LocalizedFormats;
 import org.apache.commons.math.util.FastMath;
 import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.ArithmeticUtils;
 
 /**
  * Representation of a rational number without any overflow. This class is
@@ -956,11 +957,11 @@ public class BigFraction
      */
     public BigFraction pow(final long exponent) {
         if (exponent < 0) {
-            return new BigFraction(MathUtils.pow(denominator, -exponent),
-                                   MathUtils.pow(numerator,   -exponent));
+            return new BigFraction(ArithmeticUtils.pow(denominator, -exponent),
+                                   ArithmeticUtils.pow(numerator,   -exponent));
         }
-        return new BigFraction(MathUtils.pow(numerator,   exponent),
-                               MathUtils.pow(denominator, exponent));
+        return new BigFraction(ArithmeticUtils.pow(numerator,   exponent),
+                               ArithmeticUtils.pow(denominator, exponent));
     }
 
     /**
@@ -976,11 +977,11 @@ public class BigFraction
     public BigFraction pow(final BigInteger exponent) {
         if (exponent.compareTo(BigInteger.ZERO) < 0) {
             final BigInteger eNeg = exponent.negate();
-            return new BigFraction(MathUtils.pow(denominator, eNeg),
-                                   MathUtils.pow(numerator,   eNeg));
+            return new BigFraction(ArithmeticUtils.pow(denominator, eNeg),
+                                   ArithmeticUtils.pow(numerator,   eNeg));
         }
-        return new BigFraction(MathUtils.pow(numerator,   exponent),
-                               MathUtils.pow(denominator, exponent));
+        return new BigFraction(ArithmeticUtils.pow(numerator,   exponent),
+                               ArithmeticUtils.pow(denominator, exponent));
     }
 
     /**

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=1206199&r1=1206198&r2=1206199&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 Fri Nov 25 14:48:54 2011
@@ -16,6 +16,7 @@
  */
 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;
@@ -707,6 +708,179 @@ public final class ArithmeticUtils {
     }
 
     /**
+     * Raise an int to an int power.
+     *
+     * @param k Number to raise.
+     * @param e Exponent (must be positive or zero).
+     * @return k<sup>e</sup>
+     * @throws NotPositiveException if {@code e < 0}.
+     */
+    public static int pow(final int k, int e) {
+        if (e < 0) {
+            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
+        }
+
+        int result = 1;
+        int k2p    = k;
+        while (e != 0) {
+            if ((e & 0x1) != 0) {
+                result *= k2p;
+            }
+            k2p *= k2p;
+            e = e >> 1;
+        }
+
+        return result;
+    }
+
+    /**
+     * Raise an int to a long power.
+     *
+     * @param k Number to raise.
+     * @param e Exponent (must be positive or zero).
+     * @return k<sup>e</sup>
+     * @throws NotPositiveException if {@code e < 0}.
+     */
+    public static int pow(final int k, long e) {
+        if (e < 0) {
+            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
+        }
+
+        int result = 1;
+        int k2p    = k;
+        while (e != 0) {
+            if ((e & 0x1) != 0) {
+                result *= k2p;
+            }
+            k2p *= k2p;
+            e = e >> 1;
+        }
+
+        return result;
+    }
+
+    /**
+     * Raise a long to an int power.
+     *
+     * @param k Number to raise.
+     * @param e Exponent (must be positive or zero).
+     * @return k<sup>e</sup>
+     * @throws NotPositiveException if {@code e < 0}.
+     */
+    public static long pow(final long k, int e) {
+        if (e < 0) {
+            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
+        }
+
+        long result = 1l;
+        long k2p    = k;
+        while (e != 0) {
+            if ((e & 0x1) != 0) {
+                result *= k2p;
+            }
+            k2p *= k2p;
+            e = e >> 1;
+        }
+
+        return result;
+    }
+
+    /**
+     * Raise a long to a long power.
+     *
+     * @param k Number to raise.
+     * @param e Exponent (must be positive or zero).
+     * @return k<sup>e</sup>
+     * @throws NotPositiveException if {@code e < 0}.
+     */
+    public static long pow(final long k, long e) {
+        if (e < 0) {
+            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
+        }
+
+        long result = 1l;
+        long k2p    = k;
+        while (e != 0) {
+            if ((e & 0x1) != 0) {
+                result *= k2p;
+            }
+            k2p *= k2p;
+            e = e >> 1;
+        }
+
+        return result;
+    }
+
+    /**
+     * Raise a BigInteger to an int power.
+     *
+     * @param k Number to raise.
+     * @param e Exponent (must be positive or zero).
+     * @return k<sup>e</sup>
+     * @throws NotPositiveException if {@code e < 0}.
+     */
+    public static BigInteger pow(final BigInteger k, int e) {
+        if (e < 0) {
+            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
+        }
+
+        return k.pow(e);
+    }
+
+    /**
+     * Raise a BigInteger to a long power.
+     *
+     * @param k Number to raise.
+     * @param e Exponent (must be positive or zero).
+     * @return k<sup>e</sup>
+     * @throws NotPositiveException if {@code e < 0}.
+     */
+    public static BigInteger pow(final BigInteger k, long e) {
+        if (e < 0) {
+            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
+        }
+
+        BigInteger result = BigInteger.ONE;
+        BigInteger k2p    = k;
+        while (e != 0) {
+            if ((e & 0x1) != 0) {
+                result = result.multiply(k2p);
+            }
+            k2p = k2p.multiply(k2p);
+            e = e >> 1;
+        }
+
+        return result;
+
+    }
+
+    /**
+     * Raise a BigInteger to a BigInteger power.
+     *
+     * @param k Number to raise.
+     * @param e Exponent (must be positive or zero).
+     * @return k<sup>e</sup>
+     * @throws NotPositiveException if {@code e < 0}.
+     */
+    public static BigInteger pow(final BigInteger k, BigInteger e) {
+        if (e.compareTo(BigInteger.ZERO) < 0) {
+            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
+        }
+
+        BigInteger result = BigInteger.ONE;
+        BigInteger k2p    = k;
+        while (!BigInteger.ZERO.equals(e)) {
+            if (e.testBit(0)) {
+                result = result.multiply(k2p);
+            }
+            k2p = k2p.multiply(k2p);
+            e = e.shiftRight(1);
+        }
+
+        return result;
+    }
+
+    /**
      * Add two long integers, checking for overflow.
      *
      * @param a Addend.

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java?rev=1206199&r1=1206198&r2=1206199&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java Fri Nov 25 14:48:54 2011
@@ -17,12 +17,10 @@
 
 package org.apache.commons.math.util;
 
-import java.math.BigInteger;
 import java.util.Arrays;
 
 import org.apache.commons.math.exception.MathArithmeticException;
 import org.apache.commons.math.exception.NotFiniteNumberException;
-import org.apache.commons.math.exception.NotPositiveException;
 import org.apache.commons.math.exception.NullArgumentException;
 import org.apache.commons.math.exception.util.Localizable;
 import org.apache.commons.math.exception.util.LocalizedFormats;
@@ -277,179 +275,6 @@ public final class MathUtils {
     }
 
     /**
-     * Raise an int to an int power.
-     *
-     * @param k Number to raise.
-     * @param e Exponent (must be positive or zero).
-     * @return k<sup>e</sup>
-     * @throws NotPositiveException if {@code e < 0}.
-     */
-    public static int pow(final int k, int e) {
-        if (e < 0) {
-            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
-        }
-
-        int result = 1;
-        int k2p    = k;
-        while (e != 0) {
-            if ((e & 0x1) != 0) {
-                result *= k2p;
-            }
-            k2p *= k2p;
-            e = e >> 1;
-        }
-
-        return result;
-    }
-
-    /**
-     * Raise an int to a long power.
-     *
-     * @param k Number to raise.
-     * @param e Exponent (must be positive or zero).
-     * @return k<sup>e</sup>
-     * @throws NotPositiveException if {@code e < 0}.
-     */
-    public static int pow(final int k, long e) {
-        if (e < 0) {
-            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
-        }
-
-        int result = 1;
-        int k2p    = k;
-        while (e != 0) {
-            if ((e & 0x1) != 0) {
-                result *= k2p;
-            }
-            k2p *= k2p;
-            e = e >> 1;
-        }
-
-        return result;
-    }
-
-    /**
-     * Raise a long to an int power.
-     *
-     * @param k Number to raise.
-     * @param e Exponent (must be positive or zero).
-     * @return k<sup>e</sup>
-     * @throws NotPositiveException if {@code e < 0}.
-     */
-    public static long pow(final long k, int e) {
-        if (e < 0) {
-            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
-        }
-
-        long result = 1l;
-        long k2p    = k;
-        while (e != 0) {
-            if ((e & 0x1) != 0) {
-                result *= k2p;
-            }
-            k2p *= k2p;
-            e = e >> 1;
-        }
-
-        return result;
-    }
-
-    /**
-     * Raise a long to a long power.
-     *
-     * @param k Number to raise.
-     * @param e Exponent (must be positive or zero).
-     * @return k<sup>e</sup>
-     * @throws NotPositiveException if {@code e < 0}.
-     */
-    public static long pow(final long k, long e) {
-        if (e < 0) {
-            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
-        }
-
-        long result = 1l;
-        long k2p    = k;
-        while (e != 0) {
-            if ((e & 0x1) != 0) {
-                result *= k2p;
-            }
-            k2p *= k2p;
-            e = e >> 1;
-        }
-
-        return result;
-    }
-
-    /**
-     * Raise a BigInteger to an int power.
-     *
-     * @param k Number to raise.
-     * @param e Exponent (must be positive or zero).
-     * @return k<sup>e</sup>
-     * @throws NotPositiveException if {@code e < 0}.
-     */
-    public static BigInteger pow(final BigInteger k, int e) {
-        if (e < 0) {
-            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
-        }
-
-        return k.pow(e);
-    }
-
-    /**
-     * Raise a BigInteger to a long power.
-     *
-     * @param k Number to raise.
-     * @param e Exponent (must be positive or zero).
-     * @return k<sup>e</sup>
-     * @throws NotPositiveException if {@code e < 0}.
-     */
-    public static BigInteger pow(final BigInteger k, long e) {
-        if (e < 0) {
-            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
-        }
-
-        BigInteger result = BigInteger.ONE;
-        BigInteger k2p    = k;
-        while (e != 0) {
-            if ((e & 0x1) != 0) {
-                result = result.multiply(k2p);
-            }
-            k2p = k2p.multiply(k2p);
-            e = e >> 1;
-        }
-
-        return result;
-
-    }
-
-    /**
-     * Raise a BigInteger to a BigInteger power.
-     *
-     * @param k Number to raise.
-     * @param e Exponent (must be positive or zero).
-     * @return k<sup>e</sup>
-     * @throws NotPositiveException if {@code e < 0}.
-     */
-    public static BigInteger pow(final BigInteger k, BigInteger e) {
-        if (e.compareTo(BigInteger.ZERO) < 0) {
-            throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
-        }
-
-        BigInteger result = BigInteger.ONE;
-        BigInteger k2p    = k;
-        while (!BigInteger.ZERO.equals(e)) {
-            if (e.testBit(0)) {
-                result = result.multiply(k2p);
-            }
-            k2p = k2p.multiply(k2p);
-            e = e.shiftRight(1);
-        }
-
-        return result;
-    }
-
-    /**
      * Check that the argument is a real number.
      *
      * @param x Argument.

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=1206199&r1=1206198&r2=1206199&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 Fri Nov 25 14:48:54 2011
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.math.BigInteger;
 
 import org.apache.commons.math.exception.MathArithmeticException;
 import org.apache.commons.math.exception.MathIllegalArgumentException;
@@ -600,6 +601,83 @@ public class ArithmeticUtilsTest {
         testSubAndCheckLongFailure(min, 1L);
     }
 
+    @Test
+    public void testPow() {
+
+        Assert.assertEquals(1801088541, ArithmeticUtils.pow(21, 7));
+        Assert.assertEquals(1, ArithmeticUtils.pow(21, 0));
+        try {
+            ArithmeticUtils.pow(21, -7);
+            Assert.fail("Expecting MathIllegalArgumentException");
+        } catch (MathIllegalArgumentException e) {
+            // expected behavior
+        }
+
+        Assert.assertEquals(1801088541, ArithmeticUtils.pow(21, 7l));
+        Assert.assertEquals(1, ArithmeticUtils.pow(21, 0l));
+        try {
+            ArithmeticUtils.pow(21, -7l);
+            Assert.fail("Expecting MathIllegalArgumentException");
+        } catch (MathIllegalArgumentException e) {
+            // expected behavior
+        }
+
+        Assert.assertEquals(1801088541l, ArithmeticUtils.pow(21l, 7));
+        Assert.assertEquals(1l, ArithmeticUtils.pow(21l, 0));
+        try {
+            ArithmeticUtils.pow(21l, -7);
+            Assert.fail("Expecting MathIllegalArgumentException");
+        } catch (MathIllegalArgumentException e) {
+            // expected behavior
+        }
+
+        Assert.assertEquals(1801088541l, ArithmeticUtils.pow(21l, 7l));
+        Assert.assertEquals(1l, ArithmeticUtils.pow(21l, 0l));
+        try {
+            ArithmeticUtils.pow(21l, -7l);
+            Assert.fail("Expecting MathIllegalArgumentException");
+        } catch (MathIllegalArgumentException e) {
+            // expected behavior
+        }
+
+        BigInteger twentyOne = BigInteger.valueOf(21l);
+        Assert.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, 7));
+        Assert.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, 0));
+        try {
+            ArithmeticUtils.pow(twentyOne, -7);
+            Assert.fail("Expecting MathIllegalArgumentException");
+        } catch (MathIllegalArgumentException e) {
+            // expected behavior
+        }
+
+        Assert.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, 7l));
+        Assert.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, 0l));
+        try {
+            ArithmeticUtils.pow(twentyOne, -7l);
+            Assert.fail("Expecting MathIllegalArgumentException");
+        } catch (MathIllegalArgumentException e) {
+            // expected behavior
+        }
+
+        Assert.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(7l)));
+        Assert.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, BigInteger.ZERO));
+        try {
+            ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(-7l));
+            Assert.fail("Expecting MathIllegalArgumentException");
+        } catch (MathIllegalArgumentException e) {
+            // expected behavior
+        }
+
+        BigInteger bigOne =
+            new BigInteger("1543786922199448028351389769265814882661837148" +
+                           "4763915343722775611762713982220306372888519211" +
+                           "560905579993523402015636025177602059044911261");
+        Assert.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, 103));
+        Assert.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, 103l));
+        Assert.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(103l)));
+
+    }
+
     /**
      * Exact (caching) recursive implementation to test against
      */

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java?rev=1206199&r1=1206198&r2=1206199&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java Fri Nov 25 14:48:54 2011
@@ -13,9 +13,6 @@
  */
 package org.apache.commons.math.util;
 
-import java.math.BigInteger;
-
-
 import org.apache.commons.math.exception.MathIllegalArgumentException;
 import org.apache.commons.math.exception.MathArithmeticException;
 import org.apache.commons.math.exception.NotFiniteNumberException;
@@ -257,83 +254,6 @@ public final class MathUtilsTest {
     }
 
     @Test
-    public void testPow() {
-
-        Assert.assertEquals(1801088541, MathUtils.pow(21, 7));
-        Assert.assertEquals(1, MathUtils.pow(21, 0));
-        try {
-            MathUtils.pow(21, -7);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException e) {
-            // expected behavior
-        }
-
-        Assert.assertEquals(1801088541, MathUtils.pow(21, 7l));
-        Assert.assertEquals(1, MathUtils.pow(21, 0l));
-        try {
-            MathUtils.pow(21, -7l);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException e) {
-            // expected behavior
-        }
-
-        Assert.assertEquals(1801088541l, MathUtils.pow(21l, 7));
-        Assert.assertEquals(1l, MathUtils.pow(21l, 0));
-        try {
-            MathUtils.pow(21l, -7);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException e) {
-            // expected behavior
-        }
-
-        Assert.assertEquals(1801088541l, MathUtils.pow(21l, 7l));
-        Assert.assertEquals(1l, MathUtils.pow(21l, 0l));
-        try {
-            MathUtils.pow(21l, -7l);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException e) {
-            // expected behavior
-        }
-
-        BigInteger twentyOne = BigInteger.valueOf(21l);
-        Assert.assertEquals(BigInteger.valueOf(1801088541l), MathUtils.pow(twentyOne, 7));
-        Assert.assertEquals(BigInteger.ONE, MathUtils.pow(twentyOne, 0));
-        try {
-            MathUtils.pow(twentyOne, -7);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException e) {
-            // expected behavior
-        }
-
-        Assert.assertEquals(BigInteger.valueOf(1801088541l), MathUtils.pow(twentyOne, 7l));
-        Assert.assertEquals(BigInteger.ONE, MathUtils.pow(twentyOne, 0l));
-        try {
-            MathUtils.pow(twentyOne, -7l);
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException e) {
-            // expected behavior
-        }
-
-        Assert.assertEquals(BigInteger.valueOf(1801088541l), MathUtils.pow(twentyOne, BigInteger.valueOf(7l)));
-        Assert.assertEquals(BigInteger.ONE, MathUtils.pow(twentyOne, BigInteger.ZERO));
-        try {
-            MathUtils.pow(twentyOne, BigInteger.valueOf(-7l));
-            Assert.fail("Expecting MathIllegalArgumentException");
-        } catch (MathIllegalArgumentException e) {
-            // expected behavior
-        }
-
-        BigInteger bigOne =
-            new BigInteger("1543786922199448028351389769265814882661837148" +
-                           "4763915343722775611762713982220306372888519211" +
-                           "560905579993523402015636025177602059044911261");
-        Assert.assertEquals(bigOne, MathUtils.pow(twentyOne, 103));
-        Assert.assertEquals(bigOne, MathUtils.pow(twentyOne, 103l));
-        Assert.assertEquals(bigOne, MathUtils.pow(twentyOne, BigInteger.valueOf(103l)));
-
-    }
-
-    @Test
     public void testCheckFinite() {
         try {
             MathUtils.checkFinite(Double.POSITIVE_INFINITY);