public static double logGamma ( double x ) { double ret ; if ( Double . isNaN ( x ) || ( x <= 0.0 ) ) { ret = Double . NaN ; } else { double g = 607.0 / 128.0 ; double sum = 0.0 ; for ( int i = LANCZOS . length - 1 ; i > 0 ; -- i ) { sum = sum + ( LANCZOS [ i ] / ( x + i ) ) ; } sum = sum + LANCZOS [ 0 ] ; double tmp = x + g + .5 ; ret = ( ( x + .5 ) * log ( tmp ) ) - tmp + HALF_LOG_2_PI + log ( sum / x ) ; } return ret ; }
Gamma function ported from the apache math package .