Learn R Programming

RcppAlgos (version 2.5.0)

partitionsSample: Sample Partitions

Description

  • Generate a specific (lexicographically) or random sample of partitions of a number.

  • Produce results in parallel using the Parallel or nThreads arguments.

  • GMP support allows for exploration of cases where the number of partitions is large.

Usage

partitionsSample(v, m = NULL, repetition = FALSE, freqs = NULL,
                 target = NULL, n = NULL, sampleVec = NULL,
                 seed = NULL, nThreads = NULL, namedSample = FALSE)

Arguments

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. Only integer and numeric vectors are accepted.

m

Width of the partition. If m = NULL, the length will be determined by the partitioning case (e.g. When we are generating distinct partitions of \(n\), the width will be equal to the smallest \(m\) such that sum(1:m) >= n).

repetition

Logical value indicating whether partitions 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.

n

Number of partitions to return. The default is NULL.

sampleVec

A vector of numbers representing the lexicographical partitions to return. Accepts vectors of class bigz as well as vectors of characters

seed

Random seed initialization. The default is NULL. N.B. If the gmp library is needed, this parameter must be set in order to have reproducible results (E.g set.seed() has no effect in these cases).

nThreads

Specific number of threads to be used. The default is NULL.

namedSample

Logical flag. If TRUE, rownames corresponding to the lexicographical partition, will be added to the returned matrix. The default is FALSE.

Value

A matrix is returned with each row containing a vector of length \(m\).

Details

These algorithms rely on efficiently generating the \(n^{th}\) lexicographical partition. This is the process of unranking.

References

Examples

Run this code
# NOT RUN {
partitionsSample(100, 10, n = 5)
partitionsSample(100, 10, seed = 42, n = 5, target = 200)

## retrieve specific results (lexicographically)
partitionsCount(100, 10, TRUE, target = 500)
## [1] 175591757896
partitionsSample(100, 10, TRUE, target = 500,
                 sampleVec = c(1, 1000, 175591757896))
# }

Run the code above in your browser using DataLab