##' naive direct formula:
g1 <- function(u) 1/gamma(u+1) - 1
##' @title gam1() from TOMS 708 -- translated to R (*and* vectorized)
##' @author Martin Maechler
gam1R <- function(a, chk=TRUE) { ## == 1/gamma(a+1) - 1 -- accurately ONLY for -0.5 <= a <= 1.5
if(!length(a)) return(a)
## otherwise:
if(chk) stopifnot(-0.5 <= a, a <= 1.5) # if not, the computation below is non-sense!
d <- a - 0.5
## t := if(a > 1/2) a-1 else a ==> t in [-0.5, 0.5] <==> |t| <= 0.5
R <- t <- a
dP <- d > 0
t[dP] <- d[dP] - 0.5
if(any(N <- (t < 0.))) { ## L30: */
r <- c(-.422784335098468, -.771330383816272,
-.244757765222226, .118378989872749, 9.30357293360349e-4,
-.0118290993445146, .00223047661158249, 2.66505979058923e-4,
-1.32674909766242e-4)
s1 <- .273076135303957
s2 <- .0559398236957378
t_ <- t[N]
top <- (((((((r[9] * t_ + r[8]) * t_ + r[7]) * t_ + r[6]) * t_ + r[5]) * t_ + r[4]
) * t_ + r[3]) * t_ + r[2]) * t_ + r[1]
bot <- (s2 * t_ + s1) * t_ + 1.
w <- top / bot
## if (d > 0.) :
if(length(iP <- which(dP[N])))
R[N & dP] <- (t_ * w)[iP] / a[N & dP]
## else d <= 0 :
if(length(iN <- which(!dP[N])))
R[N & !dP] <- a[N & !dP] * (w[iN] + 0.5 + 0.5)
}
if(any(Z <- (t == 0))) ## L10: a in {0, 1}
R[Z] <- 0.
if(any(P <- t > 0)) { ## t > 0; L20: */
p <- c( .577215664901533, -.409078193005776,
-.230975380857675, .0597275330452234, .0076696818164949,
-.00514889771323592, 5.89597428611429e-4 )
q <- c(1., .427569613095214, .158451672430138, .0261132021441447, .00423244297896961)
t <- t[P]
top <- (((((p[7] * t + p[6])*t + p[5])*t + p[4])*t + p[3])*t + p[2])*t + p[1]
bot <- (((q[5] * t + q[4]) * t + q[3]) * t + q[2]) * t + 1.
w <- top / bot
## if (d > 0.) ## L21: */
if(length(iP <- which(dP[P])))
R[P & dP] <- t[iP] / a[P & dP] * (w[iP] - 0.5 - 0.5)
## else d <= 0 :
if(length(iN <- which(!dP[P])))
R[P & !dP] <- a[P & !dP] * w[iN]
}
R
} ## gam1R()
u <- seq(-.5, 1.5, by=1/16); set.seed(1); u <- sample(u) # permuted (to check logic)
g11 <- vapply(u, gam1R, 1) # [-.5, 1.5] == the interval for which the above gam1() was made
gam1. <- gam1R(u)
cbind(u, gam1., D = sfsmisc::relErrV(gam1., g1(u)))[order(u),]
# looks "too good", as we are not close (but different) to {0, 1}
stopifnot( identical(g11, gam1.) )
all.equal(g1(u), gam1., tolerance = 0) # 6.7e-16 ("too good", see above)
stopifnot( all.equal(g1(u), gam1.) )
## Comparison using Rmpfr; slightly extending [-.5, 1.5] interval (and getting much closer to {0,1})
u <- seq(-0.525, 1.525, length.out = 2001)
uM <- Rmpfr::mpfr(u, 128)
gam1M. <- gam1M(uM)
relE <- Rmpfr::asNumeric(sfsmisc::relErrV(gam1M., gam1R(u, chk=FALSE)))
rbind(rErr = summary(relE),
`|rE|` = summary(abs(relE)))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## rErr -3.280e-15 -3.466e-16 1.869e-17 1.526e-16 4.282e-16 1.96e-14
## |rE| 1.343e-19 2.363e-16 3.861e-16 6.014e-16 6.372e-16 1.96e-14
stopifnot(max(abs(relE)) < 1e-13)
relEtit <- expression("Relative Error of " ~~ gam1(u) %~%{} == frac(1, Gamma(u+1)) - 1) #%
plot(relE ~ u, type="l", ylim = c(-1,1) * 2.5e-15, main = relEtit)
grid(lty = 3); abline(v = c(-.5, 1.5), col = adjustcolor(4, 1/2), lty=2, lwd=2)
## what about the direct formula -- how bad is it really ?
relED <- Rmpfr::asNumeric(sfsmisc::relErrV(gam1M., g1(u)))
plot(relE ~ u, type="l", ylim = c(-1,1) * 1e-14, main = relEtit)
lines(relED ~ u, col = adjustcolor(2, 1/2), lwd = 2); abline(v = (-1:3)/2, lty=2, col="orange3")
mtext("comparing with direct formula 1/gamma(u+1) - 1", col=2, cex=3/4)
legend("top", c("gam1R(u)", "1/gamma(u+1) - 1"), col = 1:2, lwd=1:2, bty="n")
## direct is clearly *worse* , but not catastrophical
Run the code above in your browser using DataLab