Skip to content

Commit c92ed05

Browse files
jbshannonJack Shannoninkydragon
authored
Make multiexponents type stable (#136)
* added `Base.eltype()` method for `MultiExponents` * added tests for type-stability * Update test/multinomials.jl * Update test/multinomials.jl --------- Co-authored-by: Jack Shannon <[email protected]> Co-authored-by: Chengyu Han <[email protected]>
1 parent e3278db commit c92ed05

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/multinomials.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function Base.iterate(m::MultiExponents, s = nothing)
2626
end
2727

2828
Base.length(m::MultiExponents) = length(m.c)
29+
Base.eltype(::Type{MultiExponents{T}}) where {T} = Vector{Int}
2930

3031
"""
3132
multiexponents(m, n)
@@ -35,15 +36,16 @@ Returns the exponents in the multinomial expansion (x₁ + x₂ + ... + xₘ)ⁿ
3536
For example, the expansion (x₁ + x₂ + x₃)² = x₁² + x₁x₂ + x₁x₃ + ...
3637
has the exponents:
3738
38-
julia> collect(multiexponents(3, 2))
39-
40-
6-element Array{Any,1}:
41-
[2, 0, 0]
42-
[1, 1, 0]
43-
[1, 0, 1]
44-
[0, 2, 0]
45-
[0, 1, 1]
46-
[0, 0, 2]
39+
```julia-repl
40+
julia> collect(multiexponents(3, 2))
41+
6-element Vector{Vector{Int64}}:
42+
[2, 0, 0]
43+
[1, 1, 0]
44+
[1, 0, 1]
45+
[0, 2, 0]
46+
[0, 1, 1]
47+
[0, 0, 2]
48+
```
4749
"""
4850
function multiexponents(m, n)
4951
# number of stars and bars = m+n-1

test/multinomials.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ using LinearAlgebra
2525
@test [multiexponents(2, 2)...] == [[2, 0], [1, 1], [0, 2]]
2626
@test [multiexponents(3, 3)...] == [[3, 0, 0], [2, 1, 0], [2, 0, 1], [1, 2, 0], [1, 1, 1], [1, 0, 2], [0, 3, 0], [0, 2, 1], [0, 1, 2], [0, 0, 3]]
2727

28+
@test length(multiexponents(1, 1)) == 1
29+
@test length(multiexponents(2, 2)) == 3
30+
31+
# type-stability
32+
@test typeof([multiexponents(1, 1)...]) == Vector{Vector{Int}}
33+
@test eltype(multiexponents(1, 1)) == Vector{Int}
2834
end

0 commit comments

Comments
 (0)