1 | /*
|
---|
2 | * This program is free software; you can redistribute it and/or modify
|
---|
3 | * it under the terms of the GNU General Public License as published by
|
---|
4 | * the Free Software Foundation; either version 2 of the License, or
|
---|
5 | * (at your option) any later version.
|
---|
6 | *
|
---|
7 | * This program is distributed in the hope that it will be useful,
|
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
10 | * GNU General Public License for more details.
|
---|
11 | *
|
---|
12 | * You should have received a copy of the GNU General Public License
|
---|
13 | * along with this program; if not, write to the Free Software
|
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
15 | */
|
---|
16 |
|
---|
17 | /*
|
---|
18 | * SpecialFunctions.java
|
---|
19 | * Copyright (C) 1999 Eibe Frank
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | package weka.core;
|
---|
24 |
|
---|
25 | import java.lang.Math;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Class implementing some mathematical functions.
|
---|
29 | *
|
---|
30 | * @author Eibe Frank ([email protected])
|
---|
31 | * @version $Revision: 8815 $
|
---|
32 | */
|
---|
33 | public final class SpecialFunctions {
|
---|
34 |
|
---|
35 | /** Some constants */
|
---|
36 | private static double log2 = Math.log(2);
|
---|
37 | private static double[] cofLogGamma = {76.18009172947146,-86.50532032941677,
|
---|
38 | 24.01409824083091,-1.231739572450155,
|
---|
39 | 0.1208650973866179e-2,-0.5395239384953e-5};
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Returns natural logarithm of factorial using gamma function.
|
---|
43 | *
|
---|
44 | * @param x the value
|
---|
45 | * @return natural logarithm of factorial
|
---|
46 | */
|
---|
47 | public static double lnFactorial(double x){
|
---|
48 |
|
---|
49 | return lnGamma(x+1);
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Returns natural logarithm of gamma function. Converted to java
|
---|
54 | * from Numerical Recipes in C.
|
---|
55 | *
|
---|
56 | * @param x the value
|
---|
57 | * @return natural logarithm of gamma function
|
---|
58 | */
|
---|
59 | public static double lnGamma(double x){
|
---|
60 |
|
---|
61 | double y,tmp,ser;
|
---|
62 | int j;
|
---|
63 |
|
---|
64 | y=x;
|
---|
65 | tmp=x+5.5;
|
---|
66 | tmp -= (x+0.5)*Math.log(tmp);
|
---|
67 | ser=1.000000000190015;
|
---|
68 | for (j=0;j<=5;j++) {
|
---|
69 | ser += cofLogGamma[j]/++y;
|
---|
70 | }
|
---|
71 | return -tmp+Math.log(2.5066282746310005*ser/x);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Returns base 2 logarithm of binomial coefficient using gamma function.
|
---|
76 | *
|
---|
77 | * @param a upper part of binomial coefficient
|
---|
78 | * @param b lower part
|
---|
79 | * @return the base 2 logarithm of the binominal coefficient a over b
|
---|
80 | */
|
---|
81 | public static double log2Binomial(double a, double b) throws ArithmeticException{
|
---|
82 |
|
---|
83 | if (Utils.gr(b,a)) {
|
---|
84 | throw new ArithmeticException("Can't compute binomial coefficient.");
|
---|
85 | }
|
---|
86 | return (lnFactorial(a)-lnFactorial(b)-lnFactorial(a-b))/log2;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Returns base 2 logarithm of multinomial using gamma function.
|
---|
91 | *
|
---|
92 | * @param a upper part of multinomial coefficient
|
---|
93 | * @param bs lower part
|
---|
94 | * @return multinomial coefficient of a over the bs
|
---|
95 | */
|
---|
96 | public static double log2Multinomial(double a, double[] bs)
|
---|
97 | throws ArithmeticException{
|
---|
98 |
|
---|
99 | double sum = 0;
|
---|
100 | int i;
|
---|
101 |
|
---|
102 | for (i=0;i<bs.length;i++) {
|
---|
103 | if (Utils.gr(bs[i],a)) {
|
---|
104 | throw
|
---|
105 | new ArithmeticException("Can't compute multinomial coefficient.");
|
---|
106 | } else {
|
---|
107 | sum = sum+lnFactorial(bs[i]);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | return (lnFactorial(a)-sum)/log2;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Main method for testing this class.
|
---|
115 | */
|
---|
116 | public static void main(String[] ops) {
|
---|
117 |
|
---|
118 | double[] doubles = {1, 2, 3};
|
---|
119 |
|
---|
120 | System.out.println("6!: " + Math.exp(SpecialFunctions.lnFactorial(6)));
|
---|
121 | System.out.println("lnGamma(6): "+ SpecialFunctions.lnGamma(6));
|
---|
122 | System.out.println("Binomial 6 over 2: " +
|
---|
123 | Math.pow(2, SpecialFunctions.log2Binomial(6, 2)));
|
---|
124 | System.out.println("Multinomial 6 over 1, 2, 3: " +
|
---|
125 | Math.pow(2, SpecialFunctions.log2Multinomial(6, doubles)));
|
---|
126 | }
|
---|
127 | }
|
---|