ipartitions: Partitions iterator

Description Usage Arguments Format Details See Also Examples

View source: R/partitions.R

Description

This function returns a Partitions iterator for iterating partitions 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

1
2
3
4
Partitions

ipartitions(n, k = NULL, distinct = FALSE, descending = FALSE,
  skip = NULL)

Arguments

n

an non-negative integer to be partitioned

k

number of parts

distinct

an logical to restrict distinct values

descending

an logical to use reversed lexicographical order

skip

the number of partitions skipped

Format

An object of class R6ClassGenerator of length 25.

Details

The Partitions class can be initialized by using the convenient wrapper ipartitions or

1
Partitions$new(n, k = NULL, descending = FALSE)
1
2
3
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

partitions for generating all partitions and npartitions to calculate number of partitions

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
ipart <- ipartitions(10)
ipart$getnext()
ipart$getnext(2)
ipart$getnext(layout = "column", drop = FALSE)
# collect remaining partitions
ipart$collect()

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

arrangements documentation built on Sept. 13, 2020, 5:20 p.m.