Learn R Programming

partitions (version 1.10-9)

parts: 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 blockparts(), the default of NULL means to return all partitions of any size

m

In functions restrictedparts() and compositions(), the order of the partition

include.zero

In functions restrictedparts() and compositions(), Boolean with default FALSE meaning to include only partitions of \(n\) into exactly \(m\) parts; and TRUE meaning to include partitions of \(n\) into at most \(m\) parts (because zero parts are included)

include.fewer

In function blockparts(), Boolean with default FALSE meaning to return vectors whose sum is exactly n and TRUE meaning to return partitions whose sum is at most n

decreasing

In restrictedparts(), Boolean with default TRUE meaning to return partitions whose parts are in decreasing order and FALSE meaning to return partitions in lexicographical order, as appearing in Hindenburg's algorithm. Note that setting to decreasing to FALSE has the effect of making conjugate() return garbage

f

In function blockparts(), a vector of strictly positive integers that gives the maximal number of blocks; see details

v

An integer vector. In function multiset(), argument v represents a multiset (argument n is the size of the sample to be taken). In function multinomial(), v is specifies the sizes of the sets; use a named vector for easier interpretation

k

In function allbinom(), the size of the set to be chosen; arguments match those of choose()

Author

Robin K. S. Hankin

Details

  • Function parts() uses the algorithm in Andrews. Function diffparts() 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 of c(rep(0,m-1),n).

    Functions parts() and restrictedparts() overlap in functionality. Note, however, that they can return identical partitions but in a different order: parts(6) and restrictedparts(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 argument y includes zero elements, these are treated consistently (ie a position with zero capacity).

    If n takes its default value of NULL, 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 functions durfee() and conjugate() may fail.

  • With a single argument, compositions(n) returns all \(2^{n-1}\) ways of partitioning an integer; thus 4+1+1 is distinct from 1+4+1 or 1+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(v) returns all ways of ordering multiset v. Optional argument n specifies the size of the subset to take (mset() is a low-level helper function). File README.Rmd shows how to use multiset() to reproduce freealg::pepper(), which gives related functionality.

  • Function multinomial(v) returns all ways of partitioning a set into distinguishable boxes of capacities v[1], v[2],...,v[n] (a named vector gives more understandable output). 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 from n.

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

nextpart

Examples

Run this code
    parts(7)
    P(7)
    diffparts(9)
    Q(9)
    restrictedparts(9,4)
    R(4,9,include.zero=TRUE)
    blockparts(1:4,5)
    S(1:4,5)
    compositions(5,3)
    S(rep(5,3),5)
    setparts(4)
    setparts(c(1,2,2))
    restrictedsetparts(c(a=2,b=1,c=1))
    multinomial(c(a=2,b=1,c=1))
    multiset(c(9,5,5,5,6))
    multiset(c(9,5,5,5,6),3)
  

Run the code above in your browser using DataLab