Provides fast algorithms for generating integer partitions.
partitions(n, max = 0, length = 0, perm = FALSE, fill = FALSE, equal = T)positive integer.
maximum integer in the partitions.
maximum number of elements in the partitions.
logical. Permute partitions?
logical. Fill partitions with zeros to match length?
logical. Return only partition of n? If FALSE, partitions of all integers less or equal to n are returned.
list of partitions, or matrix if length>0 and fill=TRUE.
Guidotti E (2022). "calculus: High-Dimensional Numerical and Symbolic Calculus in R." Journal of Statistical Software, 104(5), 1-37. doi:10.18637/jss.v104.i05
### partitions of 4
partitions(4)
#> [[1]]
#> [1] 4
#>
#> [[2]]
#> [1] 1 3
#>
#> [[3]]
#> [1] 2 2
#>
#> [[4]]
#> [1] 1 1 2
#>
#> [[5]]
#> [1] 1 1 1 1
#>
### partitions of 4 and permute
partitions(4, perm = TRUE)
#> [[1]]
#> [1] 4
#>
#> [[2]]
#> [1] 1 3
#>
#> [[3]]
#> [1] 3 1
#>
#> [[4]]
#> [1] 2 2
#>
#> [[5]]
#> [1] 1 1 2
#>
#> [[6]]
#> [1] 1 2 1
#>
#> [[7]]
#> [1] 2 1 1
#>
#> [[8]]
#> [1] 1 1 1 1
#>
### partitions of 4 with max element equal to 2
partitions(4, max = 2)
#> [[1]]
#> [1] 2 2
#>
#> [[2]]
#> [1] 1 1 2
#>
#> [[3]]
#> [1] 1 1 1 1
#>
### partitions of 4 with 2 elements
partitions(4, length = 2)
#> [[1]]
#> [1] 1 3
#>
#> [[2]]
#> [1] 2 2
#>
### partitions of 4 with 3 elements, fill with zeros
partitions(4, length = 3, fill = TRUE)
#> [,1] [,2] [,3] [,4]
#> [1,] 0 0 0 1
#> [2,] 0 1 2 1
#> [3,] 4 3 2 2
### partitions of 4 with 2 elements, fill with zeros and permute
partitions(4, length = 2, fill = TRUE, perm = TRUE)
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0 4 1 3 2
#> [2,] 4 0 3 1 2
### partitions of all integers less or equal to 3
partitions(3, equal = FALSE)
#> [[1]]
#> [1] 0
#>
#> [[2]]
#> [1] 1
#>
#> [[3]]
#> [1] 2
#>
#> [[4]]
#> [1] 1 1
#>
#> [[5]]
#> [1] 3
#>
#> [[6]]
#> [1] 1 2
#>
#> [[7]]
#> [1] 1 1 1
#>
### partitions of all integers less or equal to 3, fill to 2 elements and permute
partitions(3, equal = FALSE, length = 2, fill = TRUE, perm = TRUE)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 0 0 1 0 2 1 0 3 1 2
#> [2,] 0 1 0 2 0 1 3 0 2 1