Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,12 @@ end
vars = merge(V1, V2...)
Monomial{vars, length(vars)}
end

function MP.monomial_type(vars::Tuple{V,Vararg{Variable}}) where {V<:Variable}
sorted = merge((first(vars),), Base.tail(vars)...)
return Monomial{sorted, length(sorted)}
end

function MP.monomial_type(::Type{T}) where {T<:Tuple{Variable,Vararg{Variable}}}
return monomial_type(tuple((V() for V in fieldtypes(T))...))
end
17 changes: 17 additions & 0 deletions test/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@
@test typeof(@inferred(promote(x, Monomial{tuple(), 0}()))) == NTuple{2, Monomial{(x,), 1}}
end

@testset "monomial_type for variable tuples" begin
@polyvar x y z

@test @inferred(monomial_type((x,))) == Monomial{(x,), 1}
@test @inferred(monomial_type((x,))) == monomial_type(x)
@test @inferred(monomial_type((x, y))) == Monomial{(x, y), 2}
@test @inferred(monomial_type((x, y))) == typeof(x * y)
@test @inferred(monomial_type((y, x))) == Monomial{(x, y), 2}
@test @inferred(monomial_type((x, y, z))) == Monomial{(x, y, z), 3}
@test @inferred(monomial_type((x, y, z))) == typeof(x * y * z)
@test @inferred(monomial_type((z, x, y))) == Monomial{(x, y, z), 3}

@test @inferred(monomial_type(typeof((x,)))) == Monomial{(x,), 1}
@test @inferred(monomial_type(typeof((x, y)))) == Monomial{(x, y), 2}
@test @inferred(monomial_type(typeof((x, y, z)))) == Monomial{(x, y, z), 3}
end

@testset "conversion" begin
@polyvar x y z

Expand Down
Loading