psteitz 2004/10/07 22:53:19
Modified: math/src/java/org/apache/commons/math/special Gamma.java
Log:
Improved performance of logGamma.
Pr #31522
Submitted by: Ken Geis
Reviewd by: Phil Steitz
Revision Changes Path
1.22 +7 -4
jakarta-commons/math/src/java/org/apache/commons/math/special/Gamma.java
Index: Gamma.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/special/Gamma.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- Gamma.java 23 Jun 2004 16:26:17 -0000 1.21
+++ Gamma.java 8 Oct 2004 05:53:18 -0000 1.22
@@ -52,6 +52,9 @@
.36899182659531622704e-5,
};
+ /** Avoid repeated computation of log of 2 PI in logGamma */
+ private static final double HALF_LOG_2_PI = 0.5 * Math.log(2.0 * Math.PI);
+
/**
* Default constructor. Prohibit instantiation.
@@ -84,16 +87,16 @@
ret = Double.NaN;
} else {
double g = 607.0 / 128.0;
-
+
double sum = 0.0;
- for (int i = 1; i < lanczos.length; ++i) {
+ for (int i = lanczos.length - 1; i > 0; --i) {
sum = sum + (lanczos[i] / (x + i));
}
sum = sum + lanczos[0];
double tmp = x + g + .5;
ret = ((x + .5) * Math.log(tmp)) - tmp +
- (.5 * Math.log(2.0 * Math.PI)) + Math.log(sum) - Math.log(x);
+ HALF_LOG_2_PI + Math.log(sum / x);
}
return ret;
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]