In the RcppAlgos
* package for R
, there are many ranking functions including for ranking partitions with the caveat that ranking/unranking is done lexicographically.
partitionsRank
from RcppAlgos
It appears that the OP's original source vector is v = 1:8
, we are allowed to have repetition, and the target is 10. To find the corresponding rank for c(5, 3, 2)
, we have the following (N.B. R
starts indexing at 1, sometimes called 1-based):
library(RcppAlgos)
partitionsRank(c(5, 3, 2), v = 1:8, repetition = TRUE, target = 10)
#> [1] 6
Flexibility
These functions are flexible and very efficient (They are written in C++
under the hood).
Let's look at a different example. Restricted partitions of 100 of length 10 with distinct parts (N.B. repetition = FALSE
is the default).
First let's generate and explore:
## Results are immediate
system.time(parts100 <- partitionsGeneral(100, 10))
#> user system elapsed
#> 0.001 0.000 0.000
## The first 6
head(parts100)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 1 2 3 4 5 6 7 8 9 55
#> [2,] 1 2 3 4 5 6 7 8 10 54
#> [3,] 1 2 3 4 5 6 7 8 11 53
#> [4,] 1 2 3 4 5 6 7 8 12 52
#> [5,] 1 2 3 4 5 6 7 8 13 51
#> [6,] 1 2 3 4 5 6 7 8 14 50
## the last 6
tail(parts100)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [33396,] 5 6 7 8 9 10 11 12 14 18
#> [33397,] 5 6 7 8 9 10 11 12 15 17
#> [33398,] 5 6 7 8 9 10 11 13 14 17
#> [33399,] 5 6 7 8 9 10 11 13 15 16
#> [33400,] 5 6 7 8 9 10 12 13 14 16
#> [33401,] 5 6 7 8 9 11 12 13 14 15
Now, let's filter for the 1st, 10th, 100th, 1000th, and 10000th partitions:
test <- parts100[c(1, 10, 100, 1000, 10000), ]
## Print them
test
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 1 2 3 4 5 6 7 8 9 55
#> [2,] 1 2 3 4 5 6 7 8 18 46
#> [3,] 1 2 3 4 5 6 7 12 28 32
#> [4,] 1 2 3 4 5 7 8 18 21 31
#> [5,] 1 2 3 6 8 11 12 13 21 23
And finally we rank them (N.B. when the target is not explicitly given, we use the max(v) = 100 in this case):
partitionsRank(test, v = 1:100)
#> [1] 1 10 100 1000 10000
Large Cases
RcppAlgos
utilizes the gmp
library for handling cases when the number of partitions is large. Observe:
partitionsCount(10000, 10, TRUE)
Big Integer ('bigz') :
#> [1] 771441672987331681548480
## Generate a reproducible sample for testing. Note we use
## namedSample = TRUE to return the lexicographic index
bigSamp <- partitionsSample(10000, 10, TRUE, n = 3,
seed = 42, namedSample = TRUE)
bigSamp
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> 356581031796290627391690 67 219 435 487 814 847 876 1187 1841 3227
#> 346735250799296299713370 65 106 391 544 599 622 926 1097 1949 3701
#> 273267181434457516383461 48 139 145 510 615 628 958 1144 1309 4504
## Confirm we have integer partitions of 10000
rowSums(bigSamp)
#> 356581031796290627391690 346735250799296299713370 273267181434457516383461
#> 10000 10000 10000
## Now we rank:
partitionsRank(bigSamp, v = 10000, repetition = TRUE)
#> Big Integer ('bigz') object of length 3:
#> [1] 356581031796290627391690 346735250799296299713370 273267181434457516383461
* I am the author of RcppAlgos