comboIter {RcppAlgos} | R Documentation |
Combination and Permutation Iterator
Description
Returns an iterator for iterating over combinations or permutations of a vector with or without constraints.
Supports random access via the
[[
method.GMP support allows for exploration of combinations/permutations of vectors with many elements.
The output is in lexicographical order for the
next
methods and reverse lexicographical order for theprev
methods.Learn more in
vignette("iterators")
.
Usage
comboIter(v, m = NULL, ...)
permuteIter(v, m = NULL, ...)
## S3 method for class 'numeric'
comboIter(v, m = NULL, repetition = FALSE, freqs = NULL,
constraintFun = NULL, comparisonFun = NULL,
limitConstraints = NULL, keepResults = NULL,
FUN = NULL, Parallel = FALSE, nThreads = NULL,
tolerance = NULL, FUN.VALUE = NULL, ...)
## S3 method for class 'numeric'
permuteIter(v, m = NULL, repetition = FALSE, freqs = NULL,
constraintFun = NULL, comparisonFun = NULL,
limitConstraints = NULL, keepResults = NULL,
FUN = NULL, Parallel = FALSE, nThreads = NULL,
tolerance = NULL, FUN.VALUE = NULL, ...)
## S3 method for class 'factor'
comboIter(
v, m = NULL, repetition = FALSE, freqs = NULL, FUN = NULL,
Parallel = FALSE, nThreads = NULL, FUN.VALUE = NULL, ...
)
## S3 method for class 'factor'
permuteIter(
v, m = NULL, repetition = FALSE, freqs = NULL, FUN = NULL,
Parallel = FALSE, nThreads = NULL, FUN.VALUE = NULL, ...
)
## Default S3 method:
comboIter(
v, m = NULL, repetition = FALSE, freqs = NULL,
FUN = NULL, FUN.VALUE = NULL, ...
)
## Default S3 method:
permuteIter(
v, m = NULL, repetition = FALSE, freqs = NULL,
FUN = NULL, FUN.VALUE = NULL, ...
)
## S3 method for class 'table'
comboIter(
v, m = NULL, constraintFun = NULL, comparisonFun = NULL,
limitConstraints = NULL, keepResults = NULL, FUN = NULL,
Parallel = FALSE, nThreads = NULL, tolerance = NULL, FUN.VALUE = NULL, ...
)
## S3 method for class 'table'
permuteIter(
v, m = NULL, constraintFun = NULL, comparisonFun = NULL,
limitConstraints = NULL, keepResults = NULL, FUN = NULL,
Parallel = FALSE, nThreads = NULL, tolerance = NULL, FUN.VALUE = NULL, ...
)
## S3 method for class 'list'
comboIter(v, m = NULL, repetition = FALSE, freqs = NULL, ...)
## S3 method for class 'list'
permuteIter(v, m = NULL, repetition = FALSE, freqs = NULL, ...)
Arguments
v |
Source vector. If |
m |
Number of elements to choose. If |
... |
Further arguments passed to methods. |
repetition |
Logical value indicating whether combinations/permutations should be with or without repetition. The default is |
freqs |
A vector of frequencies used for producing all combinations/permutations of a multiset of |
constraintFun |
Function to be applied to the elements of |
comparisonFun |
Comparison operator that will be used to compare When
In other words, the first comparison operator is applied to the first limit and the second operator is applied to the second limit. |
limitConstraints |
This is the value(s) that will be used for comparison. Can be passed as a single value or a vector of two numerical values. The default is |
keepResults |
A logical flag indicating if the result of |
FUN |
Function to be applied to each combination/permutation. The default is |
Parallel |
Logical value indicating whether combinations/permutations should be generated in parallel using |
nThreads |
Specific number of threads to be used. The default is |
tolerance |
A numeric value greater than or equal to zero. This parameter is utilized when a constraint is applied on a numeric vector. The default value is 0 when it can be determined that whole values are being utilized, otherwise it is |
FUN.VALUE |
A template for the return value from |
Details
Once you initialize a new iterator, the following methods are available via @
(e.g. a@nextIter()
) or $
(e.g. a$nextIter()
). The preferred practice is to use @
as it is much more efficient (See examples below). Also note that not all of the methods below are available in all cases. See Combo-class
, Constraints-class
, and Partitions-class
:
nextIter
Retrieve the next lexicographical result
nextNIter
Pass an integer n to retrieve the next n lexicographical results
nextRemaining
Retrieve all remaining lexicographical results
currIter
Returns the current iteration
prevIter
Retrieve the previous lexicographical result (the next reverse lexicographical result)
prevNIter
Pass an integer n to retrieve the previous n lexicographical results (the next n reverse lexicographical results)
prevRemaining
Retrieve all remaining reverse lexicographical results
startOver
Resets the iterator
sourceVector
View the source vector
summary
Returns a list of summary information about the iterator
front
Retrieve the first lexicographical result
back
Retrieve the last lexicographical result
[[
Random access method. Pass a single value or a vector of valid indices. If a single value is passed, the internal index of the iterator will be updated, however if a vector is passed the internal state will not change. GMP support allows for flexible indexing.
Value
If
nextIter
orprevIter
is called, a vector is returnedOtherwise, a matrix with
or
columns, depending on the value of
keepResults
If
FUN
is utilized,FUN.VALUE = NULL
, and eithernextIter
orprevIter
is called, the result will be determined byFUN
, otherwise a list is returned.When both
FUN
andFUN.VALUE
are notNULL
, the return is modeled after the return ofvapply
. See the 'Value' section ofvapply
.
Note
-
Parallel
andnThreads
will be ignored in the following cases:When the output is constrained (except for most partitions cases)
If the class of the vector passed is
character
,raw
, andcomplex
(N.B.Rcpp::CharacterMatrix
is not thread safe). Alternatively, you can generate an indexing matrix in parallel.If
FUN
is utilized.
If either
constraintFun
,comparisonFun
orlimitConstraints
isNULL
–or– if the class of the vector passed islogical
,character
,raw
,factor
, orcomplex
, the constraint check will not be carried out. This is equivalent to simply finding all combinations/permutations ofchoose
.
The maximum number of combinations/permutations that can be generated at one time is
.
Factor vectors are accepted. Class and level attributes are preserved except when
FUN
is used.Lexicographical ordering isn't guaranteed for permutations if the output is constrained.
-
FUN
will be ignored if the constraint check is satisfied.
Author(s)
Joseph Wood
References
See Also
Examples
## Typical usage
a = permuteIter(unique(state.region))
a@nextIter()
a@nextNIter(3)
a@front()
a@nextRemaining()
a@prevIter()
a@prevNIter(15)
a@summary()
a@back()
a@prevRemaining()
a[[5]]
a@summary()
a[[c(1, 17, 3)]]
a@summary()
## See examples for comboGeneral where lower and upper are used
set.seed(1009)
mySamp = sort(rnorm(75, 997, 23))
b = comboIter(mySamp, 7,
constraintFun = "sum",
comparisonFun = ">",
limitConstraints = 7200)
b@nextIter()
b@nextNIter(3)
b@summary()
b@currIter()
## Not run:
## We don't have random access or previous methods
b@back()
#> Error: no slot of name "back" for this object of class "Constraints"
b@prevIter()
#> Error: no slot of name "prevIter" for this object of class "Constraints"
## End(Not run)