# combinatoricsSample: Sample Combinations and Permutations In RcppAlgos: High Performance Tools for Combinatorics and Computational Mathematics

 comboSample R Documentation

## Sample Combinations and Permutations

### Description

• Generate a specific (lexicographically) or random sample of combinations/permutations.

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

• GMP support allows for exploration of combinations/permutations of vectors with many elements.

### Usage

comboSample(v, m = NULL, repetition = FALSE, freqs = NULL, n = NULL,
sampleVec = NULL, seed = NULL, FUN = NULL, Parallel = FALSE,
nThreads = NULL, namedSample = FALSE, FUN.VALUE = NULL)

permuteSample(v, m = NULL, repetition = FALSE, freqs = NULL, n = NULL,
sampleVec = NULL, seed = NULL, FUN = NULL, Parallel = FALSE,
nThreads = NULL, namedSample = FALSE, FUN.VALUE = NULL)

### Details

These algorithms rely on efficiently generating the n^{th} lexicographical combination/permutation. This is the process of unranking.

### Value

• In general, a matrix with m or m + 1 columns, depending on the value of keepResults

• If FUN is utilized and FUN.VALUE = NULL, a list is returned

• When both FUN and FUN.VALUE are not NULL, the return is modeled after the return of vapply. See the 'Value' section of vapply.

### Note

• Parallel and nThreads will be ignored in the following cases:

• If the class of the vector passed is character (N.B. Rcpp::CharacterMatrix is not thread safe). Alternatively, you can generate an indexing matrix in parallel.

• If FUN is utilized.

• n and sampleVec cannot both be NULL.

• Factor vectors are accepted. Class and level attributes are preserved except when FUN is used.

Joseph Wood

### References

comboRank, permuteRank

### Examples

## generate 10 random combinations
comboSample(30, 8, TRUE, n = 5, seed = 10)

## using sampleVec to generate specific permutations
permuteSample(15, 10, freqs = c(1,2,2,1,2,2,1,2,1,2,2,1,2,1,1),
sampleVec = c(1, 10^2, 10^5, 10^8, 10^11))

all.equal(comboSample(10, 5,
sampleVec = 1:comboCount(10, 5)),
comboGeneral(10, 5))

## Examples with enormous number of total permutations
num = permuteCount(10000, 20)
gmp::log2.bigz(num)
## [1] 265.7268

first = gmp::urand.bigz(n = 1, size = 265, seed = 123)
mySamp = do.call(c, lapply(0:10, function(x) gmp::add.bigz(first, x)))

class(mySamp)
## [1] "bigz"

## using permuteSample
pSamp = permuteSample(10000, 20, sampleVec = mySamp)

## using permuteGeneral
pGeneral = permuteGeneral(10000, 20,
lower = first,

identical(pSamp, pGeneral)
## [1] TRUE

permPar = permuteSample(10000, 50, n = 8, seed = 10, nThreads = 2)

## Using FUN
permuteSample(10000, 50, n = 4, seed = 10, FUN = sd)

## Not run:
## Using Parallel
permuteSample(10000, 50, n = 80, seed = 10, Parallel = TRUE)

## End(Not run)

RcppAlgos documentation built on Oct. 3, 2023, 1:07 a.m.