Skip to content

Commit

Permalink
Update to MultivariatePolynomials v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Aug 21, 2017
1 parent 56bfd2b commit 674fe7f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 92 deletions.
3 changes: 2 additions & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
julia 0.6
MultivariatePolynomials 0.0.1
MultivariatePolynomials 0.1.0-
SemialgebraicSets
JuMP 0.17.1
1 change: 1 addition & 0 deletions src/PolyJuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __precompile__()
module PolyJuMP

using MultivariatePolynomials
using SemialgebraicSets

This comment has been minimized.

Copy link
@joehuchette

joehuchette Aug 25, 2017

Contributor

Can you tag a version of SemialgebraicSets.jl?

This comment has been minimized.

Copy link
@blegat

blegat Aug 25, 2017

Author Member

Yes, I will tag a version as soon as we tag MultivariatePolynomials v0.1.0 (which should be today or not too far from today)

This comment has been minimized.

Copy link
@joehuchette

joehuchette Aug 25, 2017

Contributor

Cool, just wanted to note that it doesn't work if you don't have this installed, no rush

using JuMP
import JuMP: getdual, addconstraint
export getslack, setpolymodule!
Expand Down
4 changes: 2 additions & 2 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ macro polyconstraint(m, x, args...)
@assert length(arg.args) == 2
hasdomain && polyconstraint_error(m, x, args, "Multiple domain keyword arguments")
hasdomain = true
appendconstraints!(domains, domaineqs, domainineqs, arg.args[2], msg -> polyconstraint_error(m, x, args, msg))
SemialgebraicSets.appendconstraints!(domains, domaineqs, domainineqs, arg.args[2], msg -> polyconstraint_error(m, x, args, msg))
else
polyconstraint_error(m, x, args, "Unrecognized keyword argument $(string(arg))")
end
Expand All @@ -168,7 +168,7 @@ macro polyconstraint(m, x, args...)
polyconstraint_error(m, x, args, "Invalid sense $sense")
end
newaff, parsecode = JuMP.parseExprToplevel(lhs, :q)
domainaffs, domaincode = builddomain(domains, domaineqs, domainineqs)
domainaffs, domaincode = SemialgebraicSets.builddomain(domains, domaineqs, domainineqs)
code = quote
q = zero(AffExpr)
$parsecode
Expand Down
83 changes: 7 additions & 76 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ using Base.Meta

export Poly, @set

function getvalue{C}(p::Polynomial{C, JuMP.Variable})
Polynomial(map(getvalue, p.a), p.x)
function getvalue(t::AbstractTerm{JuMP.Variable})
getvalue(coefficient(t)) * monomial(t)
end
function getvalue{C}(p::MatPolynomial{C, JuMP.Variable})
MatPolynomial(map(getvalue, p.Q), p.x)
function getvalue(p::AbstractPolynomialLike{JuMP.Variable})
polynomial(getvalue.(terms(p)), MultivariatePolynomials.SortedUniqState())
end

