Title: | Efficient Iterator for Permutations and Combinations |
---|---|
Description: | Iterator for generating permutations and combinations. They can be either drawn with or without replacement, or with distinct/ non-distinct items (multiset). The generated sequences are in lexicographical order (dictionary order). The algorithms to generate permutations and combinations are memory efficient. These iterative algorithms enable users to process all sequences without putting all results in the memory at the same time. The algorithms are written in C/C++ for faster performance. Note: 'iterpc' is no longer being maintained. Users are recommended to switch to 'arrangements'. |
Authors: | Randy Lai [aut, cre] |
Maintainer: | Randy Lai <[email protected]> |
License: | GPL-2 |
Version: | 0.4.2 |
Built: | 2024-11-01 02:52:59 UTC |
Source: | https://github.com/randy3k/iterpc |
Get all permutations/combinations for a iterator
getall(I)
getall(I)
I |
a permutation/combination iterator |
next permutation/combination sequence for the iterator I
Get the current element of a iterator
getcurrent(I)
getcurrent(I)
I |
a permutation/combination iterator |
current element of a iterator
Get the length for a iterator
getlength(I, bigz = FALSE)
getlength(I, bigz = FALSE)
I |
a permutations/combinations iterator |
bigz |
use gmp's Big Interger |
an integer
Get the next permutation(s)/combination(s) for a iterator
getnext(I, d = 1, drop = TRUE)
getnext(I, d = 1, drop = TRUE)
I |
a permutation/combination iterator |
d |
number of permutation(s)/combination(s) wanted, default to 1 |
drop |
if |
next d
permutation(s)/combination(s) sequence for the iterator I
Wrap iterpc objects by iterators::iter
iter_wrapper(I, d = 1)
iter_wrapper(I, d = 1)
I |
the iterpc object |
d |
number of permutation(s)/combination(s) wanted in each iteration, default to 1 |
a iter object compatible with iterators package
library(iterators) I <- iterpc(5, 2) it <- iter_wrapper(I) nextElem(it) nextElem(it) library(foreach) I <- iterpc(5, 2) it <- iter_wrapper(I) foreach(x=it, .combine=c) %do% { sum(x) }
library(iterators) I <- iterpc(5, 2) it <- iter_wrapper(I) nextElem(it) nextElem(it) library(foreach) I <- iterpc(5, 2) it <- iter_wrapper(I) foreach(x=it, .combine=c) %do% { sum(x) }
Efficient Iterator for Permutations and Combinations
Initialize a iterator for permutations or combinations
iterpc(n, r = NULL, labels = NULL, ordered = FALSE, replace = FALSE)
iterpc(n, r = NULL, labels = NULL, ordered = FALSE, replace = FALSE)
n |
the length of the input sequence or a vector of frequencies for a multiset. |
r |
the length of the output sequence. If missing, equals to |
labels |
if |
ordered |
|
replace |
with/without replacement. Default is |
a permutation/combination iterator
#1) all combinations of drawing 2 items from {1, 2, 3} I <- iterpc(5, 2) getall(I) #2) continuing 1), get combination by combination I <- iterpc(5, 2) getnext(I) # return 1,2 getnext(I) # return 1,3 getnext(I, 2) # return next 2 results #3) 3) all permutations of {1, 2, 3} and use of labels I <- iterpc(3, labels=c("a", "b", "c"), ordered=TRUE) getall(I) #4) permutations of multiset and I <- iterpc(c(2, 1, 1), labels=c("a", "b", "c"), ordered=TRUE) getall(I) #5) combinations with replacement and the use of table as input x <- c("a","a","b","c") I <- iterpc(table(x), 3, replace=TRUE) getall(I)
#1) all combinations of drawing 2 items from {1, 2, 3} I <- iterpc(5, 2) getall(I) #2) continuing 1), get combination by combination I <- iterpc(5, 2) getnext(I) # return 1,2 getnext(I) # return 1,3 getnext(I, 2) # return next 2 results #3) 3) all permutations of {1, 2, 3} and use of labels I <- iterpc(3, labels=c("a", "b", "c"), ordered=TRUE) getall(I) #4) permutations of multiset and I <- iterpc(c(2, 1, 1), labels=c("a", "b", "c"), ordered=TRUE) getall(I) #5) combinations with replacement and the use of table as input x <- c("a","a","b","c") I <- iterpc(table(x), 3, replace=TRUE) getall(I)
This function calculates the multinomial coefficient
where 's are the number of multiplicities in the multiset.
multichoose(n, bigz = FALSE)
multichoose(n, bigz = FALSE)
n |
a vector of group sizes |
bigz |
use gmp's Big Interger |
multinomial coefficient
# (3+1+1)!/ (3! 1! 1!) = 20 multichoose(c(3,1,1))
# (3+1+1)!/ (3! 1! 1!) = 20 multichoose(c(3,1,1))
Calculate the number of r-combinations of a multiset
nc_multiset(f, r, bigz = FALSE)
nc_multiset(f, r, bigz = FALSE)
f |
the frequencies of the mutliset |
r |
the number of object drawn from the multiset |
bigz |
use gmp's Big Interger |
the number of combinations (Big Integer from gmp)
x <- c("a","a","b") # possible combinations of size 2 are "aa" and "ab". nc_multiset(table(x), 2) # <- 2
x <- c("a","a","b") # possible combinations of size 2 are "aa" and "ab". nc_multiset(table(x), 2) # <- 2
Calculate the number of r-permutations of a multiset
np_multiset(f, r, bigz = FALSE)
np_multiset(f, r, bigz = FALSE)
f |
the frequencies of the mutliset |
r |
the number of object drawn from the multiset |
bigz |
use gmp's Big Interger |
the number of r-permutations (Big Integer from gmp)
x = c("a","a","b") # possible permutations of size 2 are "aa", "ab" and "ba". np_multiset(table(x), 2) # = 3
x = c("a","a","b") # possible permutations of size 2 are "aa", "ab" and "ba". np_multiset(table(x), 2) # = 3