1#  SPECIAL FUNCTIONS
2
3logmdigamma <- function(x)
4{
5#  log(x) - digamma(x)
6#  Saves computation of log(x) and avoids subtractive cancellation in digamma(x) when x is large
7#  Gordon Smyth, smyth@wehi.edu.au
8#  19 Jan 98.  Last revised 9 Dec 2002.
9#
10	z <- x
11	if(any(omit <- is.na(z) | Re(z) <= 0)) {
12		ps <- z
13		ps[omit] <- NA
14		if(any(!omit)) ps[!omit] <- Recall(z[!omit])
15		return(ps)
16	}
17	if(any(small <- Mod(z) < 5)) {
18		ps <- z
19		x <- z[small]
20		ps[small] <- log(x/(x+5)) + Recall(x+5) + 1/x + 1/(x+1) + 1/(x+2) + 1/(x+3) + 1/(x+4)
21		if(any(!small)) ps[!small] <- Recall(z[!small])
22		return(ps)
23	}
24	x <- 1/z^2
25	tail <- ((x * (-1/12 + ((x * (1/120 + ((x * (-1/252 + ((
26		x * (1/240 + ((x * (-1/132 + ((x * (691/32760 + (
27		(x * (-1/12 + (3617 * x)/8160)))))))))))))))))))))
28	1/(2 * z) - tail
29}
30