SpecialFunctions.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 4k
Category:

Windows Develop

Development Platform:

Java

  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.  *    SpecialFunctions.java
  18.  *    Copyright (C) 1999 Eibe Frank
  19.  *
  20.  */
  21. package weka.core;
  22. import java.lang.Math;
  23. /**
  24.  * Class implementing some mathematical functions.
  25.  *
  26.  * @author Eibe Frank (eibe@cs.waikato.ac.nz)
  27.  * @version $Revision: 1.4 $
  28.  */
  29. public final class SpecialFunctions {
  30.   /** Some constants */
  31.   private static double log2 = Math.log(2);
  32.   private static double[] cofLogGamma = {76.18009172947146,-86.50532032941677,
  33.  24.01409824083091,-1.231739572450155,
  34.  0.1208650973866179e-2,-0.5395239384953e-5};
  35.   /**
  36.    * Returns natural logarithm of factorial using gamma function.
  37.    *
  38.    * @param x the value
  39.    * @return natural logarithm of factorial
  40.    */
  41.   public static double lnFactorial(double x){
  42.     return lnGamma(x+1);
  43.   }
  44.   /**
  45.    * Returns natural logarithm of gamma function. Converted to java
  46.    * from Numerical Recipes in C.
  47.    *
  48.    * @param x the value
  49.    * @return natural logarithm of gamma function
  50.    */
  51.   public static double lnGamma(double x){
  52.     
  53.     double y,tmp,ser;
  54.     int j;
  55.     
  56.     y=x;
  57.     tmp=x+5.5;
  58.     tmp -= (x+0.5)*Math.log(tmp);
  59.     ser=1.000000000190015;
  60.     for (j=0;j<=5;j++) {
  61.       ser += cofLogGamma[j]/++y;
  62.     }
  63.     return -tmp+Math.log(2.5066282746310005*ser/x);
  64.   }
  65.   /**
  66.    * Returns base 2 logarithm of binomial coefficient using gamma function.
  67.    *
  68.    * @param a upper part of binomial coefficient
  69.    * @param b lower part
  70.    * @return the base 2 logarithm of the binominal coefficient a over b
  71.    */
  72.   public static double log2Binomial(double a, double b) throws ArithmeticException{
  73.     
  74.     if (Utils.gr(b,a)) {
  75.       throw new ArithmeticException("Can't compute binomial coefficient.");
  76.     }
  77.     return (lnFactorial(a)-lnFactorial(b)-lnFactorial(a-b))/log2;
  78.   }
  79.   /**
  80.    * Returns base 2 logarithm of multinomial using gamma function.
  81.    *
  82.    * @param a upper part of multinomial coefficient
  83.    * @param bs lower part
  84.    * @return multinomial coefficient of a over the bs
  85.    */
  86.   public static double log2Multinomial(double a, double[] bs)
  87.        throws ArithmeticException{
  88.     
  89.     double sum = 0;
  90.     int i;
  91.     
  92.     for (i=0;i<bs.length;i++) {
  93.       if (Utils.gr(bs[i],a)) {
  94. throw 
  95.   new ArithmeticException("Can't compute multinomial coefficient.");
  96.       } else {
  97. sum = sum+lnFactorial(bs[i]);
  98.       }
  99.     }
  100.     return (lnFactorial(a)-sum)/log2;
  101.   }
  102.   /**
  103.    * Main method for testing this class.
  104.    */
  105.   public static void main(String[] ops) {
  106.     double[] doubles = {1, 2, 3};
  107.     System.out.println("6!: " + Math.exp(SpecialFunctions.lnFactorial(6)));
  108.     System.out.println("lnGamma(6): "+ SpecialFunctions.lnGamma(6));
  109.     System.out.println("Binomial 6 over 2: " +
  110.        Math.pow(2, SpecialFunctions.log2Binomial(6, 2)));
  111.     System.out.println("Multinomial 6 over 1, 2, 3: " +
  112.        Math.pow(2, SpecialFunctions.log2Multinomial(6, doubles)));
  113.   }    
  114. }