OK, so here's the modded combn
code that runs with bigz
. Note that input validation is incomplete, and in particular validation that a FUN
function has a bigz
method is not yet implemented. My final version, which I'll hand off to the maintainer of gmp
, (I hope!!) will implement this as a "combin.bigz" method rather than a standalone function.
combnz <- function (x, m, FUN = NULL, simplify = TRUE, ...)
{
stopifnot(length(m) == 1L)
if (m < 0)
stop("m < 0", domain = NA)
# Really not much point in creating a sequence 1:gigundo_bigz
#if (is.numeric(x) && length(x) == 1L && x > 0 && trunc(x) == x)
#x <- seq_len(x)
n <- length(x)
if (n < m)
stop("n < m", domain = NA)
m <- as.integer(m)
e <- 0
h <- m
a <- seq_len(m)
nofun <- is.null(FUN)
if (!nofun && !is.function(FUN))
stop("'FUN' must be a function or NULL")
# TODO: add a " grep('bigz',methods(FUN) )" to verify there's a method.
len.r <- length(r <- if (nofun) x[a] else FUN(x[a], ...))
count <- as.integer(round(choose(n, m)))
if (simplify) {
dim.use <- if (nofun)
c(m, count)
else {
d <- dim(r)
if (length(d) > 1L)
c(d, count)
else if (len.r > 1L)
c(len.r, count)
else c(d, count)
}
}
if (simplify) {
out <- matrix(r, nrow = len.r, ncol = count)
}
else {
out <- vector("list", count)
out[[1L]] <- r
}
if (m > 0) {
i <- 2L
nmmp1 <- n - m + 1L
while (a[1L] != nmmp1) {
if (e < n - h) {
h <- 1L
e <- a[m]
j <- 1L
}
else {
e <- a[m - h]
h <- h + 1L
j <- 1L:h
}
a[m - h + j] <- e + j
r <- if (nofun)
x[a]
else FUN(x[a], ...)
if (simplify)
# bigz doesn't handle replacement the way regular matrices do. So...
out[1:len.r,i] <- r
# out[, i] <- r
# I suspect a list element ain't gonna care
else out[[i]] <- r
i <- i + 1L
}
}
if (simplify)
array(out, dim.use)
else out
}