2013-10-04 28 views
0

我用一些數字理論&想要獲得bigz兼容版本的combn(函數來計算梳(n,j))。 bigzgmp包生成並處理的擴展整數的類名。
我知道我可以逃脫轉換bigzdouble,並以「整數作爲浮動」的工作,至少要等到我得到一些非常大的整數,但如果有人是寫兼容一些組合學功能與gmp,我會很感激的指針。 (否則,我會去,並且試圖把它們寫在我自己的:-()尋找兼容bigz的組合函數

+0

tsk,tsk,「要求我們推薦或找到工具,圖書館或最喜愛的非現場資源的問題都是無關緊要的」,您應該知道的更好:)。現在任何,但我只是寫了一個使用Rmpfr的排列索引函數。如果你想了解詳情,請與我聯繫 –

+0

你想要組合的數量('choose',還是'chooseZ'代表'bigz'對象)或所有組合的列表('combn')? –

+0

@JoshuaUlrich是的,我知道,但是它至少可以節省15分鐘時間拷貝utils :: combn並修改它來處理bigz。 –

回答

0

好了,這裏是與bigz運行改裝成combn代碼。需要注意的是輸入驗證是不完整的,特別是驗證一個FUN函數有一個bigz方法還沒有實現我的最終版本,我將交給維護者gmp,(我希望!!)將實現這個作爲一個「combin.bigz」方法,而不是一個獨立的功能

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 
}