Computes the digamma function of the input @param x the input value @return ψ(x)
(double x)
| 203 | * @return ψ(x) |
| 204 | */ |
| 205 | public static double digamma(double x) |
| 206 | { |
| 207 | if(x == 0) |
| 208 | return Double.NaN;//complex infinity |
| 209 | else if(x < 0)//digamma(1-x) == digamma(x)+pi/tan(pi*x), to make x positive |
| 210 | { |
| 211 | if(Math.rint(x) == x) |
| 212 | return Double.NaN;//the zeros are complex infinity |
| 213 | return digamma(1-x)-PI/tan(PI*x); |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * shift over 2 values to the left and use truncated approximation |
| 218 | * log(x+2)-1/(2 (x+2))-1/(12 (x+2)^2) -1/x-1/(x+1), |
| 219 | * the x+2 and x and x+1 are grouped sepratly |
| 220 | */ |
| 221 | double xp2 = x+2; |
| 222 | |
| 223 | return log(xp2)-(6*x+13)/(12*xp2*xp2)-(2*x+1)/(x*x+x); |
| 224 | } |
| 225 | } |