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.

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

Details

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


Partitions$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

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

Super classes

arrangements::abstractiter -> arrangements::iter -> arrangements::Arrangements -> Partitions

Methods

Inherited methods


Method new()

Usage

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


Method reset()

Usage

Partitions$reset()


Method collect()

Usage

Partitions$collect(layout = "row")


Method getnext()

Usage

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


Method print()

Usage

Partitions$print(...)


Method clone()

The objects of this class are cloneable with this method.

Usage

Partitions$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

ipart <- ipartitions(10)
ipart$getnext()
#>  [1] 1 1 1 1 1 1 1 1 1 1
ipart$getnext(2)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,]    1    1    1    1    1    1    1    1    2     0
#> [2,]    1    1    1    1    1    1    1    3    0     0
ipart$getnext(layout = "column", drop = FALSE)
#>       [,1]
#>  [1,]    1
#>  [2,]    1
#>  [3,]    1
#>  [4,]    1
#>  [5,]    1
#>  [6,]    1
#>  [7,]    2
#>  [8,]    2
#>  [9,]    0
#> [10,]    0
# collect remaining partitions
ipart$collect()
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    1    1    1    1    1    1    4    0    0     0
#>  [2,]    1    1    1    1    1    2    3    0    0     0
#>  [3,]    1    1    1    1    1    5    0    0    0     0
#>  [4,]    1    1    1    1    2    2    2    0    0     0
#>  [5,]    1    1    1    1    2    4    0    0    0     0
#>  [6,]    1    1    1    1    3    3    0    0    0     0
#>  [7,]    1    1    1    1    6    0    0    0    0     0
#>  [8,]    1    1    1    2    2    3    0    0    0     0
#>  [9,]    1    1    1    2    5    0    0    0    0     0
#> [10,]    1    1    1    3    4    0    0    0    0     0
#> [11,]    1    1    1    7    0    0    0    0    0     0
#> [12,]    1    1    2    2    2    2    0    0    0     0
#> [13,]    1    1    2    2    4    0    0    0    0     0
#> [14,]    1    1    2    3    3    0    0    0    0     0
#> [15,]    1    1    2    6    0    0    0    0    0     0
#> [16,]    1    1    3    5    0    0    0    0    0     0
#> [17,]    1    1    4    4    0    0    0    0    0     0
#> [18,]    1    1    8    0    0    0    0    0    0     0
#> [19,]    1    2    2    2    3    0    0    0    0     0
#> [20,]    1    2    2    5    0    0    0    0    0     0
#> [21,]    1    2    3    4    0    0    0    0    0     0
#> [22,]    1    2    7    0    0    0    0    0    0     0
#> [23,]    1    3    3    3    0    0    0    0    0     0
#> [24,]    1    3    6    0    0    0    0    0    0     0
#> [25,]    1    4    5    0    0    0    0    0    0     0
#> [26,]    1    9    0    0    0    0    0    0    0     0
#> [27,]    2    2    2    2    2    0    0    0    0     0
#> [28,]    2    2    2    4    0    0    0    0    0     0
#> [29,]    2    2    3    3    0    0    0    0    0     0
#> [30,]    2    2    6    0    0    0    0    0    0     0
#> [31,]    2    3    5    0    0    0    0    0    0     0
#> [32,]    2    4    4    0    0    0    0    0    0     0
#> [33,]    2    8    0    0    0    0    0    0    0     0
#> [34,]    3    3    4    0    0    0    0    0    0     0
#> [35,]    3    7    0    0    0    0    0    0    0     0
#> [36,]    4    6    0    0    0    0    0    0    0     0
#> [37,]    5    5    0    0    0    0    0    0    0     0
#> [38,]   10    0    0    0    0    0    0    0    0     0

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