Java Gamma gammaOfArgOn2Plus1IncludeDivisor(int d, double divisor)

Here you can find the source of gammaOfArgOn2Plus1IncludeDivisor(int d, double divisor)

Description

Computes gamma(d/2 + 1)/divisor See http://en.wikipedia.org/wiki/Gamma_function for description of the analytical result for d odd.

License

Open Source License

Parameter

Parameter Description
d a parameter
divisor a parameter

Declaration

public static double gammaOfArgOn2Plus1IncludeDivisor(int d, double divisor) 

Method Source Code

//package com.java2s;
/*//from w  w  w. j  a va 2 s  . c om
 *  Java Information Dynamics Toolkit (JIDT)
 *  Copyright (C) 2012, Joseph T. Lizier
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Computes gamma(d/2 + 1)/divisor
     * See http://en.wikipedia.org/wiki/Gamma_function
     *  for description of the analytical result for d odd.
     * For d even, we have gamma of an integer, which is equal to
     *  (d/2)!
     * 
     * We include the divisor here since the gamma(d/2+1) hits Inf with d just over 300.
     * So if there is anything the result would have been divided by, we include it here,
     *  thus extending the range of d the function is suitable for. 
     * 
     * @param d
     * @param divisor
     * @return
     */
    public static double gammaOfArgOn2Plus1IncludeDivisor(int d, double divisor) {
        if (d % 2 == 0) {
            // d even
            return factorialAsDoubleIncludeDivisor(d / 2, divisor);
        } else {
            // d odd
            return doubleFactorialAsDoublewithDivisor(d,
                    divisor * Math.pow(2, ((double) (d + 1)) / 2.0) / Math.sqrt(Math.PI));
        }
    }

    /**
     * Computes n! as a double (to provide extended range over a long).
     * 
     * We include the divisor here since n! hits with n at about 340.
     * So if there is anything the result would have been divided by, we include it here,
     *  thus extending the range of n the function is suitable for. 
     * 
     * @param n
     * @param divisor
     * @return
     */
    public static double factorialAsDoubleIncludeDivisor(int n, double divisor) {
        double result = 1.0 / divisor;
        for (int i = 1; i <= n; i++) {
            result *= (double) i;
        }
        return result;
    }

    /**
     * n!!
     * see http://en.wikipedia.org/wiki/Double_factorial#Double_factorial
     * 
     * We include the divisor here since the gamma(d/2+1) hits Inf with d just over 300.
     * So if there is anything the result would have been divided by, we include it here,
     *  thus extending the range of d the function is suitable for. 
     * 
     * @param n
     * @param divisor
     * @return
     */
    public static double doubleFactorialAsDoublewithDivisor(int n, double divisor) {
        double result = 1.0 / divisor;
        int startValue;
        if (n % 2 == 0) {
            // n even
            startValue = 2;
        } else {
            // n odd
            startValue = 3;
        }
        for (int i = startValue; i <= n; i += 2) {
            result *= (double) i;
        }
        return result;
    }
}

Related

  1. gammaCorrection(final float gamma, final float x)
  2. gammaFunction(double in)
  3. gammaFwd(double lc)
  4. gammaln(double x)
  5. gammaOfArgOn2Plus1(int d)
  6. gammaPdf(double x, double a)
  7. gammaRand(double a)