Skip to contents

  • Generate the rank (lexicographically) of partitions/compositions. These functions are the complement to partitions/compositionsSample. See the examples below.

  • GMP support allows for exploration of partitions/compositions of vectors with many elements.

Usage

partitionsRank(..., v, repetition = FALSE, freqs = NULL,
               target = NULL, nThreads = NULL)

compositionsRank(..., v, repetition = FALSE, freqs = NULL,
                 target = NULL, weak = FALSE, nThreads = NULL)

Arguments

...

vectors or matrices to be ranked.

v

Source vector. If v is a positive integer, it will be converted to the sequence 1:v. If v is a negative integer, it will be converted to the sequence v:-1. All atomic types are supported (See is.atomic).

repetition

Logical value indicating whether partitions/compositions should be with or without repetition. The default is FALSE.

freqs

A vector of frequencies used for producing all partitions of a multiset of v. Each element of freqs represents how many times each element of the source vector, v, is repeated. It is analogous to the times argument in rep. The default value is NULL.

target

Number to be partitioned. If NULL, max(v) will be used.

weak

(Compositions only) Logical flag indicating whether to allow terms of the sequence to be zero.

nThreads

Requested number of threads. The default is NULL for serial execution. Requests are capped by the available hardware and the useful work in the calculation. Small or simple cases may use fewer threads.

Details

These algorithms rely on efficiently ranking the \(n^{th}\) lexicographical partition. Supported multiset partitions can be ranked using freqs, including cases with GMP-sized ranks. General multiset compositions can be counted with compositionsCount, but are not supported by compositionsRank.

Value

A vector of class integer, numeric, or bigz determined by the total number of partitions/compositions

Author

Joseph Wood

Note

v must be supplied.

Examples

mySamp = partitionsSample(30, 8, TRUE, n = 5, seed = 10, namedSample = TRUE)
myRank = partitionsRank(mySamp, v = 30, repetition = TRUE)
all.equal(as.integer(rownames(mySamp)), myRank)
#> [1] TRUE

## Recover the ranks of sampled multiset partitions
indices <- c(1L, 4L)
parts <- partitionsSample(1:5, 3, freqs = rep(2, 5), target = 9,
                          sampleVec = indices)
identical(partitionsRank(parts, v = 1:5, freqs = rep(2, 5), target = 9),
          indices)
#> [1] TRUE