tobrien 2003/06/18 13:02:27
Modified: math/src/java/org/apache/commons/math/special Beta.java
Gamma.java
math/src/test/org/apache/commons/math/special GammaTest.java
Log:
1. Gamma.logGamma was expecting epsilon and maxIterations but these parameters
were not being referenced in that function. These parameters has been removed.
2. Also, the Lanczos coefficients are now a private static member variable of
the Gamma class.
Revision Changes Path
1.4 +2 -2
jakarta-commons-sandbox/math/src/java/org/apache/commons/math/special/Beta.java
Index: Beta.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/special/Beta.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Beta.java 14 Jun 2003 04:17:48 -0000 1.3
+++ Beta.java 18 Jun 2003 20:02:27 -0000 1.4
@@ -223,8 +223,8 @@
if (Double.isNaN(a) || Double.isNaN(b) || (a <= 0.0) || (b <= 0.0)) {
ret = Double.NaN;
} else {
- ret = Gamma.logGamma(a, epsilon, maxIterations) + Gamma.logGamma(b,
epsilon, maxIterations)
- - Gamma.logGamma(a + b, epsilon, maxIterations);
+ ret = Gamma.logGamma(a) + Gamma.logGamma(b)
+ - Gamma.logGamma(a + b);
}
return ret;
1.6 +37 -26
jakarta-commons-sandbox/math/src/java/org/apache/commons/math/special/Gamma.java
Index: Gamma.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/special/Gamma.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Gamma.java 16 Jun 2003 20:13:41 -0000 1.5
+++ Gamma.java 18 Jun 2003 20:02:27 -0000 1.6
@@ -64,6 +64,27 @@
public class Gamma {
/** Maximum allowed numerical error. */
private static final double DEFAULT_EPSILON = 10e-9;
+
+ /** Lanczos coefficients */
+ private static double[] lanczos =
+ {
+ 0.99999999999999709182,
+ 57.156235665862923517,
+ -59.597960355475491248,
+ 14.136097974741747174,
+ -0.49191381609762019978,
+ .33994649984811888699e-4,
+ .46523628927048575665e-4,
+ -.98374475304879564677e-4,
+ .15808870322491248884e-3,
+ -.21026444172410488319e-3,
+ .21743961811521264320e-3,
+ -.16431810653676389022e-3,
+ .84418223983852743293e-4,
+ -.26190838401581408670e-4,
+ .36899182659531622704e-5,
+ };
+
/**
* Default constructor. Prohibit instantiation.
@@ -108,9 +129,16 @@
*
* @param a ???
* @param x ???
+ * @param epsilon When the absolute value of the nth item in the
+ * series is less than epsilon the approximation ceases
+ * to calculate further elements in the series.
+ * @param maxIterations Maximum number of "iterations" to complete.
* @return the regularized gamma function P(a, x)
*/
- public static double regularizedGammaP(double a, double x, double epsilon, int
maxIterations) {
+ public static double regularizedGammaP(double a,
+ double x,
+ double epsilon,
+ int maxIterations) {
double ret;
if (Double.isNaN(a) || Double.isNaN(x) || (a <= 0.0) || (x < 0.0)) {
@@ -134,7 +162,10 @@
throw new ConvergenceException(
"maximum number of iterations reached");
} else {
- ret = Math.exp(-x + (a * Math.log(x)) - logGamma(a, epsilon,
maxIterations)) * sum;
+ ret = Math.exp(-x +
+ (a * Math.log(x)) -
+ logGamma(a))
+ * sum;
}
}
@@ -162,7 +193,7 @@
* @param x ???
* @return log(Γ(x))
*/
- public static double logGamma(double x, double epsilon, int maxIterations) {
+ public static double logGamma(double x) {
double ret;
if (Double.isNaN(x) || (x <= 0.0)) {
@@ -170,31 +201,11 @@
} else {
double g = 607.0 / 128.0;
- // Lanczos coefficients
- double[] c =
- {
- 0.99999999999999709182,
- 57.156235665862923517,
- -59.597960355475491248,
- 14.136097974741747174,
- -0.49191381609762019978,
- .33994649984811888699e-4,
- .46523628927048575665e-4,
- -.98374475304879564677e-4,
- .15808870322491248884e-3,
- -.21026444172410488319e-3,
- .21743961811521264320e-3,
- -.16431810653676389022e-3,
- .84418223983852743293e-4,
- -.26190838401581408670e-4,
- .36899182659531622704e-5,
- };
-
double sum = 0.0;
- for (int i = 1; i < c.length; ++i) {
- sum = sum + (c[i] / (x + i));
+ for (int i = 1; i < lanczos.length; ++i) {
+ sum = sum + (lanczos[i] / (x + i));
}
- sum = sum + c[0];
+ sum = sum + lanczos[0];
double tmp = x + g + .5;
ret = ((x + .5) * Math.log(tmp)) - tmp
1.2 +1 -1
jakarta-commons-sandbox/math/src/test/org/apache/commons/math/special/GammaTest.java
Index: GammaTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/math/src/test/org/apache/commons/math/special/GammaTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- GammaTest.java 16 Jun 2003 20:13:43 -0000 1.1
+++ GammaTest.java 18 Jun 2003 20:02:27 -0000 1.2
@@ -75,7 +75,7 @@
}
private void testLogGamma(double expected, double x) {
- double actual = Gamma.logGamma(x, 10e-5, Integer.MAX_VALUE);
+ double actual = Gamma.logGamma(x);
TestUtils.assertEquals(expected, actual, 10e-5);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]