From 7dc8fb625238f1d91d10ee1c4e4826241878824f Mon Sep 17 00:00:00 2001 From: Stefan Rigger Date: Thu, 5 Jan 2017 17:00:02 +0100 Subject: [PATCH 1/3] Add files via upload This is a julia port of the sage VectorPartitions function originally written by Amritanshu Prasad (2013). A vector partition of "vec" is a list of vectors with non-negative integer entries whose sum is "vec". The method vector_partitions creates all vector partitions of "vector" with all parts greater than or equal to "min" in lexicographic order recursively. INPUT: - "vec" -- a list of non-negative integers. EXAMPLES: If "min" is not specified, then the class of all vector partitions of "vec" is created:: julia> vector_partitions([2, 2]) 9-element Array{Any,1}: Array{Int64,1}[[1,0],[1,0],[0,1],[0,1]] Array{Int64,1}[[2,0],[0,1],[0,1]] Array{Int64,1}[[1,1],[1,0],[0,1]] Array{Int64,1}[[2,1],[0,1]] Array{Int64,1}[[1,0],[1,0],[0,2]] Array{Int64,1}[[2,0],[0,2]] Array{Int64,1}[[1,2],[1,0]] Array{Int64,1}[[1,1],[1,1]] Array{Int64,1}[[2,2]] --- src/vector_partitions.jl | 136 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 src/vector_partitions.jl diff --git a/src/vector_partitions.jl b/src/vector_partitions.jl new file mode 100644 index 0000000..ebd053a --- /dev/null +++ b/src/vector_partitions.jl @@ -0,0 +1,136 @@ +# This is a julia version of the VectorPartitions function for sage by Amritanshu Prasad (2013) +# AUTHORS: Stefan Rigger, Gudmund Pammer (2017) + +export vector_partitions + +function find_min(vector::Array{Int64}) + # Return a string of 0's with one 1 at the location where the list + # "vector" has its last entry which is not equal to 0. + # INPUT: + # -"vector" -- An Int64-Array + # OUTPUT: + # A list of the same length with 0's everywhere, except for a 1 + # at the last position where "vector" has an entry not equal to 0. + # EXAMPLES:: + # julia> find_min([2, 1]) + # [0, 1] + # julia> find_min([2, 1, 0]) + # [0, 1, 0] + + min = zeros(Int64,length(vector)) + if vector != min + min[maximum(find(vector))] = 1 + end + + return min + +end + +function componentwise_minors(vector::Array{Int64},min=zeros(Int64,length(vector))) + + # Return array of integer vectors which are componentwise + # less than or equal to "vector" and lexicographically greater than or equal + # to "min". + # INPUT: + # - "vector" -- A list of non-negative integers + # - "min" -- A list of non-negative integers dominated lexicographically by "vector" + # OUTPUT: + # An array in lexicographic order of all integer arrays which are + # dominated elementwise by "vector" and are greater than or equal to "min" in + # lexicographic order. + # EXAMPLES:: + # julia> componentwise_minors([1, 1])) + # 4-element Array{Any,1}: + # [0,0] + # [0,1] + # [1,0] + # [1,1] + # julia> componentwise_minors([3,2],[2,4]) + # 3-element Array{Any,1}: + # [3,0] + # [3,1] + # [3,2] + + minors = [] + difference = vector - min + + if difference == zeros(Int64,length(difference)) + minors = [vector] + elseif length(vector) == 1 + minors =[[k] for k in min[1]:vector[1]] + else + # test if min is lexicographically less than vector + if (difference[minimum(find(difference))] < 0) + minors = [] + else + for vec in componentwise_minors(vector[2:end],min[2:end]) + unshift!(vec,min[1]) + push!(minors,vec) + end + for j in (min[1]+1):vector[1] + for vec in componentwise_minors(vector[2:end]) + unshift!(vec,j) + push!(minors,vec) + end + end + end + end + + return minors +end + +function vector_partitions(vector::Array{Int64},min=zeros(Int64,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 "vec" is a list of vectors with non-negative + # integer entries whose sum is "vec". + # INPUT: + # - "vec" -- a list of non-negative integers. + # EXAMPLES: + # If "min" is not specified, then the class of all vector partitions of + # "vec" is created:: + # julia> vector_partitions([2, 2]) + # 9-element Array{Any,1}: + # Array{Int64,1}[[1,0],[1,0],[0,1],[0,1]] + # Array{Int64,1}[[2,0],[0,1],[0,1]] + # Array{Int64,1}[[1,1],[1,0],[0,1]] + # Array{Int64,1}[[2,1],[0,1]] + # Array{Int64,1}[[1,0],[1,0],[0,2]] + # Array{Int64,1}[[2,0],[0,2]] + # Array{Int64,1}[[1,2],[1,0]] + # Array{Int64,1}[[1,1],[1,1]] + # Array{Int64,1}[[2,2]] + # + # If "min" is specified, then the array lists those vector + # partitions whose parts are all greater than or equal to "min" in + # lexicographic order:: + # julia> vector_partitions([2,2],[1,0]) + # 3-element Array{Any,1}: + # Array{Int64,1}[[1,2],[1,0]] + # Array{Int64,1}[[1,1],[1,1]] + # Array{Int64,1}[[2,2]] + + vpartitions=[] + + if min == zeros(Int64,length(vector)) + min = find_min(vector) + end + + if vector == zeros(Int64,length(vector)) + vpartitions = [] + else + for vec in componentwise_minors(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 From 19ef3276189a0110aca521550ebe29368044c083 Mon Sep 17 00:00:00 2001 From: Stefan Rigger Date: Mon, 13 Feb 2017 14:12:44 +0100 Subject: [PATCH 2/3] Update comment formatting in vector_partitions.jl --- src/vector_partitions.jl | 132 +++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/src/vector_partitions.jl b/src/vector_partitions.jl index ebd053a..26ee372 100644 --- a/src/vector_partitions.jl +++ b/src/vector_partitions.jl @@ -4,19 +4,19 @@ export vector_partitions function find_min(vector::Array{Int64}) - # Return a string of 0's with one 1 at the location where the list - # "vector" has its last entry which is not equal to 0. - # INPUT: - # -"vector" -- An Int64-Array - # OUTPUT: - # A list of the same length with 0's everywhere, except for a 1 - # at the last position where "vector" has an entry not equal to 0. - # EXAMPLES:: - # julia> find_min([2, 1]) - # [0, 1] - # julia> find_min([2, 1, 0]) - # [0, 1, 0] - + "Return a string of 0's with one 1 at the location where the list + 'vector' has its last entry which is not equal to 0. + INPUT: + -"vector" -- An Int64-Array + OUTPUT: + A list of the same length with 0's everywhere, except for a 1 + at the last position where "vector" has an entry not equal to 0. + EXAMPLES:: + julia> find_min([2, 1]) + [0, 1] + julia> find_min([2, 1, 0]) + [0, 1, 0] + " min = zeros(Int64,length(vector)) if vector != min min[maximum(find(vector))] = 1 @@ -28,29 +28,29 @@ end function componentwise_minors(vector::Array{Int64},min=zeros(Int64,length(vector))) - # Return array of integer vectors which are componentwise - # less than or equal to "vector" and lexicographically greater than or equal - # to "min". - # INPUT: - # - "vector" -- A list of non-negative integers - # - "min" -- A list of non-negative integers dominated lexicographically by "vector" - # OUTPUT: - # An array in lexicographic order of all integer arrays which are - # dominated elementwise by "vector" and are greater than or equal to "min" in - # lexicographic order. - # EXAMPLES:: - # julia> componentwise_minors([1, 1])) - # 4-element Array{Any,1}: - # [0,0] - # [0,1] - # [1,0] - # [1,1] - # julia> componentwise_minors([3,2],[2,4]) - # 3-element Array{Any,1}: - # [3,0] - # [3,1] - # [3,2] - + " Return array of integer vectors which are componentwise + less than or equal to "vector" and lexicographically greater than or equal + to "min". + INPUT: + - "vector" -- A list of non-negative integers + - "min" -- A list of non-negative integers dominated lexicographically by "vector" + OUTPUT: + An array in lexicographic order of all integer arrays which are + dominated elementwise by "vector" and are greater than or equal to "min" in + lexicographic order. + EXAMPLES:: + julia> componentwise_minors([1, 1])) + 4-element Array{Any,1}: + [0,0] + [0,1] + [1,0] + [1,1] + julia> componentwise_minors([3,2],[2,4]) + 3-element Array{Any,1}: + [3,0] + [3,1] + [3,2] + " minors = [] difference = vector - min @@ -81,36 +81,36 @@ end function vector_partitions(vector::Array{Int64},min=zeros(Int64,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 "vec" is a list of vectors with non-negative - # integer entries whose sum is "vec". - # INPUT: - # - "vec" -- a list of non-negative integers. - # EXAMPLES: - # If "min" is not specified, then the class of all vector partitions of - # "vec" is created:: - # julia> vector_partitions([2, 2]) - # 9-element Array{Any,1}: - # Array{Int64,1}[[1,0],[1,0],[0,1],[0,1]] - # Array{Int64,1}[[2,0],[0,1],[0,1]] - # Array{Int64,1}[[1,1],[1,0],[0,1]] - # Array{Int64,1}[[2,1],[0,1]] - # Array{Int64,1}[[1,0],[1,0],[0,2]] - # Array{Int64,1}[[2,0],[0,2]] - # Array{Int64,1}[[1,2],[1,0]] - # Array{Int64,1}[[1,1],[1,1]] - # Array{Int64,1}[[2,2]] - # - # If "min" is specified, then the array lists those vector - # partitions whose parts are all greater than or equal to "min" in - # lexicographic order:: - # julia> vector_partitions([2,2],[1,0]) - # 3-element Array{Any,1}: - # Array{Int64,1}[[1,2],[1,0]] - # Array{Int64,1}[[1,1],[1,1]] - # Array{Int64,1}[[2,2]] - + " Creates all vector partitions of "vector" with all parts greater than + or equal to "min" in lexicographic order recursively. + A vector partition of "vec" is a list of vectors with non-negative + integer entries whose sum is "vec". + INPUT: + - "vec" -- a list of non-negative integers. + EXAMPLES: + If "min" is not specified, then the class of all vector partitions of + "vec" is created:: + julia> vector_partitions([2, 2]) + 9-element Array{Any,1}: + Array{Int64,1}[[1,0],[1,0],[0,1],[0,1]] + Array{Int64,1}[[2,0],[0,1],[0,1]] + Array{Int64,1}[[1,1],[1,0],[0,1]] + Array{Int64,1}[[2,1],[0,1]] + Array{Int64,1}[[1,0],[1,0],[0,2]] + Array{Int64,1}[[2,0],[0,2]] + Array{Int64,1}[[1,2],[1,0]] + Array{Int64,1}[[1,1],[1,1]] + Array{Int64,1}[[2,2]] + + If "min" is specified, then the array lists those vector + partitions whose parts are all greater than or equal to "min" in + lexicographic order:: + julia> vector_partitions([2,2],[1,0]) + 3-element Array{Any,1}: + Array{Int64,1}[[1,2],[1,0]] + Array{Int64,1}[[1,1],[1,1]] + Array{Int64,1}[[2,2]] + " vpartitions=[] if min == zeros(Int64,length(vector)) From 7ceb259b8a7ed6fb0ce9fbf6b92939d043dcc40a Mon Sep 17 00:00:00 2001 From: Stefan Rigger Date: Mon, 26 Dec 2022 11:39:08 +0100 Subject: [PATCH 3/3] bugfix: include empty combination, update tests --- src/combinations.jl | 4 +- src/vector_partitions.jl | 136 --------------------------------------- test/combinations.jl | 5 +- 3 files changed, 4 insertions(+), 141 deletions(-) delete mode 100644 src/vector_partitions.jl diff --git a/src/combinations.jl b/src/combinations.jl index 60f449f..6a49a00 100644 --- a/src/combinations.jl +++ b/src/combinations.jl @@ -56,9 +56,7 @@ end Generate combinations of the elements of `a` of all orders. Chaining of order iterators is eager, but the sequence at each order is lazy. """ -combinations(a) = Iterators.flatten([combinations(a, k) for k = 1:length(a)]) - - +combinations(a) = Iterators.flatten([combinations(a, k) for k = 0:length(a)]) # cool-lex combinations iterator diff --git a/src/vector_partitions.jl b/src/vector_partitions.jl deleted file mode 100644 index 26ee372..0000000 --- a/src/vector_partitions.jl +++ /dev/null @@ -1,136 +0,0 @@ -# This is a julia version of the VectorPartitions function for sage by Amritanshu Prasad (2013) -# AUTHORS: Stefan Rigger, Gudmund Pammer (2017) - -export vector_partitions - -function find_min(vector::Array{Int64}) - "Return a string of 0's with one 1 at the location where the list - 'vector' has its last entry which is not equal to 0. - INPUT: - -"vector" -- An Int64-Array - OUTPUT: - A list of the same length with 0's everywhere, except for a 1 - at the last position where "vector" has an entry not equal to 0. - EXAMPLES:: - julia> find_min([2, 1]) - [0, 1] - julia> find_min([2, 1, 0]) - [0, 1, 0] - " - min = zeros(Int64,length(vector)) - if vector != min - min[maximum(find(vector))] = 1 - end - - return min - -end - -function componentwise_minors(vector::Array{Int64},min=zeros(Int64,length(vector))) - - " Return array of integer vectors which are componentwise - less than or equal to "vector" and lexicographically greater than or equal - to "min". - INPUT: - - "vector" -- A list of non-negative integers - - "min" -- A list of non-negative integers dominated lexicographically by "vector" - OUTPUT: - An array in lexicographic order of all integer arrays which are - dominated elementwise by "vector" and are greater than or equal to "min" in - lexicographic order. - EXAMPLES:: - julia> componentwise_minors([1, 1])) - 4-element Array{Any,1}: - [0,0] - [0,1] - [1,0] - [1,1] - julia> componentwise_minors([3,2],[2,4]) - 3-element Array{Any,1}: - [3,0] - [3,1] - [3,2] - " - minors = [] - difference = vector - min - - if difference == zeros(Int64,length(difference)) - minors = [vector] - elseif length(vector) == 1 - minors =[[k] for k in min[1]:vector[1]] - else - # test if min is lexicographically less than vector - if (difference[minimum(find(difference))] < 0) - minors = [] - else - for vec in componentwise_minors(vector[2:end],min[2:end]) - unshift!(vec,min[1]) - push!(minors,vec) - end - for j in (min[1]+1):vector[1] - for vec in componentwise_minors(vector[2:end]) - unshift!(vec,j) - push!(minors,vec) - end - end - end - end - - return minors -end - -function vector_partitions(vector::Array{Int64},min=zeros(Int64,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 "vec" is a list of vectors with non-negative - integer entries whose sum is "vec". - INPUT: - - "vec" -- a list of non-negative integers. - EXAMPLES: - If "min" is not specified, then the class of all vector partitions of - "vec" is created:: - julia> vector_partitions([2, 2]) - 9-element Array{Any,1}: - Array{Int64,1}[[1,0],[1,0],[0,1],[0,1]] - Array{Int64,1}[[2,0],[0,1],[0,1]] - Array{Int64,1}[[1,1],[1,0],[0,1]] - Array{Int64,1}[[2,1],[0,1]] - Array{Int64,1}[[1,0],[1,0],[0,2]] - Array{Int64,1}[[2,0],[0,2]] - Array{Int64,1}[[1,2],[1,0]] - Array{Int64,1}[[1,1],[1,1]] - Array{Int64,1}[[2,2]] - - If "min" is specified, then the array lists those vector - partitions whose parts are all greater than or equal to "min" in - lexicographic order:: - julia> vector_partitions([2,2],[1,0]) - 3-element Array{Any,1}: - Array{Int64,1}[[1,2],[1,0]] - Array{Int64,1}[[1,1],[1,1]] - Array{Int64,1}[[2,2]] - " - vpartitions=[] - - if min == zeros(Int64,length(vector)) - min = find_min(vector) - end - - if vector == zeros(Int64,length(vector)) - vpartitions = [] - else - for vec in componentwise_minors(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 diff --git a/test/combinations.jl b/test/combinations.jl index 2e7c74c..396c14f 100644 --- a/test/combinations.jl +++ b/test/combinations.jl @@ -1,5 +1,6 @@ -@test [combinations([])...] == [] -@test [combinations(['a', 'b', 'c'])...] == [['a'],['b'],['c'],['a','b'],['a','c'],['b','c'],['a','b','c']] +@test [combinations([])...] == [[]] +@test [combinations(['a', 'b', 'c'])...] == [[],['a'],['b'],['c'],['a','b'],['a','c'],['b','c'],['a','b','c']] +@test length(collect(combinations(1:5))) == 32 @test [combinations("abc",3)...] == [['a','b','c']] @test [combinations("abc",2)...] == [['a','b'],['a','c'],['b','c']]