Skip to content
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

add combinations(), integer_partitions(), and partitions() to combinatorics.j #425

Merged
merged 1 commit into from
Feb 21, 2012

Conversation

robert-chiniquy
Copy link
Contributor

These are as described in TAoCP 7.2.1.3, 7.2.1.4, and 7.2.1.5.

All three use coroutines. Each produce() call, combinations() and integer_partitions() return Array, partitions() returns a set of sets. partitions() could have been written to return a generator which would return each set from an individual partitioning in turn, I wasn't sure what the preferred behavior would be so I went simple if clunky.

Here's my test script and the output:

macro task(ex); :(Task(()->$ex)); end

function test_combinations(a, t)
println(length(a), ',', t)
println("nCr: ", binomial(length(a), t))
for comb in @task combinations(a, t)
println(comb)
end
end

test_combinations(['a','b','c','d','e','f'], 4)

test_combinations(['a','b','c','d','e'], 4)
test_combinations(['a','b','c','d'], 3)
test_combinations(['a','b','c','d'], 2)
test_combinations(['a','b','c'], 2)
test_combinations(['a','b','c'], 1)
test_combinations(['a','b'], 1)

function test_integer_partitions(n, m)
println("n, m: ", n, ',', m)
for int_part in @task integer_partitions(n, m)
println(int_part)
end
end

for i = 4:5
for j = 2:4
test_integer_partitions(i, j)
end
end

function test_partitions(s)
println("s: ", s)
for part in @task partitions(s)
println(part)
end
end

test_partitions(['a', 'b', 'c', 'd'])

OUTPUT:

6,4
nCr: 15
{'a', 'b', 'c', 'd'}
{'a', 'b', 'c', 'e'}
{'a', 'b', 'd', 'e'}
{'a', 'c', 'd', 'e'}
{'b', 'c', 'd', 'e'}
{'a', 'b', 'c', 'f'}
{'a', 'b', 'd', 'f'}
{'a', 'c', 'd', 'f'}
{'b', 'c', 'd', 'f'}
{'a', 'b', 'e', 'f'}
{'a', 'c', 'e', 'f'}
{'b', 'c', 'e', 'f'}
{'a', 'd', 'e', 'f'}
{'b', 'd', 'e', 'f'}
{'c', 'd', 'e', 'f'}
5,4
nCr: 5
{'a', 'b', 'c', 'd'}
{'a', 'b', 'c', 'e'}
{'a', 'b', 'd', 'e'}
{'a', 'c', 'd', 'e'}
{'b', 'c', 'd', 'e'}
4,3
nCr: 4
{'a', 'b', 'c'}
{'a', 'b', 'd'}
{'a', 'c', 'd'}
{'b', 'c', 'd'}
4,2
nCr: 6
{'a', 'b'}
{'a', 'c'}
{'b', 'c'}
{'a', 'd'}
{'b', 'd'}
{'c', 'd'}
3,2
nCr: 3
{'a', 'b'}
{'a', 'c'}
{'b', 'c'}
3,1
nCr: 3
{'a'}
{'b'}
{'c'}
2,1
nCr: 2
{'a'}
{'b'}
n, m: 4,2
[3.0, 1.0]
[2.0, 2.0]
n, m: 4,3
[2.0, 1.0, 1.0]
n, m: 4,4
[1.0, 1.0, 1.0, 1.0]
n, m: 5,2
[4.0, 1.0]
[3.0, 2.0]
n, m: 5,3
[3.0, 1.0, 1.0]
[2.0, 2.0, 1.0]
n, m: 5,4
[2.0, 1.0, 1.0, 1.0]
s: ['a', 'b', 'c', 'd']
Set{Any}(Set{Any}('a','c','b','d'))
Set{Any}(Set{Any}('a','c','b'),Set{Any}('d'))
Set{Any}(Set{Any}('a','b','d'),Set{Any}('c'))
Set{Any}(Set{Any}('a','b'),Set{Any}('c','d'))
Set{Any}(Set{Any}('a','b'),Set{Any}('c'),Set{Any}('d'))
Set{Any}(Set{Any}('a','c','d'),Set{Any}('b'))
Set{Any}(Set{Any}('a','c'),Set{Any}('b','d'))
Set{Any}(Set{Any}('a','c'),Set{Any}('b'),Set{Any}('d'))
Set{Any}(Set{Any}('a','d'),Set{Any}('c','b'))
Set{Any}(Set{Any}('a'),Set{Any}('c','b','d'))
Set{Any}(Set{Any}('a'),Set{Any}('c','b'),Set{Any}('d'))
Set{Any}(Set{Any}('a','d'),Set{Any}('b'),Set{Any}('c'))
Set{Any}(Set{Any}('a'),Set{Any}('b','d'),Set{Any}('c'))
Set{Any}(Set{Any}('a'),Set{Any}('b'),Set{Any}('c','d'))
Set{Any}(Set{Any}('b'),Set{Any}('d'),Set{Any}('a'),Set{Any}('c'))

