sapplyMpfr0 <- ## Originally, the function was simply defined as
function (X, FUN, ...) new("mpfr", unlist(lapply(X, FUN, ...), recursive = FALSE))
(m1 <- sapply ( 3, function(k) (1:3)^k)) # 3 x 1 matrix (numeric)
(p1 <- sapplyMpfr(mpfr(3, 64), function(k) (1:3)^k))
stopifnot(m1 == p1, is(p1, "mpfrMatrix"), dim(p1) == c(3,1), dim(p1) == dim(m1))
k.s <- c(2, 5, 10, 20)
(mk <- sapply ( k.s, function(k) (1:3)^k)) # 3 x 4 " "
(pm <- sapplyMpfr(mpfr(k.s, 64), function(k) (1:3)^k))
stopifnot(mk == pm, is(pm, "mpfrMatrix"), dim(pm) == 3:4, 3:4 == dim(mk))
## was *wrongly* 4x3 in Rmpfr 0.8-x
f5k <- function(k) outer(1:5, k+0:2, `^`)# matrix-valued
(mk5 <- sapply ( k.s, f5k)) # sapply()'s default; not "ideal"
(ak5 <- sapply ( k.s, f5k, simplify = "array")) # what we want
(pm5 <- sapplyMpfr(mpfr(k.s, 64), f5k))
stopifnot(c(mk5) == c(ak5), ak5 == pm5, is(pm5, "mpfrArray"), is.array(ak5),
dim(pm5) == dim(ak5), dim(pm5) == c(5,3, 4))
if(require("Bessel")) { # here X, is simple
bI1 <- function(k) besselI.nuAsym(mpfr(1.31e9, 128), 10, expon.scaled=TRUE, k.max=k)
bImp1 <- sapplyMpfr (0:4, bI1, drop_1_ = FALSE) # 1x5 mpfrMatrix -- as in DPQ 0.8-8
bImp <- sapplyMpfr (0:4, bI1, drop_1_ = TRUE ) # 5 "mpfr" vector {by default}
bImp0 <- sapplyMpfr0(0:4, bI1) # 5-vector
stopifnot(identical(bImp, bImp0), bImp == bImp1,
is(bImp, "mpfr"), is(bImp1, "mpfrMatrix"), dim(bImp1) == c(1, 5))
}# {Bessel}
Run the code above in your browser using DataLab