Skip to content

Commit

Permalink
Fix #10
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jun 9, 2017
1 parent 8ffab01 commit 0f22b34
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 12 additions & 7 deletions test/constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 0f22b34

Please sign in to comment.