diff --git a/REQUIRE b/REQUIRE index 414901b..df6247e 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,3 +1,3 @@ julia 0.5 MultivariatePolynomials 0.0.1 -JuMP 0.17 +JuMP 0.17.1 diff --git a/src/macros.jl b/src/macros.jl index 8b92443..e4bf6d8 100644 --- a/src/macros.jl +++ b/src/macros.jl @@ -62,6 +62,10 @@ function JuMP.constructconstraint!(p::Polynomial, sense::Symbol) PolyConstraint(sense == :(<=) ? -p : p, sense != :(==)) end +function JuMP.constructconstraint!{PolyT<:MultivariatePolynomials.PolyType}(p::AbstractMatrix{PolyT}, ::PSDCone) + PolyConstraint(p, true) +end + function appendconstraints!(domains, domaineqs, domainineqs, expr, _error) if isexpr(expr, :call) try diff --git a/test/constraint.jl b/test/constraint.jl index cff1ce2..1fab494 100644 --- a/test/constraint.jl +++ b/test/constraint.jl @@ -14,19 +14,23 @@ #@test macroexpand(:(@constraint(m, p >= 0, domain = (@set x >= -1 && x <= 1, domain = y >= -1 && y <= 1)))).head == :error @test macroexpand(:(@constraint(m, p + 0, domain = (@set x >= -1 && x <= 1)))).head == :error + # TODO Once JuMP drops Julia v0.5, this should be move to JuMP and be renamed Base.iszero + function affexpr_iszero(affexpr) + affexpr.constant == 0 || return false + tmp = JuMP.IndexedVector(Float64, m.numCols) + JuMP.collect_expr!(m, tmp, affexpr) + tmp.nnz == 0 + end + _iszero(p) = all(affexpr_iszero, p.a) + _iszero(p::AbstractArray) = all(_iszero, p) function testcon(m, cref, nonnegative, p, ineqs, eqs) @test isa(cref, ConstraintRef{Model, PolyJuMP.PolyConstraint}) c = PolyJuMP.getpolyconstr(m)[cref.idx] @test c.nonnegative == nonnegative # == between JuMP affine expression is not accurate, e.g. β + α != α + β # == 0 is not defined either - function affexpr_iszero(affexpr) - affexpr.constant == 0 || return false - tmp = JuMP.IndexedVector(Float64, m.numCols) - JuMP.collect_expr!(m, tmp, affexpr) - tmp.nnz == 0 - end - @test all(affexpr_iszero, (c.p - p).a) + # c.p and p can be matrices + @test _iszero(c.p - p) if isempty(ineqs) if isempty(eqs) @test isa(c.domain, FullSpace) @@ -47,6 +51,7 @@ testcon(m, @constraint(m, p <= q), true, q - p, [], []) testcon(m, @constraint(m, p + q >= 0, domain = @set x == y^3), true, p + q, [], [x - y^3]) testcon(m, @constraint(m, p == q, domain = @set x == 1 && f(x, y)), false, p - q, [], [x - 1, x + y - 2]) + testcon(m, @SDconstraint(m, [p q; q 0] ⪰ [0 0; 0 p]), true, [p q; q -p], [], []) end @testset "@polyconstraint macro" begin