-
Notifications
You must be signed in to change notification settings - Fork 140
Import Multipartition
functionality from JuLie
#4746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
9606ce6
6abec3f
6f6a1ee
80a3163
f14021d
983b91d
0ecec77
05b2871
252fb8e
1ab0cd8
4769f5f
ebea4c1
bd6221b
ec306ed
e834620
c7a6620
e910ed7
6a377d4
b984af5
2329bbb
18463a5
f2c1f7c
1ce301b
086fcde
74dd089
68c9d97
5265721
e87b426
1768d5e
56b28e4
6eaca86
c27ab68
8df8e2d
1e2b98b
4f79617
c393a30
e61fa14
e5023dc
3f1abc1
4aabd02
5bfa9f0
3b8cf31
cd6bda6
10e381f
c69b1d1
c00e546
1a30b62
9fc395a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,168 @@ | ||||||
################################################################################ | ||||||
# Multipartitions. | ||||||
# | ||||||
# Copyright (C) 2020 Ulrich Thiel, ulthiel.com/math | ||||||
################################################################################ | ||||||
|
||||||
|
||||||
|
||||||
|
||||||
""" | ||||||
multipartition(mp::Vector{Vector{T}}) where T <: IntegerUnion | ||||||
|
||||||
Return the multipartition given by the vector `mp` of integer sequences `mp` | ||||||
(which are interpreted as integer partitions) as an object of type | ||||||
`Multipartition{T}`. | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
The element type `T` may be optionally specified, see also the examples below. | ||||||
|
||||||
# Examples | ||||||
```julia-repl | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
julia> P=Multipartition( [[2,1], [], [3,2,1]] ) | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
Partition{Int64}[[2, 1], [], [3, 2, 1]] | ||||||
julia> sum(P) | ||||||
9 | ||||||
julia> P[2] | ||||||
Int64[] | ||||||
julia> P=Multipartition( Vector{Int8}[[2,1], [], [3,2,1]] ) | ||||||
Partition{Int8}[[2, 1], [], [3, 2, 1]] | ||||||
``` | ||||||
|
||||||
# References | ||||||
1. Wikipedia, [Multipartition](https://en.wikipedia.org/wiki/Multipartition) | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
""" | ||||||
function multipartition(mp::Vector{Vector{T}}) where T <: IntegerUnion | ||||||
return Multipartition([partition(p) for p in mp]) | ||||||
end | ||||||
|
||||||
# This is only called when the empty array is part of mp (because then it's | ||||||
# "Any" type and not of Integer type). | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
function multipartition(mp::Vector{Vector{Any}}) | ||||||
return Multipartition([partition(p) for p in mp]) | ||||||
end | ||||||
|
||||||
|
||||||
function Base.show(io::IO, ::MIME"text/plain", MP::Multipartition) | ||||||
print(io, MP.mp) | ||||||
end | ||||||
|
||||||
function Base.size(MP::Multipartition) | ||||||
return size(MP.mp) | ||||||
end | ||||||
|
||||||
function Base.length(MP::Multipartition) | ||||||
return length(MP.mp) | ||||||
end | ||||||
|
||||||
function Base.getindex(MP::Multipartition, i::Int) | ||||||
return getindex(MP.mp,i) | ||||||
end | ||||||
|
||||||
|
||||||
""" | ||||||
sum(P::Multipartition{T}) where T<:Integer | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
If P is a multipartition of the integer n, this function returns n. | ||||||
""" | ||||||
function Base.sum(P::Multipartition{T}) where T<:Integer | ||||||
s = zero(T) | ||||||
for i=1:length(P) | ||||||
s += sum(P[i]) | ||||||
end | ||||||
return s | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
end | ||||||
|
||||||
|
||||||
""" | ||||||
multipartitions(n::T, r::Integer) where T<:Integer | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
A list of all r-component multipartitions of n. The algorithm is recursive and based on [`partitions(::Integer)`](@ref). | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
# Example | ||||||
```julia-repl | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
julia> multipartitions(2,2) | ||||||
5-element Vector{Multipartition{Int64}}: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this perhaps like this?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But CI test pass... It seems I am misunderstanding something... huh There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that it seems like this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah okay, I understand the confusion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahah I get it now:
So my confusion is that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can do this but I'm not sure what it should look like. We have the same issue with julia> P = partition(6, 4, 4, 2)
[6, 4, 4, 2] |
||||||
Partition{Int64}[[], [2]] | ||||||
Partition{Int64}[[], [1, 1]] | ||||||
Partition{Int64}[[1], [1]] | ||||||
Partition{Int64}[[2], []] | ||||||
Partition{Int64}[[1, 1], []] | ||||||
``` | ||||||
""" | ||||||
function multipartitions(n::T, r::IntegerUnion) where T<:IntegerUnion | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
#Argument checking | ||||||
n >= 0 || throw(ArgumentError("n >= 0 required")) | ||||||
r >= 1 || throw(ArgumentError("r >= 1 required")) | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
#This will be the list of multipartitions | ||||||
MP = Multipartition{T}[] | ||||||
|
||||||
#We will go through all compositions of n into r parts, and for each such #composition, we collect all the partitions for each of the components of | ||||||
#the composition. | ||||||
#We create the compositions here in place for efficiency. | ||||||
|
||||||
#recursively produces all Integer Vectors p of length r such that the sum of all the Elements equals n. Then calls recMultipartitions! | ||||||
function recP!(p::Vector{T}, i::T, n::T) #where T<:Integer | ||||||
if i==length(p) || n==0 | ||||||
p[i] = n | ||||||
recMultipartitions!(fill(Partition(T[]),r), p, T(1)) | ||||||
else | ||||||
for j=0:n | ||||||
p[i] = T(j) | ||||||
recP!(copy(p), T(i+1), T(n-j)) | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
#recursively produces all multipartitions such that the i-th partition sums up to p[i] | ||||||
function recMultipartitions!(mp::Vector{Partition{T}}, p::Vector{T}, i::T) #where T<:Integer | ||||||
if i == length(p) | ||||||
for q in partitions(p[i]) | ||||||
mp[i] = q | ||||||
push!(MP, Multipartition{T}(copy(mp))) | ||||||
end | ||||||
else | ||||||
for q in partitions(p[i]) | ||||||
mp[i] = q | ||||||
recMultipartitions!(copy(mp), p, T(i+1)) | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
recP!(zeros(T,r), T(1), n) | ||||||
return MP | ||||||
end | ||||||
|
||||||
|
||||||
@doc raw""" | ||||||
num_multipartitions(n::Int, k::Int) | ||||||
|
||||||
The number of multipartitions of ``n`` into ``k`` parts is equal to | ||||||
```math | ||||||
\sum_{a=1}^k {k \choose a} \sum_{λ} p(λ₁) p(λ₂) ⋯ p(λ_a) \;, | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
``` | ||||||
where the second sum is over all [compositions](@ref Compositions) ``λ`` of ``n`` into ``a`` parts. I found this formula in the Proof of Lemma 2.4 in Craven (2006). | ||||||
|
||||||
# References | ||||||
1. Craven, D. (2006). The Number of t-Cores of Size n. [http://web.mat.bham.ac.uk/D.A.Craven/docs/papers/tcores0608.pdf](http://web.mat.bham.ac.uk/D.A.Craven/docs/papers/tcores0608.pdf) | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
""" | ||||||
function num_multipartitions(n::Int, k::Int) | ||||||
|
||||||
z = ZZ(0) | ||||||
|
||||||
# Special cases | ||||||
if n==0 | ||||||
return ZZ(k) | ||||||
end | ||||||
|
||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
for a=1:k | ||||||
w = ZZ(0) | ||||||
for λ in compositions(n,a) | ||||||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
w += prod([num_partitions(λ[i]) for i=1:length(λ)]) | ||||||
end | ||||||
z += binomial(k,a)*w | ||||||
end | ||||||
|
||||||
return z | ||||||
|
||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
@testset "Multipartitions" begin | ||
mjrodgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#constructors | ||
@test Multipartition([[3,2],[1]]) == Multipartition([Partition([3,2]),Partition([1])]) | ||
@test Multipartition([[3,2],[]]) == Multipartition([Partition([3,2]),Partition([])]) | ||
|
||
# multi-partitions | ||
check = true | ||
N = 0:10 | ||
R = 1:5 | ||
for n in N | ||
for r in R | ||
MP = multipartitions(n,r) | ||
# check that all multipartitions are distinct | ||
if MP != unique(MP) | ||
check = false | ||
break | ||
end | ||
# check that multipartititons are really multipartitions of n | ||
for mp in MP | ||
if sum(mp) != n | ||
check = false | ||
break | ||
end | ||
end | ||
# check that all multisetpartitions have k parts | ||
if length(MP) !=0 && unique([ length(mp) for mp in MP ]) != [r] | ||
check = false | ||
break | ||
end | ||
end | ||
end | ||
@test check==true | ||
|
||
#counting | ||
for n=0:5 | ||
for k=1:n+1 | ||
@test length(multipartitions(n,k)) == num_multipartitions(n,k) | ||
end | ||
end | ||
|
||
end |
Uh oh!
There was an error while loading. Please reload this page.