OP는 최대 길이가 100 인 모든 정수 파티션을 찾고 있습니다. partitions
패키지에는이 목적으로 정확히 restrictedparts
이라는 기능이 있습니다. 예컨대 : 일단 모든 이들의 생성 된
library(partitions)
## all integer partitions of 10 of maximal length = 4
restrictedparts(10, 4)
[1,] 10 9 8 7 6 5 8 7 6 5 6 5 4 4 7 6 5 4 5 4 3 4 3
[2,] 0 1 2 3 4 5 1 2 3 4 2 3 4 3 1 2 3 4 2 3 3 2 3
[3,] 0 0 0 0 0 0 1 1 1 1 2 2 2 3 1 1 1 1 2 2 3 2 2
[4,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2
, 단순히 각 조합의 5 × 5 행렬을 (restrictedparts
이 0 0 3
및 0 3 0
구분하지 않음) 만듭니다. 유일한 문제는 가능한 조합 (partitions::R(25, 100, TRUE) = 139620591
)이 많아 restrictedparts(100, 25)
에 전화 할 때 오류가 발생한다는 것입니다. restrictedparts에
가
test <- restrictedparts(100, 25)
에러 (100, 25)의 NA는 외부 기능 호출 (ARG 3) 또한 : 경고 메시지가 restrictedparts에서 (100, 25)의 NA의 범위를 정수하기위한 강제 도입
우리가 restrictedparts
를 통해 그들 모두를 생성 할 수 있기 때문에, 우리는 개별적과 함께 firstrestrictedpart
를 사용하여 생성 할 수 있습니다 nextrestrictedpart
과 같이 :
funPartition <- function(n) {
p <- firstrestrictedpart(100, 25)
mat <- matrix(nrow = 25, ncol = n)
mat[,1] <- p
for (i in 2:n) {
p <- nextrestrictedpart(p)
mat[,i] <- p
}
mat
}
head(funPartition(5))
[,1] [,2] [,3] [,4] [,5]
[1,] 100 99 98 97 96
[2,] 0 1 2 3 4
[3,] 0 0 0 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 0
[6,] 0 0 0 0 0
유일한 문제는 효율적이지 않다는 것입니다.
는 RcppAlgos가
(나는의 저자) 패키지 RcppAlgos
를 사용하여 빠른 방법이 입력합니다.
library(RcppAlgos)
combs <- comboGeneral(0:100,25,TRUE,"sum","==",100,rowCap=10^5)
matrixCombs <- lapply(1:nrow(combs), function(x) matrix(combs[x,], nrow = 5, ncol = 5))
matrixCombs[1:3]
[[1]]
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 0 0 0 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 100
[[2]]
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 0 0 0 0 0
[4,] 0 0 0 0 1
[5,] 0 0 0 0 99
[[3]]
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 0 0 0 0 0
[4,] 0 0 0 0 2
[5,] 0 0 0 0 98
당신이 정말로 순열, 아무 문제를 원한다면
, 단지
permuteGeneral
전화 :
perms <- permuteGeneral(0:100,25,TRUE,"sum","==",100,rowCap=10^5)
matrixPerms <- lapply(1:nrow(perms), function(x) matrix(perms[x,], nrow = 5, ncol = 5))
matrixPerms[1:3]
[[1]]
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 0 0 0 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 100
[[2]]
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 0 0 0 0 0
[4,] 0 0 0 0 100
[5,] 0 0 0 0 0
[[3]]
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 0 0 0 0 100
[4,] 0 0 0 0 0
[5,] 0 0 0 0 0
그것은뿐만 아니라 매우 빠릅니다. norm100Master
을 의 래퍼로 사용하고 lapply(rep(5, runs), norm100)
과 함께 두십시오.
funRcppAlgos <- function(myCap) {
perms <- permuteGeneral(0:100,25,TRUE,"sum","==",100,rowCap=myCap)
lapply(1:myCap, function(x) matrix(perms[x,], nrow = 5, ncol = 5))
}
runs <- 5000
microbenchmark(norm100Master(runs), funRcppAlgos(runs))
Unit: milliseconds
expr min lq mean median uq max neval
norm100Master(runs) 50.930848 56.413103 65.00415 57.341665 64.242075 125.5940 100
funRcppAlgos(runs) 8.711444 9.382808 13.05653 9.555321 9.912229 116.9166 100
그리고 (행렬로 변환 없음) 이상 funPartition
와 정수 파티션의 유일한 세대를 비교, 우리는이 :
microbenchmark(nextPs = funPartition(10^4),
algos = comboGeneral(0:100,25,TRUE,"sum","==",100,10^4))
Unit: milliseconds
expr min lq mean median uq max neval
nextPs 317.778757 334.35560 351.68058 343.81085 355.03575 521.13181 100
algos 9.438661 10.12685 10.60887 10.37617 10.85003 13.99447 100
와 평등 테스트하기 :
identical(t(apply(funPartition(10^4), 2, rev)),
comboGeneral(0:100,25,TRUE,"sum","==",100,10^4))
[1] TRUE
내가하지 않았다 통지 * 가능한 모든 행렬 * 부분은 처음에 대답하기 전에 (나는 그것을 삭제했습니다). 나는 모든 가능한 행렬의 수가 심지어 거대 할 것이라고 생각한다! – Suren
사실 그것은 다른 접근법을 찾아야 할 것 같습니다. 시간 내 줘서 고마워! – Xbel