diff --git a/src/java/org/apache/commons/math/MathUtils.java b/src/java/org/apache/commons/math/MathUtils.java index f06cf8a0a..d6aa18420 100644 --- a/src/java/org/apache/commons/math/MathUtils.java +++ b/src/java/org/apache/commons/math/MathUtils.java @@ -14,7 +14,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the - * distribution. + * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: @@ -58,31 +58,131 @@ package org.apache.commons.math; * Some useful additions to the built-in functions in lang.Math
*
* @author Phil Steitz
- * @version $Revision: 1.1 $ $Date: 2003/06/04 02:31:13 $
+ * @version $Revision: 1.2 $ $Date: 2003/06/06 03:07:39 $
*/
public class MathUtils {
/**
- * Returns an exact representation of the
- *
- * Binomial Coefficient, "n choose k
",
- * the number of k
-element subsets that can be selected from
- * an n
-element set.
- *
- * Preconditions:
0 < k <= n
(otherwise
- * IllegalArgumentException
is thrown)long
. The
- * largest value of n
for which all coefficients are
- * < Long.MAX_VALUE
is 66. If the computed value
- * exceeds Long.MAX_VALUE
an ArithMeticException
- *
is thrown.n choose k
+ * For a double precision value x, this method returns +1.0 if x >= 0
+ * and -1.0 if x < 0.
+ *
+ * @author Albert Davidson Chou
+ * @param x the value, a double
+ * @return +1.0 or -1.0, depending on the the sign of x
*/
+ public static double sign( double x ) {
+ if ( x >= 0.0 ) {
+ return 1.0 ;
+ } else {
+ return -1.0 ;
+ }
+ }
+
+ /**
+ * For a float value x, this method returns +1.0F if x >= 0
+ * and -1.0F if x < 0.
+ *
+ * @author Albert Davidson Chou
+ * @param x the value, a float
+ * @return +1.0F or -1.0F, depending on the the sign of x
+ */
+ public static float sign( float x ) {
+ if ( x >= 0.0F ) {
+ return 1.0F ;
+ } else {
+ return -1.0F ;
+ }
+ }
+
+ /**
+ * For a byte value x, this method returns (byte)(+1) if x >= 0
+ * and (byte)(-1) if x < 0.
+ *
+ * @author Albert Davidson Chou
+ * @param x the value, a byte
+ * @return (byte)(+1) or (byte)(-1), depending on the the sign of x
+ */
+ public static byte sign( byte x ) {
+ if ( x >= (byte)0 ) {
+ return (byte)1 ;
+ } else {
+ return (byte)(-1) ;
+ }
+ }
+
+ /**
+ * For a short value x, this method returns (short)(+1) if x >= 0
+ * and (short)(-1) if x < 0.
+ *
+ * @author Albert Davidson Chou
+ * @param x the value, a short
+ * @return (short)(+1) or (short)(-1), depending on the the sign of x
+ */
+ public static short sign( short x ) {
+ if ( x >= (short)0 ) {
+ return (short)1 ;
+ } else {
+ return (short)(-1) ;
+ }
+ }
+
+ /**
+ * For an int value x, this method returns +1 if x >= 0
+ * and -1 if x < 0.
+ *
+ * @author Albert Davidson Chou
+ * @param x the value, an int
+ * @return +1 or -1, depending on the the sign of x
+ */
+ public static int sign( int x ) {
+ if ( x >= 0 ) {
+ return 1 ;
+ } else {
+ return -1 ;
+ }
+ }
+
+ /**
+ * For a long value x, this method returns +1L if x >= 0
+ * and -1L if x < 0.
+ *
+ * @author Albert Davidson Chou
+ * @param x the value, a long
+ * @return +1L or -1L, depending on the the sign of x
+ */
+ public static long sign( long x ) {
+ if ( x >= 0L ) {
+ return 1L ;
+ } else {
+ return -1L ;
+ }
+ }
+ /**
+ * Returns an exact representation of the
+ *
+ * Binomial Coefficient, "n choose k
",
+ * the number of k
-element subsets that can be selected from
+ * an n
-element set.
+ * + * Preconditions:
0 < k <= n
(otherwise
+ * 0 < k <= n
(otherwise
+ * IllegalArgumentException
is thrown)long
. The
+ * largest value of n
for which all coefficients are
+ * < Long.MAX_VALUE
is 66. If the computed value
+ * long
. The
+ * largest value of n
for which all coefficients are
+ * < Long.MAX_VALUE
is 66. If the computed value
+ * exceeds Long.MAX_VALUE
an ArithMeticException
+ *
is thrown.n choose k
+ */
public static long binomialCoefficient(int n, int k) {
if (n < k) {
throw new IllegalArgumentException
@@ -98,51 +198,51 @@ public class MathUtils {
if ((k == 1) || (k == n - 1)) {
return n;
}
-
+
long result = Math.round(binomialCoefficientDouble(n, k));
if (result == Long.MAX_VALUE) {
throw new ArithmeticException
("result too large to represent in a long integer");
}
- return result;
- }
-
+ return result;
+ }
+
/**
- * Returns a double
representation of the
- *
- * Binomial Coefficient, "n choose k
",
- * the number of k
-element subsets that can be selected from
+ * Returns a double
representation of the
+ *
+ * Binomial Coefficient, "n choose k
",
+ * the number of k
-element subsets that can be selected from
* an n
-element set.
* * Preconditions:
0 < k <= n
(otherwise
+ * 0 < k <= n
(otherwise
* IllegalArgumentException
is thrown)double
.
- * The largest value of n
for which all coefficients are
- * < Double.MAX_VALUE is 1029. If the computed value exceeds
+ * double
.
+ * The largest value of n
for which all coefficients are
+ * < Double.MAX_VALUE is 1029. If the computed value exceeds
* Double.MAX_VALUE, Double.POSITIVE_INFINITY is returnedn choose k
*/
- public static double binomialCoefficientDouble(int n, int k) {
- return Math.floor(Math.exp(binomialCoefficientLog(n, k)) + .5);
+ public static double binomialCoefficientDouble(int n, int k) {
+ return Math.floor(Math.exp(binomialCoefficientLog(n, k)) + .5);
}
-
+
/**
* Returns the natural log
of the
- *
- * Binomial Coefficient, "n choose k
",
- * the number of k
-element subsets that can be selected from
+ *
+ * Binomial Coefficient, "n choose k
",
+ * the number of k
-element subsets that can be selected from
* an n
-element set.
* * Preconditions:
0 < k <= n
(otherwise
+ * 0 < k <= n
(otherwise
* IllegalArgumentException
is thrown)n choose k
@@ -161,38 +261,38 @@ public class MathUtils {
}
if ((k == 1) || (k == n - 1)) {
return Math.log((double) n);
- }
- double logSum = 0;
-
+ }
+ double logSum = 0;
+
// n!/k!
for (int i = k + 1; i <= n; i++) {
logSum += Math.log((double) i);
}
-
+
// divide by (n-k)!
for (int i = 2; i <= n - k; i++) {
logSum -= Math.log((double) i);
}
-
+
return logSum;
}
-
+
/**
* Returns n
- *
- * Factorial, or n!
,
+ *
+ * Factorial, or n!
,
* the product of the numbers 1,...,n
.
* * Preconditions:
n > 0
(otherwise
+ * n > 0
(otherwise
* IllegalArgumentException
is thrown)long
. The
- * largest value of n
for which n!
- * < Long.MAX_VALUE is 20. If the computed value
+ * long
. The
+ * largest value of n
for which n!
+ * < Long.MAX_VALUE is 20. If the computed value
* exceeds Long.MAX_VALUE
an ArithMeticException
*
is thrown.n!
*/
@@ -202,25 +302,25 @@ public class MathUtils {
throw new ArithmeticException
("result too large to represent in a long integer");
}
- return result;
+ return result;
}
-
+
/**
* Returns n
- *
- * Factorial, or n!
,
- * the product of the numbers 1,...,n
, as as
+ *
+ * Factorial, or n!
,
+ * the product of the numbers 1,...,n
, as as
* double
.
* * Preconditions:
n > 0
(otherwise
+ * n > 0
(otherwise
* IllegalArgumentException
is thrown)double
. The
- * largest value of n
for which n!
- * < Double.MAX_VALUE is 170. If the computed value exceeds
+ * double
. The
+ * largest value of n
for which n!
+ * < Double.MAX_VALUE is 170. If the computed value exceeds
* Double.MAX_VALUE, Double.POSITIVE_INFINITY is returnedn!
*/
@@ -229,21 +329,21 @@ public class MathUtils {
throw new IllegalArgumentException
("must have n > 0 for n!");
}
- return Math.floor(Math.exp(factorialLog(n)) + 0.5);
+ return Math.floor(Math.exp(factorialLog(n)) + 0.5);
}
-
+
/**
* Returns the natural log
of n
- *
- * Factorial, or n!
,
- * the product of the numbers 1,...,n
, as as
+ *
+ * Factorial, or n!
,
+ * the product of the numbers 1,...,n
, as as
* double
.
* * Preconditions:
n > 0
(otherwise
+ * n > 0
(otherwise
* IllegalArgumentException
is thrown)n!
*/
@@ -255,7 +355,7 @@ public class MathUtils {
double logSum = 0;
for (int i = 2; i <= n; i++) {
logSum += Math.log((double) i);
- }
+ }
return logSum;
- }
+ }
}
\ No newline at end of file
diff --git a/src/test/org/apache/commons/math/MathUtilsTest.java b/src/test/org/apache/commons/math/MathUtilsTest.java
index 314d71ad7..34922250c 100644
--- a/src/test/org/apache/commons/math/MathUtilsTest.java
+++ b/src/test/org/apache/commons/math/MathUtilsTest.java
@@ -14,7 +14,7 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
- * distribution.
+ * distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
@@ -62,16 +62,16 @@ import junit.framework.AssertionFailedError;
* Test cases for the MathUtils class.
*
* @author Phil Steitz
- * @version $Revision: 1.1 $ $Date: 2003/06/04 02:31:14 $
+ * @version $Revision: 1.2 $ $Date: 2003/06/06 03:07:39 $
*/
public final class MathUtilsTest extends TestCase {
public MathUtilsTest(String name) {
super(name);
- }
-
- public void setUp() {
+ }
+
+ public void setUp() {
}
public static Test suite() {
@@ -157,10 +157,10 @@ public final class MathUtilsTest extends TestCase {
;
}
double x = MathUtils.binomialCoefficientDouble(1030,515);
- assertTrue("expecting infinite binomial coefficient",
+ assertTrue("expecting infinite binomial coefficient",
Double.isInfinite(x));
}
-
+
public void testFactorial() {
for (int i = 1; i < 10; i++) {
assertEquals(i + "! ",factorial(i),MathUtils.factorial(i));
@@ -170,7 +170,7 @@ public final class MathUtilsTest extends TestCase {
MathUtils.factorialLog(i),10E-12);
}
}
-
+
public void testFactorialFail() {
try {
long x = MathUtils.factorial(0);
@@ -196,26 +196,26 @@ public final class MathUtilsTest extends TestCase {
} catch (ArithmeticException ex) {
;
}
- assertTrue("expecting infinite factorial value",
+ assertTrue("expecting infinite factorial value",
Double.isInfinite(MathUtils.factorialDouble(171)));
-
+
}
-
-
- /**
+
+
+ /**
* Exact recursive implementation to test against
*/
- private long binomialCoefficient(int n, int k) {
+ private long binomialCoefficient(int n, int k) {
if ((n == k) || (k == 0)) {
return 1;
}
if ((k == 1) || (k == n - 1)) {
return n;
}
- return binomialCoefficient(n - 1, k - 1) +
+ return binomialCoefficient(n - 1, k - 1) +
binomialCoefficient(n - 1, k);
- }
-
+ }
+
/**
* Finds the largest values of n for which binomialCoefficient and
* binomialCoefficientDouble will return values that fit in a long, double,
@@ -225,7 +225,7 @@ public final class MathUtilsTest extends TestCase {
findBinomialLimits();
}
*/
-
+
private void findBinomialLimits() {
/**
* will kick out 66 as the limit for long
@@ -241,8 +241,8 @@ public final class MathUtilsTest extends TestCase {
("largest n for binomialCoefficient = " + (test - 1) );
}
test++;
- }
-
+ }
+
/**
* will kick out 1029 as the limit for double
*/
@@ -256,19 +256,19 @@ public final class MathUtilsTest extends TestCase {
("largest n for binomialCoefficientD = " + (test - 1) );
}
test++;
- }
+ }
}
-
+
/**
* Finds the largest values of n for which factiorial and
* factorialDouble will return values that fit in a long, double,
* resp. Remove comments around test below to get this in test-report
-
+
public void testFactiorialLimits() {
findFactorialLimits();
}
*/
-
+
private void findFactorialLimits() {
/**
* will kick out 20 as the limit for long
@@ -284,8 +284,8 @@ public final class MathUtilsTest extends TestCase {
("largest n for factorial = " + (test - 1) );
}
test++;
- }
-
+ }
+
/**
* will kick out 170 as the limit for double
*/
@@ -299,21 +299,56 @@ public final class MathUtilsTest extends TestCase {
("largest n for factorialDouble = " + (test - 1) );
}
test++;
- }
+ }
}
-
-
- /**
+
+
+ /**
* Exact direct multiplication implementation to test against
*/
- private long factorial(int n) {
+ private long factorial(int n) {
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
- }
-
-
+ }
+
+ public void testSignDouble() {
+ double delta = 0.0 ;
+ assertEquals( 1.0, MathUtils.sign( 2.0 ), delta ) ;
+ assertEquals( -1.0, MathUtils.sign( -2.0 ), delta ) ;
+ }
+
+
+ public void testSignFloat() {
+ float delta = 0.0F ;
+ assertEquals( 1.0F, MathUtils.sign( 2.0F ), delta ) ;
+ assertEquals( -1.0F, MathUtils.sign( -2.0F ), delta ) ;
+ }
+
+
+ public void testSignByte() {
+ assertEquals( (byte)1, MathUtils.sign( (byte)2 ) ) ;
+ assertEquals( (byte)(-1), MathUtils.sign( (byte)(-2) ) ) ;
+ }
+
+
+ public void testSignShort() {
+ assertEquals( (short)1, MathUtils.sign( (short)2 ) ) ;
+ assertEquals( (short)(-1), MathUtils.sign( (short)(-2) ) ) ;
+ }
+
+
+ public void testSignInt() {
+ assertEquals( (int)1, MathUtils.sign( (int)(2) ) ) ;
+ assertEquals( (int)(-1), MathUtils.sign( (int)(-2) ) ) ;
+ }
+
+
+ public void testSignLong() {
+ assertEquals( 1L, MathUtils.sign( 2L ) ) ;
+ assertEquals( -1L, MathUtils.sign( -2L ) ) ;
+ }
}
\ No newline at end of file