Java Gamma gamma(double x)

Here you can find the source of gamma(double x)

Description

gamma

License

Open Source License

Declaration

public static double gamma(double x) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static double lgfGamma = 5.0;
    private static double[] lgfCoeff = { 1.000000000190015, 76.18009172947146, -86.50532032941677,
            24.01409824083091, -1.231739572450155, 0.1208650973866179E-2, -0.5395239384953E-5 };
    private static int lgfN = 6;

    public static double gamma(double x) {

        double xcopy = x;
        double first = x + lgfGamma + 0.5;
        double second = lgfCoeff[0];
        double fg = 0.0D;

        if (x >= 0.0) {
            if (x >= 1.0D && x - (int) x == 0.0D) {
                fg = factorial((int) x) / x;
            } else {
                first = Math.pow(first, x + 0.5) * Math.exp(-first);
                for (int i = 1; i <= lgfN; i++)
                    second += lgfCoeff[i] / ++xcopy;
                fg = first * Math.sqrt(2.0 * Math.PI) * second / x;
            }/*from  w  ww .  ja v a  2s.  com*/
        } else {
            fg = -Math.PI / (x * gamma(-x) * Math.sin(Math.PI * x));
        }

        return fg;
    }

    public static double factorial(int n) {
        if (n < 0 || (n - (int) n) != 0)
            throw new IllegalArgumentException(
                    "\nn must be a positive integer\nIs a Gamma funtion [gamma(x)] more appropriate?");
        double f = 1.0D;
        for (int i = 1; i <= n; i++)
            f *= i;
        return f;
    }
}

Related

  1. gamma(double x)
  2. gamma(double x)
  3. gamma(double x)
  4. Gamma(double z)
  5. gamma(int alpha)
  6. gamma(int rgb)
  7. gammaCdf(double a, double x)