Returns Gamma function of x. - Java java.lang

Java examples for java.lang:Math Function

Description

Returns Gamma function of x.

Demo Code


//package com.java2s;

public class Main {
    /**//from w  ww. java  2  s  . co m
     * Returns Gamma function of x.
     * Algorithm for logGamma(x) is from Numerical recipes 6.1
     * 
     * @param x
     * @return Gamma(x)
     */
    public static double gamma(double x) {
        return Math.floor(Math.exp(logGamma(x)) * 10e5 + 0.5) / 10e5;
    }

    /**
     * Returns natural logarithm of Gamma function of x
     * Algorithm is from Numerical recipes 6.1
     * 
     * @param x
     * @return log(Gamma(x))
     */
    public static double logGamma(double x) {
        double tmp = (x - 0.5) * Math.log(x + 4.5) - (x + 4.5);
        double ser = 1.0 + 76.18009173 / (x + 0) - 86.50532033 / (x + 1)
                + 24.01409822 / (x + 2) - 1.231739516 / (x + 3)
                + 0.00120858003 / (x + 4) - 0.00000536382 / (x + 5);
        return tmp + Math.log(ser * Math.sqrt(2 * Math.PI));

        /*
         * TODO zaokrouhlovat
         */
    }
}

Related Tutorials