Skip to content

Commit 8b09117

Browse files
authored
Implement monomial constructor (#94)
1 parent 45b4681 commit 8b09117

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/types.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ MP.nvariables(::Type{<:Monomial{V}}) where {V} = length(V)
4343
MP.nvariables(m::Monomial) = nvariables(typeof(m))
4444
MP.monomial_type(::Type{V}) where V<:Variable = monomial_type(V())
4545
MP.monomial_type(v::Variable) = Monomial{(v,), 1}
46+
function MP.monomial(vars::Tuple, exps::NTuple{N,Int}) where {N}
47+
@assert length(vars) == length(exps)
48+
return Monomial{vars,N}(exps)
49+
end
4650

4751
MP.exponents(m::Monomial) = m.exponents
4852
MP.exponent(m::Monomial, i::Integer) = m.exponents[i]

test/polynomials.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ end
9797
@test @inferred(Monomial{tuple(), 0}()) == @inferred(x^0)
9898

9999
@test @inferred(Monomial{(x, y)}([1, 2])) == Monomial{(x, y)}((1, 2))
100+
@test @inferred(monomial((x, y), (1, 2))) == Monomial{(x, y)}((1, 2))
101+
@test_throws AssertionError monomial((x,), (1, 2))
102+
@test_throws AssertionError monomial((y, x), (1, 2))
103+
@test_throws AssertionError monomial((x, x), (1, 2))
100104
end
101105

102106
@testset "terms" begin

0 commit comments

Comments
 (0)