A weak composition of a non-negative integer n is a sequence λ1,…,λk of non-negative integers λi such that n=λ1+⋯+λk. A composition of n is a weak composition consisting of positive integers. The λi are called the parts of the (weak) composition.
weak_composition(parts::Vector{T}; check::Bool = true) where T <: IntegerUnion
Return the weak composition given by the integer sequence parts as an object of type WeakComposition{T}.
If check is true (default), it is checked whether the given sequence defines a weak composition, that is, whether all elements of parts are non-negative.
Examples
julia> W = weak_composition([6, 0, 2, 3]) # the weak composition 6, 0, 2, 3 of 11[6, 0, 2, 3]
julia> W = weak_composition(Int8[6, 0, 2, 3]) # save the elements in 8-bit integersInt8[6, 0, 2, 3]
composition(parts::Vector{T}; check::Bool = true) where T <: IntegerUnion
Return the composition given by the integer sequence parts as an object of type Composition{T}.
If check is true (default), it is checked whether the given sequence defines a composition, that is, whether all elements of parts are positive.
Examples
julia> C = composition([6, 1, 2, 3]) # the composition 6, 1, 2, 3 of 12[6, 1, 2, 3]
julia> C = composition(Int8[6, 1, 2, 3]) # save the elements in 8-bit integersInt8[6, 1, 2, 3]
Note that an integer n has infinitely many weak compositions as one may always append zeros to the end of a given weak composition. Without restrictions on the number of parts, we can hence only generate compositions, but not weak compositions.
Return an iterator over all compositions of a non-negative integer n into k parts, produced in lexicographically descending order.
By a composition of n into k parts we mean a sequence of k positive integers whose sum is n.
Examples
julia> C = compositions(4, 2)
Iterator over the compositions of 4 into 2 parts
julia> collect(C)
3-element Vector{Composition{Int64}}:
[3, 1]
[2, 2]
[1, 3]
Return an iterator over all weak compositions of a non-negative integer n into k parts, produced in lexicographically descending order. Using a smaller integer type for n (e.g. Int8) may increase performance.
By a weak composition of n into k parts we mean a sequence of k non-negative integers whose sum is n.
Examples
julia> W = weak_compositions(3, 2)
Iterator over the weak compositions of 3 into 2 parts
julia> length(W)
4
julia> collect(W)
4-element Vector{WeakComposition{Int64}}:
[3, 0]
[2, 1]
[1, 2]
[0, 3]