Skip to content

Commit 069a5e3

Browse files
authored
add documentation for combinations (#5390)
1 parent 3b0a632 commit 069a5e3

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

docs/doc.main

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@
291291
"Combinatorics/simplicialcomplexes.md",
292292
"Combinatorics/phylogenetic_trees.md",
293293
"Enumerative combinatorics" => [
294+
"Combinatorics/EnumerativeCombinatorics/combinations.md",
294295
"Combinatorics/EnumerativeCombinatorics/partitions.md",
295296
"Combinatorics/EnumerativeCombinatorics/multipartitions.md",
296297
"Combinatorics/EnumerativeCombinatorics/tableaux.md",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
```@meta
2+
CurrentModule = Oscar
3+
CollapsedDocStrings = true
4+
DocTestSetup = Oscar.doctestsetup()
5+
```
6+
7+
# Combinations
8+
9+
A **combination** from a set $S$ is a selection $\lambda_1, \lambda_2 \dots \lambda_r$ of elements of $S$ taken without repetition; the order of the elements is considered not to matter. A $k$-combination is a combination consisting of $k$ elements.
10+
A general reference on combinations is [Knu11](@cite), Section 7.2.1.3.
11+
12+
A combination can be encoded as an array with elements $\lambda_i$.
13+
In OSCAR, the parametric type `Combination{T}` is provided which is a subtype of `AbstractVector{T}`.
14+
Here, `T` can be any subtype of `IntegerUnion`.
15+
The parametric type allows one to increase performance by using smaller integer types.
16+
```julia
17+
julia> @time collect(combinations(20,10));
18+
0.010399 seconds (184.76 k allocations: 26.782 MiB)
19+
20+
julia> @time collect(combinations(Int8(20),10));
21+
0.008780 seconds (184.76 k allocations: 12.686 MiB)
22+
```
23+
24+
25+
## Generating
26+
27+
```@docs
28+
combinations(n::IntegerUnion, k::IntegerUnion)
29+
combinations(v::AbstractVector, k::IntegerUnion)
30+
```
31+
32+
Because `Combination` is a subtype of `AbstractVector`, many functions that can be used for vectors (1-dimensional arrays) can be used for combinations as well.
33+
For example:
34+
```jldoctest
35+
julia> C = Combination([6, 4, 4, 2])
36+
[6, 4, 4, 2]
37+
38+
julia> length(C)
39+
4
40+
41+
julia> C[1]
42+
6
43+
```

docs/src/Combinatorics/EnumerativeCombinatorics/partitions.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ General references on partitions are [Ful97](@cite) and [Knu11](@cite), Section
1313
A partition can be encoded as an array with elements $\lambda_i$.
1414
In OSCAR, the parametric type `Partition{T}` is provided which is a subtype of `AbstractVector{T}`.
1515
Here, `T` can be any subtype of `IntegerUnion`.
16-
There is no performance impact by using an own type for partitions rather than simply using arrays.
1716
The parametric type allows to increase performance by using smaller integer types.
1817

1918
```@docs

src/Combinatorics/EnumerativeCombinatorics/combinations.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@doc raw"""
2-
combinations(n::Int, k::Int)
2+
combinations(n::IntegerUnion, k::IntegerUnion)
33
44
Return an iterator over all $k$-combinations of ${1,...,n}$, produced in
55
lexicographically ascending order.
@@ -18,10 +18,10 @@ julia> collect(C)
1818
[2, 3, 4]
1919
```
2020
"""
21-
combinations(n::Int, k::Int) = Combinations(Base.OneTo(n), k)
21+
combinations(n::IntegerUnion, k::IntegerUnion) = Combinations(Base.OneTo(n), k)
2222

2323
@doc raw"""
24-
combinations(v::AbstractVector, k::Int)
24+
combinations(v::AbstractVector, k::IntegerUnion)
2525
2626
Return an iterator over all `k`-combinations of a given vector `v` produced in
2727
lexicographically ascending order of the indices.
@@ -40,11 +40,11 @@ julia> collect(C)
4040
['b', 'c', 'd']
4141
```
4242
"""
43-
function combinations(v::AbstractVector{T}, k::Int) where T
43+
function combinations(v::AbstractVector{T}, k::IntegerUnion) where T
4444
return Combinations(v, k)
4545
end
4646

47-
Combinations(v::AbstractArray{T}, k::Int) where T = Combinations(v, length(v), k)
47+
Combinations(v::AbstractArray{T}, k::IntegerUnion) where T = Combinations(v, length(v), Int(k))
4848

4949
@inline function Base.iterate(C::Combinations, state = [min(C.k - 1, i) for i in 1:C.k])
5050
if C.k == 0 # special case to generate 1 result for k = 0

0 commit comments

Comments
 (0)