diff --git a/src/types.jl b/src/types.jl index d83f94e..870b3cd 100644 --- a/src/types.jl +++ b/src/types.jl @@ -43,6 +43,10 @@ MP.nvariables(::Type{<:Monomial{V}}) where {V} = length(V) MP.nvariables(m::Monomial) = nvariables(typeof(m)) MP.monomial_type(::Type{V}) where V<:Variable = monomial_type(V()) MP.monomial_type(v::Variable) = Monomial{(v,), 1} +function MP.monomial(vars::Tuple, exps::NTuple{N,Int}) where {N} + @assert length(vars) == length(exps) + return Monomial{vars,N}(exps) +end MP.exponents(m::Monomial) = m.exponents MP.exponent(m::Monomial, i::Integer) = m.exponents[i] diff --git a/test/polynomials.jl b/test/polynomials.jl index 1e5231a..dc52671 100644 --- a/test/polynomials.jl +++ b/test/polynomials.jl @@ -97,6 +97,10 @@ end @test @inferred(Monomial{tuple(), 0}()) == @inferred(x^0) @test @inferred(Monomial{(x, y)}([1, 2])) == Monomial{(x, y)}((1, 2)) + @test @inferred(monomial((x, y), (1, 2))) == Monomial{(x, y)}((1, 2)) + @test_throws AssertionError monomial((x,), (1, 2)) + @test_throws AssertionError monomial((y, x), (1, 2)) + @test_throws AssertionError monomial((x, x), (1, 2)) end @testset "terms" begin