public static double digamma ( double x ) { final double C7 [ ] [ ] = { { 1.3524999667726346383e4 , 4.5285601699547289655e4 , 4.5135168469736662555e4 , 1.8529011818582610168e4 , 3.3291525149406935532e3 , 2.4068032474357201831e2 , 5.1577892000139084710 , 6.2283506918984745826e-3 } , { 6.9389111753763444376e-7 , 1.9768574263046736421e4 , 4.1255160835353832333e4 , 2.9390287119932681918e4 , 9.0819666074855170271e3 , 1.2447477785670856039e3 , 6.7429129516378593773e1 , 1.0 } } ; final double C4 [ ] [ ] = { { - 2.728175751315296783e-15 , - 6.481571237661965099e-1 , - 4.486165439180193579 , - 7.016772277667586642 , - 2.129404451310105168 } , { 7.777885485229616042 , 5.461177381032150702e1 , 8.929207004818613702e1 , 3.227034937911433614e1 , 1.0 } } ; double prodPj = 0.0 ; double prodQj = 0.0 ; double digX = 0.0 ; if ( x >= 3.0 ) { double x2 = 1.0 / ( x * x ) ; for ( int j = 4 ; j >= 0 ; j -- ) { prodPj = prodPj * x2 + C4 [ 0 ] [ j ] ; prodQj = prodQj * x2 + C4 [ 1 ] [ j ] ; } digX = Math . log ( x ) - ( 0.5 / x ) + ( prodPj / prodQj ) ; } else if ( x >= 0.5 ) { final double X0 = 1.46163214496836234126 ; for ( int j = 7 ; j >= 0 ; j -- ) { prodPj = x * prodPj + C7 [ 0 ] [ j ] ; prodQj = x * prodQj + C7 [ 1 ] [ j ] ; } digX = ( x - X0 ) * ( prodPj / prodQj ) ; } else { double f = ( 1.0 - x ) - Math . floor ( 1.0 - x ) ; digX = digamma ( 1.0 - x ) + Math . PI / Math . tan ( Math . PI * f ) ; } return digX ; }
The digamma function is defined as the logarithmic derivative of the gamma function .