-
Notifications
You must be signed in to change notification settings - Fork 176
Add multicombinations to Oscar
#5477
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
61c4c63
add multicombinations iterator
mjrodgers 89fd839
fix export
mjrodgers 9dd1331
Add tests, fix edgecase
mjrodgers d8ffb8c
remove `Experimental` version of `multicombinations`
mjrodgers 896aca8
formatting
mjrodgers 9ea1b38
formatting?
mjrodgers 82a6e2d
fix spaces in doctests
mjrodgers 589fcf8
some typos in tests
mjrodgers be6453b
fix failing test
mjrodgers 605fa8f
Apply suggestions from code review
fingolfin 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
86 changes: 86 additions & 0 deletions
86
src/Combinatorics/EnumerativeCombinatorics/multicombinations.jl
This file contains hidden or 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,86 @@ | ||
| @doc raw""" | ||
| multicombinations(n::IntegerUnion, k::IntegerUnion) | ||
|
|
||
| Return an iterator over all $k$-combinations of ${1,...,n}$ with repetition, | ||
| produced in lexicographically ascending order. | ||
|
|
||
| # Examples | ||
|
|
||
| ```jldoctest | ||
| julia> C = multicombinations(4, 2) | ||
| Iterator over the 2-combinations of 1:4 with repetition | ||
|
|
||
| julia> collect(C) | ||
| 10-element Vector{Combination{Int64}}: | ||
| [1, 1] | ||
| [1, 2] | ||
| [1, 3] | ||
| [1, 4] | ||
| [2, 2] | ||
| [2, 3] | ||
| [2, 4] | ||
| [3, 3] | ||
| [3, 4] | ||
| [4, 4] | ||
| ``` | ||
| """ | ||
| multicombinations(n::T, k::T) where T<:IntegerUnion = MultiCombinations(Base.oneto(n), n, k) | ||
|
|
||
|
|
||
| @doc raw""" | ||
| multicombinations(v::AbstractVector{T}, k::Integer) where {T} | ||
|
|
||
| Return an iterator over all combinations of `k` elements of `v` with repetitions. | ||
| In each iteration, the elements are returned in the order they appear in `v`. | ||
| The order of the combinations is lexicographic. | ||
|
|
||
| ```jldoctest | ||
| julia> collect(multicombinations(['a', 'b', 'c'], 2)) | ||
| 6-element Vector{Combination{Char}}: | ||
| ['a', 'a'] | ||
| ['a', 'b'] | ||
| ['a', 'c'] | ||
| ['b', 'b'] | ||
| ['b', 'c'] | ||
| ['c', 'c'] | ||
| ``` | ||
| """ | ||
| multicombinations(v::AbstractVector, k::IntegerUnion) = MultiCombinations(v, k) | ||
|
|
||
| MultiCombinations(v::AbstractArray, k::T) where {T<:IntegerUnion} = MultiCombinations(v, T(length(v)), k) | ||
|
|
||
|
|
||
| @inline function Base.iterate(C::MultiCombinations{<:AbstractVector{T}, U}, state::Vector{U} = C.k == 0 ? U[] : (s = ones(U, C.k); s[Int(C.k)] = U(0); s)) where {T, U<:IntegerUnion} | ||
| if is_zero(C.k) # special case to generate 1 result for k = 0 | ||
| if isempty(state) | ||
| return Combination{T}(T[]), U[0] | ||
| end | ||
| return nothing | ||
| end | ||
| for i in C.k:U(-1):U(1) | ||
| state[i] += 1 | ||
| if state[i] > C.n | ||
| continue | ||
| end | ||
| for j in i+1:C.k | ||
| state[j] = state[i] | ||
| end | ||
| break | ||
| end | ||
| if state[1] > C.n | ||
| return nothing | ||
| end | ||
| return Combination{T}(C.v[state]), state | ||
| end | ||
|
|
||
| Base.length(C::MultiCombinations) = binomial(Int(C.n)+Int(C.k)-1, Int(C.k)) | ||
|
|
||
| Base.eltype(::Type{<:MultiCombinations{T}}) where {T} = Combination{eltype(T)} | ||
|
|
||
| function Base.show(io::IO, C::MultiCombinations{<:Base.OneTo}) | ||
| print(io, "Iterator over the ", C.k, "-combinations of ", 1:C.n, " with repetition") | ||
| end | ||
|
|
||
| function Base.show(io::IO, C::MultiCombinations) | ||
| print(io, "Iterator over the ", C.k, "-combinations of ", C.v, " with repetition") | ||
| end |
This file contains hidden or 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 hidden or 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
85 changes: 85 additions & 0 deletions
85
test/Combinatorics/EnumerativeCombinatorics/multicombinations.jl
This file contains hidden or 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,85 @@ | ||
| @testset "multicombinations" begin | ||
| let | ||
| C = multicombinations(3, 2) | ||
| @test length(C) == 6 | ||
| @test collect(C) == [[1, 1], | ||
| [1, 2], | ||
| [1, 3], | ||
| [2, 2], | ||
| [2, 3], | ||
| [3, 3]] | ||
| @test eltype(C) === Combination{Int} | ||
| C = multicombinations(100, 3) | ||
| @test length(C) == binomial(102, 3) | ||
|
|
||
| C = multicombinations(Int16(2), Int16(2)) | ||
| @test length(C) == 3 | ||
| @test collect(C) == [[1, 1], | ||
| [1, 2], | ||
| [2, 2]] | ||
| @test eltype(C) === Combination{Int16} | ||
|
|
||
| C = multicombinations('a':'c', 3) | ||
| @test length(C) == 10 | ||
| @test collect(C) == [['a', 'a', 'a'], | ||
| ['a', 'a', 'b'], | ||
| ['a', 'a', 'c'], | ||
| ['a', 'b', 'b'], | ||
| ['a', 'b', 'c'], | ||
| ['a', 'c', 'c'], | ||
| ['b', 'b', 'b'], | ||
| ['b', 'b', 'c'], | ||
| ['b', 'c', 'c'], | ||
| ['c', 'c', 'c']] | ||
| @test eltype(C) === Combination{Char} | ||
| end | ||
|
|
||
| for n in 1:5, k in 1:n | ||
| @test collect(multicombinations(n, k)) == collect(multicombinations(1:n, k)) | ||
| end | ||
|
|
||
| @test collect(multicombinations(["1", "2", "3", "4"], 0)) == [String[]] | ||
| @test collect(multicombinations(["1", "2", "3", "4"], 1)) == [["1"], ["2"], ["3"], ["4"]] | ||
| @test collect(multicombinations(["1", "2", "3", "4"], 2)) == [ | ||
| ["1", "1"], | ||
| ["1", "2"], | ||
| ["1", "3"], | ||
| ["1", "4"], | ||
| ["2", "2"], | ||
| ["2", "3"], | ||
| ["2", "4"], | ||
| ["3", "3"], | ||
| ["3", "4"], | ||
| ["4", "4"], | ||
| ] | ||
| @test collect(multicombinations(["1", "2", "3", "4"], 3)) == [ | ||
| ["1", "1", "1"], | ||
| ["1", "1", "2"], | ||
| ["1", "1", "3"], | ||
| ["1", "1", "4"], | ||
| ["1", "2", "2"], | ||
| ["1", "2", "3"], | ||
| ["1", "2", "4"], | ||
| ["1", "3", "3"], | ||
| ["1", "3", "4"], | ||
| ["1", "4", "4"], | ||
| ["2", "2", "2"], | ||
| ["2", "2", "3"], | ||
| ["2", "2", "4"], | ||
| ["2", "3", "3"], | ||
| ["2", "3", "4"], | ||
| ["2", "4", "4"], | ||
| ["3", "3", "3"], | ||
| ["3", "3", "4"], | ||
| ["3", "4", "4"], | ||
| ["4", "4", "4"], | ||
| ] | ||
|
|
||
| v = collect(multicombinations(5, 3)) | ||
| @test issorted(v) | ||
| @test allunique(v) | ||
|
|
||
| @test collect(multicombinations(0, 0)) == [Int[]] | ||
| @test collect(multicombinations(0, 1)) == [] | ||
|
|
||
| end | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.