Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

icompositions

Compositions iterator


Description

This function returns a Compositions iterator for iterating compositions of an non-negative integer n into k parts or parts of any sizes. The iterator allows users to fetch the next partition(s) via the getnext() method.

Usage

Compositions

icompositions(n, k = NULL, descending = FALSE, skip = NULL)

Arguments

n

an non-negative integer to be partitioned

k

number of parts

descending

an logical to use reversed lexicographical order

skip

the number of compositions skipped

Format

An object of class R6ClassGenerator of length 25.

Details

The Compositions class can be initialized by using the convenient wrapper icompositions or

Compositions$new(n, k = NULL, descending = FALSE)
getnext(d = 1L, layout = NULL, drop = NULL)
collect(layout = "row")
reset()
d

number of fetched arrangements

layout

if "row", "column" or "list" is specified, the returned value would be a "row-major" matrix, a "column-major" matrix or a list respectively

drop

vectorize a matrix or unlist a list

See Also

compositions for generating all compositions and ncompositions to calculate number of compositions

Examples

ipart <- icompositions(4)
ipart$getnext()
ipart$getnext(2)
ipart$getnext(layout = "column", drop = FALSE)
# collect remaining compositions
ipart$collect()

library(foreach)
foreach(x = icompositions(6, 2), .combine=c) %do% {
  prod(x)
}

arrangements

Fast Generators and Iterators for Permutations, Combinations, Integer Partitions and Compositions

v1.1.9
MIT + file LICENSE
Authors
Randy Lai [aut, cre]
Initial release
2020-09-12

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.