View source: R/partitionsGeneral.R
| partitionsGeneral | R Documentation |
The algorithms in RcppAlgos go beyond the traditional integer partition algorithms and can tackle a wide variety of cases.
Efficient algorithms for partitioning numbers under various constraints:
Standard (with repetition)
Distinct
Restricted
Where each part has a specific multiplicity (i.e. when using freqs for multisets).
Arbitrary target and source vector (e.g. partitionsGeneral(sample(1000, 20), 10, TRUE, target = 5000))
Produce results in parallel using the nThreads arguments.
Alternatively, the arguments lower and upper make it possible to generate partitions/compositions in chunks allowing for parallelization via the parallel package.
GMP support allows for exploration of cases where the number of partitions/compositions is large.
The output is in lexicographical order.
partitionsGeneral(v, m = NULL, ...)
compositionsGeneral(v, m = NULL, ...)
## Default S3 method:
partitionsGeneral(
v, m = NULL, repetition = FALSE, freqs = NULL, target = NULL,
lower = NULL, upper = NULL, nThreads = NULL, tolerance = NULL, ...
)
## Default S3 method:
compositionsGeneral(
v, m = NULL, repetition = FALSE, freqs = NULL, target = NULL, weak = FALSE,
lower = NULL, upper = NULL, nThreads = NULL, tolerance = NULL, ...
)
## S3 method for class 'table'
partitionsGeneral(
v, m = NULL, target = NULL, lower = NULL,
upper = NULL, nThreads = NULL, tolerance = NULL, ...
)
## S3 method for class 'table'
compositionsGeneral(
v, m = NULL, target = NULL, weak = FALSE, lower = NULL,
upper = NULL, nThreads = NULL, tolerance = NULL, ...
)
v |
Source vector. If |
m |
Width of the partition. If |
... |
Further arguments passed to methods. |
repetition |
Logical value indicating whether partitions/compositions should be with or without repetition. The default is |
freqs |
A vector of frequencies used for producing all partitions of a multiset of |
lower |
The lower bound. Partitions/compositions are generated lexicographically, thus utilizing this argument will determine which specific partition to start generating from (e.g. |
upper |
The upper bound. Similar to |
target |
Number to be partitioned. If |
weak |
(Compositions only) Logical flag indicating whether to allow terms of the sequence to be zero. |
nThreads |
Requested number of threads. 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 |
compositionsGeneral generates supported compositions in
lexicographical order across all matching rows. Its lower and
upper bounds index those matching rows, so lower = 1 returns
the same prefix as omitting lower when matching rows exist.
For supported partitions and compositions with no matches, omitting both
bounds returns an empty matrix. Explicit positive bounds, including
lower = 1, exceed the zero result count and raise an error.
This differs from sum/equality calls to permuteGeneral.
Without lower, supported constrained permutation calls generate
matching partitions and then all distinct permutations of each partition;
upper caps matching rows in that order. With lower supplied,
permutation bounds select ordinary candidate ranks to filter instead.
See Ordering and bounds in comboGeneral.
When zero is present, permuteGeneral treats it as an actual element
that can occupy any position, subject to multiplicities. It does not use
the zero-padding convention of non-weak compositions.
A matrix is returned with each row containing a vector of length m.
nThreads will be ignored in the following cases (i.e. Generating the n^{th} partition in these cases are currently unavailable):
If the source vector is not isomorphic to 1:length(v) (e.g. v = c(1, 4, 6, 7, 8)).
The maximum number of partitions/compositions that can be generated at one time is 2^{31} - 1. Utilizing lower and upper makes it possible to generate additional partitions/compositions.
Joseph Wood
partitionsGeneral(1)
partitionsGeneral(-1:0, 1)
partitionsGeneral(-1:0, 1, target = -1)
partitionsGeneral(20, 5)
partitionsGeneral(20, 5, repetition = TRUE)
partitionsGeneral(20, 5, freqs = rep(1:4, 5))
partitionsGeneral(20, 5, TRUE, target = 80)
partitionsGeneral(0:10, repetition = TRUE)
partitionsGeneral(seq(2L, 500L, 23L), 5, target = 1804)
compositionsGeneral(0:10, 5, repetition = TRUE)
set.seed(111)
partitionsGeneral(sample(1000, 20), 5, TRUE, target = 2500)
system.time(one_thread <- partitionsGeneral(80, 10, TRUE))
system.time(two_threads <- partitionsGeneral(80, 10, TRUE, nThreads = 2))
identical(one_thread, two_threads)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.