parts {partitions} | R Documentation |
Enumerate the partitions of an integer
Description
Given an integer, return a matrix whose columns enumerate various partitions.
Function parts()
returns the unrestricted partitions; function
diffparts()
returns the unequal partitions; function
restrictedparts()
returns the restricted partitions; function
blockparts()
returns the partitions subject to specified
maxima; and function compositions()
returns all compositions
of the argument.
Usage
parts(n)
diffparts(n)
restrictedparts(n, m, include.zero=TRUE, decreasing=TRUE)
blockparts(f, n=NULL, include.fewer=FALSE)
compositions(n, m=NULL, include.zero=TRUE)
multiset(v,n=length(v))
mset(v)
multinomial(v)
allbinom(n,k)
Arguments
n |
Integer to be partitioned. In function |
m |
In functions |
include.zero |
In functions |
include.fewer |
In function |
decreasing |
In |
f |
In function |
v |
In function |
k |
In function |
Details
Function
parts()
uses the algorithm in Andrews. Functiondiffparts()
uses a very similar algorithm that I have not seen elsewhere. These functions behave strangely if given an argument of zero.Function
restrictedparts()
uses the algorithm in Andrews, originally due to Hindenburg. For partitions into at most m parts, the same Hindenburg's algorithm is used but with a start vector ofc(rep(0,m-1),n)
.Functions
parts()
andrestrictedparts()
overlap in functionality. Note, however, that they can return identical partitions but in a different order:parts(6)
andrestrictedparts(6,6)
for example.If \(m>n\), the partitions are padded with zeros.
Function
blockparts()
enumerates the compositions of an integer subject to a maximum criterion: given vector \(y=(y_1,\ldots,y_n)\) all sets of \(a=(a_1,\ldots,a_n)\) satisfying \(\sum_{i=1}^pa_i=n\) subject to \(0\leq a_i\leq y_i\) for all i are given in lexicographical order. If argumenty
includes zero elements, these are treated consistently (ie a position with zero capacity).If
n
takes its default value ofNULL
, then the restriction \(\sum_{i=1}^pa_i=n\) is relaxed (so that the numbers may sum to anything). Note that these solutions are not necessarily in standard form, so functionsdurfee()
andconjugate()
may fail.With a single argument,
compositions(n)
returns all \(2^{n-1}\) ways of partitioning an integer; thus4+1+1
is distinct from1+4+1
or1+1+4
.With two arguments,
compositions(n,m)
returns all nonnegative solutions to \(x_1+\cdots+x_m=n\).This function is different from all the others in the package in that it is written in R; it is not clear that C would be any faster.
Function
multiset()
returns all ways of ordering a multiset (mset()
is a low-level helper function).Function
multinomial(v)
returns all ways of partitioning a set into distinguishable boxes of capacitiesv[1], v[2],...,v[n]
. The number of columns is given by the multinomial coefficient \({\sum v_i\choose v_1\,v_2\,\ldots\,v_n}\).Function
allbinom(n,k)
is provided for convenience; it enumerates the ways of choosing k objects fromn
.
Note
These vectorized functions return a matrix whose columns are the
partitions. If this matrix is too large, consider enumerating the
partitions individually using the functionality documented in
nextpart.Rd
.
One commonly encountered idiom is blockparts(rep(n,n),n)
, which
is equivalent to compositions(n,n)
[Sloane's A001700
].
If you have a minimum number of balls in each block, a construction like
x <- c(1, 1, 2, 1) y <- c(2, 3, 4, 5) sweep(blockparts(y - x, 7 - sum(x)), 1, x, "+") ##: [1,] 2 1 2 1 1 2 1 1 1 ##: [2,] 2 3 1 2 1 1 2 1 1 ##: [3,] 2 2 3 3 4 2 2 3 2 ##: [4,] 1 1 1 1 1 2 2 2 3
can be helpful (that is, subtract off the minimum number of balls and add them back again at the end).
blockparts(c(4,3,3,2),5) # Knuth's example, pre-fascicle 3a, p16 multiset(c(1,2,2,3)) # also Knuth
Author(s)
Robin K. S. Hankin
References
G. E. Andrews. “The theory of partitions”, Cambridge University Press, 1998
R. K. S. Hankin 2006. “Additive integer partitions in R”. Journal of Statistical Software, Volume 16, code snippet 1
R. K. S. Hankin 2007. “Urn sampling without replacement: enumerative combinatorics in R”. Journal of Statistical Software, Volume 17, code snippet 1
R. K. S. Hankin 2007. “Set partitions in R”. Journal of Statistical Software, Volume 23, code snippet 2
N. J. A. Sloane, 2008, The On-Line Encyclopedia of Integer Sequences. Sequence A001700
D. Knuth, 2004. The art of computer programming, pre-fascicle 2B “Generating all permutations”
See Also
Examples
parts(7)
##: [1,] 7 6 5 5 4 4 4 3 3 3 3 2 2 2 1
##: [2,] 0 1 2 1 3 2 1 3 2 2 1 2 2 1 1
##: [3,] 0 0 0 1 0 1 1 1 2 1 1 2 1 1 1
##: [4,] 0 0 0 0 0 0 1 0 0 1 1 1 1 1 1
##: [5,] 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1
##: [6,] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
##: [7,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
P(7)
##: [1] 15
diffparts(9)
##: [1,] 9 8 7 6 6 5 5 4
##: [2,] 0 1 2 3 2 4 3 3
##: [3,] 0 0 0 0 1 0 1 2
Q(9)
##: [1] 8
restrictedparts(9, 4)
##: [1,] 9 8 7 6 5 7 6 5 4 5 4 3 6 5 4 4 3 3
##: [2,] 0 1 2 3 4 1 2 3 4 2 3 3 1 2 3 2 3 2
##: [3,] 0 0 0 0 0 1 1 1 1 2 2 3 1 1 1 2 2 2
##: [4,] 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 2
R(4, 9, include.zero = TRUE)
##: [1] 18
blockparts(1:4, 5)
##: [1,] 1 1 0 1 1 0 1 0 1 1 0 1 0 0 1 0 1 0 0 1 0 0
##: [2,] 2 1 2 2 1 2 0 1 2 1 2 0 1 0 1 2 0 1 0 0 1 0
##: [3,] 2 3 3 1 2 2 3 3 0 1 1 2 2 3 0 0 1 1 2 0 0 1
##: [4,] 0 0 0 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 4 4 4
S(1:4, 5)
##: [1] 22
compositions(5, 3)
##: [1,] 5 4 3 2 1 0 4 3 2 1 0 3 2 1 0 2 1 0 1 0 0
##: [2,] 0 1 2 3 4 5 0 1 2 3 4 0 1 2 3 0 1 2 0 1 0
##: [3,] 0 0 0 0 0 0 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
S(rep(5, 3), 5)
##: [1] 21
setparts(4)
##: [1,] 1 1 1 1 2 1 1 1 1 1 1 2 2 2 1
##: [2,] 1 1 1 2 1 2 1 2 2 1 2 1 1 3 2
##: [3,] 1 2 1 1 1 2 2 1 3 2 1 3 1 1 3
##: [4,] 1 1 2 1 1 1 2 2 1 3 3 1 3 1 4
setparts(c(1, 2, 2))
##: [1,] 1 1 1 1 1 1 1 1 1 1 1 1 3 3 3
##: [2,] 2 2 3 1 1 1 2 2 3 2 2 3 1 1 1
##: [3,] 3 2 2 3 2 2 1 1 1 3 2 2 2 1 2
##: [4,] 2 3 2 2 3 2 3 2 2 1 1 1 2 2 1
##: [5,] 1 1 1 2 2 3 2 3 2 2 3 2 1 2 2
multinomial(c(a = 1, b = 2, c = 1))
##: a 1 1 1 2 2 3 4 3 4 2 3 4
##: b 2 2 3 1 1 1 1 1 1 3 2 2
##: b 3 4 4 3 4 2 2 4 3 4 4 3
##: c 4 3 2 4 3 4 3 2 2 1 1 1