-
Notifications
You must be signed in to change notification settings - Fork 59
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
RFC: vector partitions #128
Open
Stivanification
wants to merge
6
commits into
JuliaMath:master
Choose a base branch
from
Stivanification:Add-vector_partitions-method
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7dc8fb6
Add files via upload
Stivanification 20a4247
Merge branch 'JuliaMath:master' into Add-vector_partitions-method
Stivanification 2f981fc
added tests and cleaned up code
8deb3f7
removed incorrect initialization
380ff94
add test throwing DomainError if min has a negative entry
6245f53
add test for zero vector input
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
|
||
export vector_partitions | ||
|
||
function vector_partitions(vector::Vector{Int64},min=zeros(Integer,length(vector))) | ||
|
||
# Creates all vector partitions of "vector" with all parts greater than | ||
# or equal to "min" in lexicographic order recursively. | ||
# A vector partition of "vector" is a list of vectors with non-negative | ||
# integer entries whose sum is "vector". | ||
# EXAMPLES: | ||
# julia> vector_partitions([2, 2]) | ||
# 9-element Vector{Any}: | ||
# [[1, 0], [1, 0], [0, 1], [0, 1]] | ||
# [[2, 0], [0, 1], [0, 1]] | ||
# [[1, 1], [1, 0], [0, 1]] | ||
# [[2, 1], [0, 1]] | ||
# [[1, 0], [1, 0], [0, 2]] | ||
# [[2, 0], [0, 2]] | ||
# [[1, 2], [1, 0]] | ||
# [[1, 1], [1, 1]] | ||
# [[2, 2]] | ||
# | ||
# julia> vector_partitions([2,2],[1,0]) | ||
# 3-element Vector{Any}: | ||
# [[1, 2], [1, 0]] | ||
# [[1, 1], [1, 1]] | ||
# [[2, 2]] | ||
|
||
if minimum(min) < 0 | ||
throw(DomainError(min, "vector must have nonnegative entries")) | ||
end | ||
|
||
if minimum(vector) < 0 | ||
throw(DomainError(max, "vector must have nonnegative entries")) | ||
end | ||
|
||
vpartitions=[] | ||
|
||
if min == zeros(Integer,length(vector)) | ||
min = lexicographic_nonzero_minimum(vector) | ||
end | ||
|
||
if vector == zeros(Integer,length(vector)) | ||
vpartitions=[] | ||
else | ||
for vec in lexicographic_summand_range(vector,min) | ||
if vec == vector | ||
push!(vpartitions,[vector]) | ||
else | ||
for part in vector_partitions(vector-vec,vec) | ||
push!(part,vec) | ||
push!(vpartitions,part) | ||
end | ||
end | ||
end | ||
end | ||
|
||
return vpartitions | ||
|
||
end | ||
|
||
function lexicographic_summand_range(max::Vector{Int64},min=zeros(Integer,length(max))) | ||
|
||
# Return array of nonegative integer vectors which are componentwise dominated by | ||
# "max" and greater than or equal to "min" in lexicographic order. | ||
# | ||
# EXAMPLES:: | ||
# julia> lexicographic_summand_range([1,1,0],[0,2,0])) | ||
# 2-element Vector{Any}: | ||
# [1, 0, 0] | ||
# [1, 1, 0] | ||
# julia> lexicographic_summand_range([1,2]) | ||
# 6-element Vector{Any}: | ||
# [0, 0] | ||
# [0, 1] | ||
# [0, 2] | ||
# [1, 0] | ||
# [1, 1] | ||
# [1, 2] | ||
|
||
range = [] | ||
difference = max - min | ||
|
||
if difference == zeros(Integer,length(difference)) | ||
range = [max] | ||
elseif length(max) == 1 | ||
range =[[k] for k in min[1]:max[1]] | ||
else | ||
if (difference[findfirst(x->x!=0, difference)] < 0) | ||
range = [] | ||
else | ||
for vector in lexicographic_summand_range(max[2:end],min[2:end]) | ||
pushfirst!(vector,min[1]) | ||
push!(range,vector) | ||
end | ||
for j in (min[1]+1):max[1] | ||
for vector in lexicographic_summand_range(max[2:end]) | ||
pushfirst!(vector,j) | ||
push!(range,vector) | ||
end | ||
end | ||
end | ||
end | ||
|
||
return range | ||
end | ||
|
||
function lexicographic_nonzero_minimum(vector::Array{Int64}) | ||
# Returns the lexicographically smallest nonzero vector of the same length as | ||
# "vector" that is componentwise dominated by "vector". | ||
# EXAMPLES:: | ||
# julia> lexicographic_nonzero_minimum([2, 1]) | ||
# [0, 1] | ||
# julia> lexicographic_nonzero_minimum([2, 1, 0]) | ||
# [0, 1, 0] | ||
|
||
min = zeros(Integer,length(vector)) | ||
if vector != min | ||
min[findlast(x->x>0,vector)] = 1 | ||
end | ||
|
||
return min | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#vector_partitions | ||
@test collect(vector_partitions([1,1])) == Any[[[1,0],[0,1]],[[1,1]]] | ||
@test collect(vector_partitions([2,1])) == Any[[[1, 0], [1, 0], [0, 1]], | ||
[[2, 0], [0, 1]], | ||
[[1, 1], [1, 0]], | ||
[[2, 1]]] | ||
@test collect(vector_partitions([2,1],[0,2])) == Any[[[1,1],[1,0]],[[2,1]]] | ||
@test collect(vector_partitions([2,1],[3,2])) == [] | ||
|
||
@test_throws DomainError vector_partitions([-3,2]) | ||
@test_throws DomainError vector_partitions([3,2],[-2,-3]) | ||
|
||
@test length(vector_partitions([1,1],[2,3])) == 0 | ||
@test length(vector_partitions([0,0,0])) == 0 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this (and the similar comments in functions below) could be moved above the function as a doc string?