01: /*
02: * Created on 12-Apr-2004
03: */
04: package uk.org.ponder.util;
05:
06: /**
07: * @author Bosmon
08: *
09: * The class
10: */
11: public class SpecialFunctions {
12: public static int sgn(double arg) {
13: return arg > 0 ? 1 : arg < 0 ? -1 : 0;
14: }
15:
16: private static double[] gammalncof = new double[] {
17: 76.18009172947146, -86.50532032941677, 24.01409824083091,
18: -1.231739572450155, 0.1208650973866179e-2,
19: -0.5395239384953e-5 };
20:
21: double gammaln(double xx) {
22: double y = xx, x = xx;
23: double tmp = x + 5.5;
24: tmp -= (x + 0.5) * Math.log(tmp);
25: double ser = 1.000000000190015;
26: for (int j = 0; j <= 5; j++) {
27: ser += gammalncof[j] / ++y;
28: }
29: return -tmp + Math.log(2.5066282746310005 * ser / x);
30: }
31:
32: //Returns ln(n!).
33: double factln(int n) {
34: if (n <= 1)
35: return 0.0;
36: return gammaln(n + 1.0);
37: }
38:
39: //Returns the binomial coefficient (n k) as a floating-point number.
40: double bico(int n, int k) {
41: return Math.floor(0.5 + Math.exp(factln(n) - factln(k)
42: - factln(n - k)));
43:
44: }
45: }
|