SpecialFunctions.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 3k
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.5 $
  28.  */
  29. public final class SpecialFunctions {
  30.   /** Some constants */
  31.   private static double log2 = Math.log(2);
  32.   /**
  33.    * Returns natural logarithm of factorial using gamma function.
  34.    *
  35.    * @param x the value
  36.    * @return natural logarithm of factorial
  37.    */
  38.   public static double lnFactorial(double x){
  39.     return Statistics.lnGamma(x+1);
  40.   }
  41.   /**
  42.    * Returns base 2 logarithm of binomial coefficient using gamma function.
  43.    *
  44.    * @param a upper part of binomial coefficient
  45.    * @param b lower part
  46.    * @return the base 2 logarithm of the binominal coefficient a over b
  47.    */
  48.   public static double log2Binomial(double a, double b) throws ArithmeticException{
  49.     
  50.     if (Utils.gr(b,a)) {
  51.       throw new ArithmeticException("Can't compute binomial coefficient.");
  52.     }
  53.     return (lnFactorial(a)-lnFactorial(b)-lnFactorial(a-b))/log2;
  54.   }
  55.   /**
  56.    * Returns base 2 logarithm of multinomial using gamma function.
  57.    *
  58.    * @param a upper part of multinomial coefficient
  59.    * @param bs lower part
  60.    * @return multinomial coefficient of a over the bs
  61.    */
  62.   public static double log2Multinomial(double a, double[] bs)
  63.        throws ArithmeticException{
  64.     
  65.     double sum = 0;
  66.     int i;
  67.     
  68.     for (i=0;i<bs.length;i++) {
  69.       if (Utils.gr(bs[i],a)) {
  70. throw 
  71.   new ArithmeticException("Can't compute multinomial coefficient.");
  72.       } else {
  73. sum = sum+lnFactorial(bs[i]);
  74.       }
  75.     }
  76.     return (lnFactorial(a)-sum)/log2;
  77.   }
  78.   /**
  79.    * Main method for testing this class.
  80.    */
  81.   public static void main(String[] ops) {
  82.     double[] doubles = {1, 2, 3};
  83.     System.out.println("6!: " + Math.exp(SpecialFunctions.lnFactorial(6)));
  84.     System.out.println("Binomial 6 over 2: " +
  85.        Math.pow(2, SpecialFunctions.log2Binomial(6, 2)));
  86.     System.out.println("Multinomial 6 over 1, 2, 3: " +
  87.        Math.pow(2, SpecialFunctions.log2Multinomial(6, doubles)));
  88.   }    
  89. }