Skip to content

Commit

Permalink
Update given refactoring of MultivariateBases (#127)
Browse files Browse the repository at this point in the history
* Changes needed for SumOfSquares

* Fix printing

* Fix

* Fix format

* Fixes

* fix format

* fix

* fix
  • Loading branch information
blegat authored Jun 18, 2024
1 parent 4adb153 commit 25246dc
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
run: |
using Pkg
Pkg.add([
PackageSpec(name="StarAlgebras", rev="mk/non_monomial_basis"),
PackageSpec(name="StarAlgebras", rev="main"),
PackageSpec(name="MultivariateBases", rev="master"),
PackageSpec(name="MultivariateMoments", rev="master"),
])
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ StarAlgebras = "0c0c59c1-dc5f-42e9-9a8b-b5dc384a6cd1"

[compat]
DataStructures = "0.18"
DynamicPolynomials = "0.5"
DynamicPolynomials = "0.5,0.6"
IntervalArithmetic = "0.20, 0.21, 0.22" # Needs v0.20 for Julia v1.6 support
JuMP = "1"
MathOptInterface = "1"
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/Constraint/zero_polynomial_in_algebraic_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function MOI.Bridges.Constraint.bridge_constraint(
MB.SubBasis{MB.Monomial,MT,MVT},
},
) where {T,F,Z,DT,MT,MVT}
p = MP.polynomial(MOI.Utilities.scalarize(f), s.basis)
p = MP.polynomial(MB.algebra_element(MOI.Utilities.scalarize(f), s.basis))
# As `*(::MOI.ScalarAffineFunction{T}, ::S)` is only defined if `S == T`, we
# need to call `similar`. This is critical since `T` is
# `Float64` when used with JuMP and the coefficient type is often `Int` with
Expand Down
15 changes: 10 additions & 5 deletions src/constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ struct ZeroPoly <: PolynomialSet end
struct NonNegPoly <: PolynomialSet end
struct PosDefPolyMatrix <: PolynomialSet end

function JuMP.function_string(::MIME"text/plain", p::MP.AbstractPolynomialLike)
function JuMP.function_string(
::MIME"text/plain",
p::Union{SA.AlgebraElement,MP.AbstractPolynomialLike},
)
return sprint(show, MIME"text/plain"(), p)
end
function JuMP.function_string(::MIME"text/latex", p::MP.AbstractPolynomialLike)
# `show` prints `$$` around what `_show` prints.
return sprint(MP._show, MIME"text/latex"(), p)
function JuMP.function_string(
mime::MIME"text/latex",
p::Union{SA.AlgebraElement,MP.AbstractPolynomialLike},
)
return SA.trim_LaTeX(mime, sprint(show, MIME"text/latex"(), p))
end

### Shapes for polynomial/moments primal-dual pair ###
Expand All @@ -28,7 +33,7 @@ struct PolynomialShape{B<:SA.ExplicitBasis} <: JuMP.AbstractShape
basis::B
end
function JuMP.reshape_vector(x::Vector, shape::PolynomialShape)
return MP.polynomial(x, shape.basis)
return MB.algebra_element(x, shape.basis)
end
struct MomentsShape{B<:SA.ExplicitBasis} <: JuMP.AbstractShape
basis::B
Expand Down
1 change: 1 addition & 0 deletions test/Tests/utilities.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import StarAlgebras as SA
using Test, JuMP

function _model(optimizer::MOI.AbstractOptimizer)
Expand Down
4 changes: 2 additions & 2 deletions test/Tests/zero_polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function _zero_polynomial_test(
@test value(β) 1.0 atol = atol rtol = rtol
@test value(γ) 0.0 atol = atol rtol = rtol
@test value(UpperBoundRef(β)) 1.0 atol = atol rtol = rtol
@test value(cref) isa MP.AbstractPolynomial{Float64}
@test value(cref) 0.0 * x * y atol = atol rtol = rtol
@test value(cref) isa SA.AlgebraElement
@test MP.polynomial(value(cref)) 0.0 * x * y atol = atol rtol = rtol
@test value(cγ) 0.0 * x * y atol = atol rtol = rtol

@test dual_status(model) == MOI.FEASIBLE_POINT
Expand Down
28 changes: 17 additions & 11 deletions test/constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module TestConstraint

using Test

import StarAlgebras as SA
using MultivariatePolynomials
const MP = MultivariatePolynomials
import MultivariateBases as MB
Expand All @@ -13,7 +14,10 @@ using PolyJuMP
include("utilities.jl")
include("testpolymodule.jl")

_isequal(p, q) = all(JuMP.isequal_canonical.(coefficients(p), coefficients(q)))
_coeffs(p::MP.AbstractPolynomialLike) = coefficients(p)
_coeffs(a::SA.AlgebraElement) = SA.coeffs(a, MB.explicit_basis(a))

_isequal(p, q) = all(JuMP.isequal_canonical.(_coeffs(p), _coeffs(q)))
function _isequal(x::AbstractArray, y::AbstractArray)
return size(x) == size(y) && all(_isequal.(x, y))
end
Expand All @@ -25,16 +29,18 @@ _con_constant(a) = a
# `MOI.Utilities.Model` canonicalizes the constraints so we need to
# canonicalize them as well for the printing tests.
function _canon(model, p::MP.AbstractPolynomialLike)
return MP.polynomial(
map(MP.terms(p)) do t
coef = _con_constant(MP.coefficient(t))
moi = JuMP.moi_function(coef)
jump = JuMP.jump_function(model, MOI.Utilities.canonical(moi))
return MP.term(jump, MP.monomial(t))
end,
return MB.algebra_element(
MP.polynomial(
map(MP.terms(p)) do t
coef = _con_constant(MP.coefficient(t))
moi = JuMP.moi_function(coef)
jump = JuMP.jump_function(model, MOI.Utilities.canonical(moi))
return MP.term(jump, MP.monomial(t))
end,
),
)
end
_canon(model, p::Matrix) = _canon.(model, p)
_canon(model, p::Matrix) = MP.polynomial.(_canon.(model, p))

function _test_constraint(
m,
Expand Down Expand Up @@ -150,9 +156,9 @@ function test_printing(var)
in_sym = Sys.iswindows() ? "in" : ""
eqref = @constraint(m, p == q)
@test sprint(show, MIME"text/plain"(), eqref) ==
"(-α)y² + (α - β)xy + (-α + β)x² $in_sym PolyJuMP.ZeroPoly()"
"(-α)·y² + (α - β)·xy + (-α + β)·$in_sym PolyJuMP.ZeroPoly()"
@test sprint(show, MIME"text/latex"(), eqref) ==
"\$\$ (-α)y^{2} + (α - β)xy + (-α + β)x^{2} \\in PolyJuMP.ZeroPoly() \$\$"
"\$\$ (-α) \\cdot y^{2} + (α - β) \\cdot xy + (-α + β) \\cdot x^{2} \\in PolyJuMP.ZeroPoly() \$\$"
sdref = @constraint(m, [p q; q p] in PSDCone())
@test sprint(show, MIME"text/plain"(), sdref) ==
"[(α)xy + (β)x² (α)y² + (β)xy + (α)x²;\n (α)y² + (β)xy + (α)x² (α)xy + (β)x²] $in_sym $DummyPolyModule.DummyPosDefMatrix()"
Expand Down

0 comments on commit 25246dc

Please sign in to comment.