Java Digamma digamma(double x)

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

Description

Stolen from Radford Neal's fbm package.

License

Open Source License

Declaration

public static double digamma(double x) 

Method Source Code

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

public class Main {
    /**/*from w  w  w . j a  va 2  s  .  c  o  m*/
    * Stolen from Radford Neal's fbm package.
    * digamma(x) is defined as (d/dx) log Gamma(x).  It is computed here
    * using an asymptotic expansion when x>5.  For x<=5, the recurrence
    * relation digamma(x) = digamma(x+1) - 1/x is used repeatedly.  See
    * Venables & Ripley, Modern Applied Statistics with S-Plus, pp. 151-152.
    * COMPUTE THE DIGAMMA FUNCTION.  Returns -inf if the argument is an integer
    * less than or equal to zero.
    */
    public static double digamma(double x) {
        assert x > 0 : x;
        double r, f, t;
        r = 0;
        while (x <= 5) {
            r -= 1 / x;
            x += 1;
        }
        f = 1 / (x * x);
        t = f * (-1 / 12.0 + f * (1 / 120.0 + f * (-1 / 252.0
                + f * (1 / 240.0 + f * (-1 / 132.0 + f * (691 / 32760.0 + f * (-1 / 12.0 + f * 3617 / 8160.0)))))));
        return r + Math.log(x) - 0.5 / x + t;
    }
}

Related

  1. digamma(double x)
  2. digamma(double x)
  3. digammaByDefinition(int d)
  4. digammaDiff(double x, int d)