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.

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

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

Super classes

arrangements::abstractiter -> arrangements::iter -> arrangements::Arrangements -> Compositions

Methods

Inherited methods


Method new()

Usage

Compositions$new(n, k = NULL, descending = FALSE, skip = NULL)


Method reset()

Usage

Compositions$reset()


Method collect()

Usage

Compositions$collect(layout = "row")


Method getnext()

Usage

Compositions$getnext(d = 1L, layout = NULL, drop = NULL)


Method print()

Usage

Compositions$print(...)


Method clone()

The objects of this class are cloneable with this method.

Usage

Compositions$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

ipart <- icompositions(4)
ipart$getnext()
#> [1] 1 1 1 1
ipart$getnext(2)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    2    0
#> [2,]    1    2    1    0
ipart$getnext(layout = "column", drop = FALSE)
#>      [,1]
#> [1,]    1
#> [2,]    3
#> [3,]    0
#> [4,]    0
# collect remaining compositions
ipart$collect()
#>      [,1] [,2] [,3] [,4]
#> [1,]    2    1    1    0
#> [2,]    2    2    0    0
#> [3,]    3    1    0    0
#> [4,]    4    0    0    0

library(foreach)
#> Error in library(foreach): there is no package called ‘foreach’
foreach(x = icompositions(6, 2), .combine=c) %do% {
  prod(x)
}
#> Error in foreach(x = icompositions(6, 2), .combine = c) %do% {    prod(x)}: could not find function "%do%"