Skip to content

Commit f8bfd2f

Browse files
authored
Define monomial constructor (#174)
* Define monomial constructor * Add tests
1 parent f8fe686 commit f8bfd2f

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DynamicPolynomials"
22
uuid = "7c1d4256-1411-5781-91ec-d7bc3513ac07"
33
repo = "https://github.com/JuliaAlgebra/DynamicPolynomials.jl.git"
4-
version = "0.6.2"
4+
version = "0.6.3"
55

66
[deps]
77
Future = "9fa8497b-333b-5362-9e8d-4d0656e87820"

src/mono.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ function Monomial{V,M}(
3030
return Monomial{V,M}([vars...], z)
3131
end
3232

33+
function MP.monomial(vars::Vector{Variable{V,M}}, z::Vector{Int}) where {V,M}
34+
@assert issorted(vars, rev = true)
35+
return Monomial{V,M}(vars, z)
36+
end
37+
3338
iscomm(::Type{<:Monomial{V}}) where {V} = iscomm(V)
3439
Monomial{V,M}() where {V,M} = Monomial{V,M}(Variable{V,M}[], Int[])
3540
function Monomial(vars::TupOrVec{Variable{V,M}}, z::Vector{Int}) where {V,M}
@@ -79,7 +84,6 @@ MP.exponents(m::Monomial) = m.z
7984
# /!\ vars not copied, do not mess with vars
8085
MP.variables(m::Union{Monomial}) = m.vars
8186

82-
MP.monomial(m::Monomial) = m
8387
# Does m1 divides m2 ?
8488
#function MP.divides(m1::Monomial, m2::Monomial)
8589
# i = j = 1

src/var.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ end
209209

210210
MP.monomial(v::Variable) = Monomial(v)
211211
MP.variables(v::Variable) = [v]
212+
MP.exponents(v::Variable) = [1]
212213
MP.ordering(v::Variable) = MP.ordering(typeof(v))
213214
MP.ordering(::Type{Variable{V,M}}) where {V,M} = M
214215

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ function alloc_test_lt(f, n)
88
@test n >= @allocated f()
99
end
1010

11+
# TODO move to MP
12+
@testset "monomial" begin
13+
@polyvar x
14+
@test monomial(x) == monomial(variables(x), exponents(x))
15+
@polyvar y
16+
@test monomial([x, y], [2, 3]) == x^2 * y^3
17+
@test_throws AssertionError monomial([y, x], [2, 3]) == x^2 * y^3
18+
end
19+
1120
# TODO move to MP
1221
@testset "See https://github.com/jump-dev/SumOfSquares.jl/issues/388" begin
1322
@polyvar x[1:3]

0 commit comments

Comments
 (0)