Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #10 #11

Merged
merged 2 commits into from
Jun 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
julia 0.5
MultivariatePolynomials 0.0.1
JuMP 0.17
JuMP 0.17.1
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