abstract type AbstractPoly end
Expand Down Expand Up @@ -57,83 +57,14 @@ function JuMP.constructvariable!(m::Model, p::AbstractPoly, _error::Function, lo
getpolymodule(m).createpoly(m, p, category == :Default ? :Cont : category)
end

function JuMP.constructconstraint!(p::Polynomial, sense::Symbol)
function JuMP.constructconstraint!(p::AbstractPolynomialLike, sense::Symbol)
PolyConstraint(sense == :(<=) ? -p : p, sense == :(==) ? ZeroPoly() : NonNegPoly())
end

function JuMP.constructconstraint!{PolyT<:MultivariatePolynomials.PolyType}(p::Union{PolyT, AbstractMatrix{PolyT}}, s)
function JuMP.constructconstraint!{PolyT<:AbstractPolynomialLike}(p::Union{PolyT, AbstractMatrix{PolyT}}, s)
PolyConstraint(p, s)
end
# there is already a method for AbstractMatrix in PSDCone in JuMP so we need a more specific here to avoid ambiguity
function JuMP.constructconstraint!{PolyT<:MultivariatePolynomials.PolyType}(p::AbstractMatrix{PolyT}, s::PSDCone)
function JuMP.constructconstraint!{PolyT<:AbstractPolynomialLike}(p::AbstractMatrix{PolyT}, s::PSDCone)
PolyConstraint(p, s)
end

function appendconstraints!(domains, domaineqs, domainineqs, expr, _error)
if isexpr(expr, :call)
try
sense, vectorized = JuMP._canonicalize_sense(expr.args[1])
@assert !vectorized
if sense == :(>=)
push!(domainineqs, :($(expr.args[2]) - $(expr.args[3])))
elseif sense == :(<=)
push!(domainineqs, :($(expr.args[3]) - $(expr.args[2])))
elseif sense == :(==)
push!(domaineqs, :($(expr.args[2]) - $(expr.args[3])))
else
polyconstraint_error("Unrecognized sense $(string(sense)) in domain specification")
end
catch
push!(domains, esc(expr))
end
elseif isexpr(expr, :&&)
map(t -> appendconstraints!(domains, domaineqs, domainineqs, t, _error), expr.args)
else
push!(domains, esc(expr))
end
nothing
end

function builddomain(domains, domaineqs, domainineqs)
domainaffs = gensym()
code = :( $domainaffs = isempty($domainineqs) ? (isempty($domaineqs) ? FullSpace() : AlgebraicSet()) : BasicSemialgebraicSet() )
for dom in domaineqs
affname = gensym()
newaffdomain, parsecodedomain = JuMP.parseExprToplevel(dom, affname)
code = quote
$code
$affname = zero(Polynomial{true, Int})
$parsecodedomain
addequality!($domainaffs, $newaffdomain)
end
end
for dom in domainineqs
affname = gensym()
newaffdomain, parsecodedomain = JuMP.parseExprToplevel(dom, affname)
code = quote
$code
$affname = zero(Polynomial{true, Int})
$parsecodedomain
addinequality!($domainaffs, $newaffdomain)
end
end
for dom in domains
code = quote
$code
$domainaffs = $domainaffs $dom
end
end
domainaffs, code
end

macro set(expr)
domains = []
domaineqs = []
domainineqs = []
appendconstraints!(domains, domaineqs, domainineqs, expr, msg -> error("In @set($expr: ", msg))
domainvar, domaincode = builddomain(domains, domaineqs, domainineqs)
quote
$domaincode
$domainvar
end
end
18 changes: 10 additions & 8 deletions test/constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ _iszero(m, p::AbstractArray) = all(q -> _iszero(m, q), p)
@variable m β
@polyvar x y
p = α * x*y + β * x^2
q = MatPolynomial([α β; β α], [x])
#q = MatPolynomial([α β; β α], [x])
q = α*x^2 + β*x*y + α*y^2
@test macroexpand(:(@constraint(m, p))).head == :error
@test macroexpand(:(@constraint(m, begin p >= 0 end))).head == :error
@test macroexpand(:(@constraint(m, +(p, p, p)))).head == :error
Expand All @@ -37,12 +38,12 @@ _iszero(m, p::AbstractArray) = all(q -> _iszero(m, q), p)
@test isa(c.domain, FullSpace)
else
@test isa(c.domain, AlgebraicSet)
@test c.domain.p == eqs
@test equalities(c.domain) == eqs
end
else
@test isa(c.domain, BasicSemialgebraicSet)
@test c.domain.p == ineqs
@test c.domain.V.p == eqs
@test inequalities(c.domain) == ineqs
@test equalities(c.domain) == eqs
end
end

Expand All @@ -62,7 +63,8 @@ end
@variable m β
@polyvar x y
p = α * x*y + β * x^2
q = MatPolynomial([α β; β α], [x])
#q = MatPolynomial([α β; β α], [x])
q = α*x^2 + β*x*y + α*y^2
@test macroexpand(:(@polyconstraint(m, p))).head == :error
@test macroexpand(:(@polyconstraint(m, begin p >= 0 end))).head == :error
@test macroexpand(:(@polyconstraint(m, +(p, p, p)))).head == :error
Expand All @@ -83,12 +85,12 @@ end
@test isa(c.domain, FullSpace)
else
@test isa(c.domain, AlgebraicSet)
@test c.domain.p == eqs
@test equalities(c.domain) == eqs
end
else
@test isa(c.domain, BasicSemialgebraicSet)
@test c.domain.p == ineqs
@test c.domain.V.p == eqs
@test inequalities(c.domain) == ineqs
@test equalities(c.domain) == eqs
end
end

Expand Down
5 changes: 3 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using MultivariatePolynomials
using DynamicPolynomials
using JuMP
using PolyJuMP
using Base.Test

using SemialgebraicSets

module TestPolyModule
using MultivariatePolynomials
using JuMP
using PolyJuMP
struct TestPoly{P}
Expand Down
3 changes: 0 additions & 3 deletions test/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,9 @@ end
@variable m β
@polyvar x y
p = α * x*y + β * x^2
q = MatPolynomial([α β; β α], [x, y])
JuMP.fix(α, 2)
JuMP.fix(β, 3)
@test getvalue(p) == 2x*y + 3x^2
# Explicit polynomial conversion is needed only if MultivariatePolynomials < v0.0.2
@test Polynomial(getvalue(q)) == 2x^2 + 2y^2 + 6x*y
end

@testset "@polyvariable macro" begin
Expand Down

0 comments on commit 674fe7f

Please sign in to comment.