source: src/main/java/weka/core/SpecialFunctions.java @ 28

Last change on this file since 28 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 3.0 KB
Line 
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 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.core;
24
25import java.lang.Math;
26
27/**
28 * Class implementing some mathematical functions.
29 *
30 * @author Eibe Frank (eibe@cs.waikato.ac.nz)
31 * @version $Revision: 5953 $
32 */
33public final class SpecialFunctions
34  implements RevisionHandler {
35
36  /** Some constants */
37  private static double log2 = Math.log(2);
38
39  /**
40   * Returns natural logarithm of factorial using gamma function.
41   *
42   * @param x the value
43   * @return natural logarithm of factorial
44   */
45  public static double lnFactorial(double x){
46
47    return Statistics.lnGamma(x+1);
48  }
49
50  /**
51   * Returns base 2 logarithm of binomial coefficient using gamma function.
52   *
53   * @param a upper part of binomial coefficient
54   * @param b lower part
55   * @return the base 2 logarithm of the binominal coefficient a over b
56   */
57  public static double log2Binomial(double a, double b) {
58   
59    if (Utils.gr(b,a)) {
60      throw new ArithmeticException("Can't compute binomial coefficient.");
61    }
62    return (lnFactorial(a)-lnFactorial(b)-lnFactorial(a-b))/log2;
63  }
64
65  /**
66   * Returns base 2 logarithm of multinomial using gamma function.
67   *
68   * @param a upper part of multinomial coefficient
69   * @param bs lower part
70   * @return multinomial coefficient of a over the bs
71   */
72  public static double log2Multinomial(double a, double[] bs)
73       {
74   
75    double sum = 0;
76    int i;
77   
78    for (i=0;i<bs.length;i++) {
79      if (Utils.gr(bs[i],a)) {
80        throw 
81          new ArithmeticException("Can't compute multinomial coefficient.");
82      } else {
83        sum = sum+lnFactorial(bs[i]);
84      }
85    }
86    return (lnFactorial(a)-sum)/log2;
87  }
88 
89  /**
90   * Returns the revision string.
91   *
92   * @return            the revision
93   */
94  public String getRevision() {
95    return RevisionUtils.extract("$Revision: 5953 $");
96  }
97
98  /**
99   * Main method for testing this class.
100   */
101  public static void main(String[] ops) {
102
103    double[] doubles = {1, 2, 3};
104
105    System.out.println("6!: " + Math.exp(SpecialFunctions.lnFactorial(6)));
106    System.out.println("Binomial 6 over 2: " +
107                       Math.pow(2, SpecialFunctions.log2Binomial(6, 2)));
108    System.out.println("Multinomial 6 over 1, 2, 3: " +
109                       Math.pow(2, SpecialFunctions.log2Multinomial(6, doubles)));
110  }   
111}
Note: See TracBrowser for help on using the repository browser.