Java Gamma gammaOfArgOn2Plus1(int d)

Here you can find the source of gammaOfArgOn2Plus1(int d)

Description

Computes gamma(d/2 + 1) 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

Declaration

public static double gammaOfArgOn2Plus1(int d) 

Method Source Code

//package com.java2s;
/*//  w w  w  .  j  a v  a  2  s  .  c  o m
 *  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)
     * 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)!
     * 
     * @param d
     * @return
     */
    public static double gammaOfArgOn2Plus1(int d) {
        if (d % 2 == 0) {
            // d even
            return factorialAsDouble(d / 2);
        } else {
            // d odd
            return Math.sqrt(Math.PI) * (double) doubleFactorialAsDouble(d)
                    / (double) Math.pow(2, ((double) (d + 1)) / 2.0);
        }
    }

    public static double factorialAsDouble(int n) {
        double result = 1;
        for (int i = 1; i <= n; i++) {
            result *= (double) i;
        }
        return result;
    }

    /**
     * n!!
     * see http://en.wikipedia.org/wiki/Double_factorial#Double_factorial
     * 
     * @param n
     * @return
     */
    public static double doubleFactorialAsDouble(int n) {
        double result = 1.0;
        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. gammaCdf(double a, double x)
  2. gammaCorrection(final float gamma, final float x)
  3. gammaFunction(double in)
  4. gammaFwd(double lc)
  5. gammaln(double x)
  6. gammaOfArgOn2Plus1IncludeDivisor(int d, double divisor)
  7. gammaPdf(double x, double a)
  8. gammaRand(double a)