These are as described in TAoCP 7.2.1.3, 7.2.1.4, and 7.2.1.5. All three use coroutines. Each produce() call, combinations() and integer_partitions() return Array, partitions() returns a set of sets. partitions() could have been written to return a generator which would return each set from an individual partitioning in turn, I wasn't sure what the preferred behavior would be so I went simple if clunky.
ViralBShah added a commit that referenced this pull request Feb 21, 2012
add combinations(), integer_partitions(), and partitions() to combinatorics.j
@ViralBShah ViralBShah merged commit 9d4b4a0 into JuliaLang:master Feb 21, 2012
cmcaine pushed a commit to cmcaine/julia that referenced this pull request Nov 11, 2022
ViralBShah pushed a commit that referenced this pull request Aug 26, 2023
Stdlib: SparseArrays
URL: https://github.com/JuliaSparse/SparseArrays.jl.git
Stdlib branch: main
Julia branch: master
Old commit: 99c99b4
New commit: 54f4b39
Julia version: 1.11.0-DEV
SparseArrays version: 1.11.0
Bump invoked by: @ViralBShah
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaSparse/SparseArrays.jl@99c99b4...54f4b39

```
$ git log --oneline 99c99b4..54f4b39
54f4b39 Fix docs conflict when building as part of full Julia docs (#430)
a64ef4f Cleanup reloaded (#426)
4e2d1e4 Respect `IOContext` while displaying a `SparseMatrixCSC` (#423)
3d1eda9 Test suite: activate a temp project if we need to install Aqua.jl during the test suite (#425)
18b7fce Merge pull request #422 from JuliaSparse/jn/cat
e2c78b8 test: restore ambiguous test
68afc6e fix inference of SparseVector cat
c402d09 cat: ensure vararg is more inferrable
2c4f870 Fix some broken links (#421)
36a5308 bump version (#418)
```

Co-authored-by: Dilum Aluthge <dilum@aluthge.com>
Keno pushed a commit that referenced this pull request Oct 9, 2023
* Add ExprSplitter and remove `prepare_thunk` and `split_expressions`

The main motivation is to eliminate their need to `eval`, which feels
like a code smell.

* Restrict new releases to still-supported Julia versions

* Fix doctests
dkarrasch pushed a commit that referenced this pull request Nov 4, 2024
8c84b8c (#56431)

Stdlib: SparseArrays
URL: https://github.com/JuliaSparse/SparseArrays.jl.git
Stdlib branch: release-1.10
Julia branch: backports-release-1.10
Old commit: 279b363
New commit: 8c84b8c
Julia version: 1.10.6
SparseArrays version: 1.10.0(Does not match)
Bump invoked by: @IanButterworth
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaSparse/SparseArrays.jl@279b363...8c84b8c

```
$ git log --oneline 279b363..8c84b8c
8c84b8c Merge pull request #572 from JuliaSparse/backports-release-1.10
ec38631 Update ci.yml with more architectures
46c8f7e Merge branch 'release-1.10' into backports-release-1.10
2d762b3 Manual commit for PR #550 to backport to 1.10 (#577)
5c37298 Add versions to include arch
b539588 Update CI
fa49620 Disable nested dissection
d2a80a6 Change default QR tolerance to match SPQR (#557)
9b8cd14 SparseMatrixCSC constructor with a Tuple of Integers (#523)
546be18 Fix docs conflict when building as part of full Julia docs (#430)
30fbfc6 Test suite: activate a temp project if we need to install Aqua.jl during the test suite (#425)
91b0aa5 doc: move solvers doc to `src\solvers.md` (#576)
5d3724a Inline sparse-times-dense in-place multiplication
```

Co-authored-by: Dilum Aluthge <dilum@aluthge.com>
N5N3 pushed a commit that referenced this pull request Nov 11, 2024
8c84b8c (#56431)

Stdlib: SparseArrays
URL: https://github.com/JuliaSparse/SparseArrays.jl.git
Stdlib branch: release-1.10
Julia branch: backports-release-1.10
Old commit: 279b363
New commit: 8c84b8c
Julia version: 1.10.6
SparseArrays version: 1.10.0(Does not match)
Bump invoked by: @IanButterworth
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaSparse/SparseArrays.jl@279b363...8c84b8c

```
$ git log --oneline 279b363..8c84b8c
8c84b8c Merge pull request #572 from JuliaSparse/backports-release-1.10
ec38631 Update ci.yml with more architectures
46c8f7e Merge branch 'release-1.10' into backports-release-1.10
2d762b3 Manual commit for PR #550 to backport to 1.10 (#577)
5c37298 Add versions to include arch
b539588 Update CI
fa49620 Disable nested dissection
d2a80a6 Change default QR tolerance to match SPQR (#557)
9b8cd14 SparseMatrixCSC constructor with a Tuple of Integers (#523)
546be18 Fix docs conflict when building as part of full Julia docs (#430)
30fbfc6 Test suite: activate a temp project if we need to install Aqua.jl during the test suite (#425)
91b0aa5 doc: move solvers doc to `src\solvers.md` (#576)
5d3724a Inline sparse-times-dense in-place multiplication
```

Co-authored-by: Dilum Aluthge <dilum@aluthge.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants