Skip to content

Commit 25246dc

Browse files
authored
Update given refactoring of MultivariateBases (#127)
* Changes needed for SumOfSquares * Fix printing * Fix * Fix format * Fixes * fix format * fix * fix
1 parent 4adb153 commit 25246dc

File tree

7 files changed

+33
-21
lines changed

7 files changed

+33
-21
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
run: |
4141
using Pkg
4242
Pkg.add([
43-
PackageSpec(name="StarAlgebras", rev="mk/non_monomial_basis"),
43+
PackageSpec(name="StarAlgebras", rev="main"),
4444
PackageSpec(name="MultivariateBases", rev="master"),
4545
PackageSpec(name="MultivariateMoments", rev="master"),
4646
])

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ StarAlgebras = "0c0c59c1-dc5f-42e9-9a8b-b5dc384a6cd1"
1919

2020
[compat]
2121
DataStructures = "0.18"
22-
DynamicPolynomials = "0.5"
22+
DynamicPolynomials = "0.5,0.6"
2323
IntervalArithmetic = "0.20, 0.21, 0.22" # Needs v0.20 for Julia v1.6 support
2424
JuMP = "1"
2525
MathOptInterface = "1"

src/Bridges/Constraint/zero_polynomial_in_algebraic_set.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function MOI.Bridges.Constraint.bridge_constraint(
3030
MB.SubBasis{MB.Monomial,MT,MVT},
3131
},
3232
) where {T,F,Z,DT,MT,MVT}
33-
p = MP.polynomial(MOI.Utilities.scalarize(f), s.basis)
33+
p = MP.polynomial(MB.algebra_element(MOI.Utilities.scalarize(f), s.basis))
3434
# As `*(::MOI.ScalarAffineFunction{T}, ::S)` is only defined if `S == T`, we
3535
# need to call `similar`. This is critical since `T` is
3636
# `Float64` when used with JuMP and the coefficient type is often `Int` with

src/constraint.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ struct ZeroPoly <: PolynomialSet end
1313
struct NonNegPoly <: PolynomialSet end
1414
struct PosDefPolyMatrix <: PolynomialSet end
1515

16-
function JuMP.function_string(::MIME"text/plain", p::MP.AbstractPolynomialLike)
16+
function JuMP.function_string(
17+
::MIME"text/plain",
18+
p::Union{SA.AlgebraElement,MP.AbstractPolynomialLike},
19+
)
1720
return sprint(show, MIME"text/plain"(), p)
1821
end
19-
function JuMP.function_string(::MIME"text/latex", p::MP.AbstractPolynomialLike)
20-
# `show` prints `$$` around what `_show` prints.
21-
return sprint(MP._show, MIME"text/latex"(), p)
22+
function JuMP.function_string(
23+
mime::MIME"text/latex",
24+
p::Union{SA.AlgebraElement,MP.AbstractPolynomialLike},
25+
)
26+
return SA.trim_LaTeX(mime, sprint(show, MIME"text/latex"(), p))
2227
end
2328

2429
### Shapes for polynomial/moments primal-dual pair ###
@@ -28,7 +33,7 @@ struct PolynomialShape{B<:SA.ExplicitBasis} <: JuMP.AbstractShape
2833
basis::B
2934
end
3035
function JuMP.reshape_vector(x::Vector, shape::PolynomialShape)
31-
return MP.polynomial(x, shape.basis)
36+
return MB.algebra_element(x, shape.basis)
3237
end
3338
struct MomentsShape{B<:SA.ExplicitBasis} <: JuMP.AbstractShape
3439
basis::B

test/Tests/utilities.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import StarAlgebras as SA
12
using Test, JuMP
23

34
function _model(optimizer::MOI.AbstractOptimizer)

test/Tests/zero_polynomial.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ function _zero_polynomial_test(
3636
@test value(β) 1.0 atol = atol rtol = rtol
3737
@test value(γ) 0.0 atol = atol rtol = rtol
3838
@test value(UpperBoundRef(β)) 1.0 atol = atol rtol = rtol
39-
@test value(cref) isa MP.AbstractPolynomial{Float64}
40-
@test value(cref) 0.0 * x * y atol = atol rtol = rtol
39+
@test value(cref) isa SA.AlgebraElement
40+
@test MP.polynomial(value(cref)) 0.0 * x * y atol = atol rtol = rtol
4141
@test value(cγ) 0.0 * x * y atol = atol rtol = rtol
4242

4343
@test dual_status(model) == MOI.FEASIBLE_POINT

test/constraint.jl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module TestConstraint
22

33
using Test
44

5+
import StarAlgebras as SA
56
using MultivariatePolynomials
67
const MP = MultivariatePolynomials
78
import MultivariateBases as MB
@@ -13,7 +14,10 @@ using PolyJuMP
1314
include("utilities.jl")
1415
include("testpolymodule.jl")
1516

16-
_isequal(p, q) = all(JuMP.isequal_canonical.(coefficients(p), coefficients(q)))
17+
_coeffs(p::MP.AbstractPolynomialLike) = coefficients(p)
18+
_coeffs(a::SA.AlgebraElement) = SA.coeffs(a, MB.explicit_basis(a))
19+
20+
_isequal(p, q) = all(JuMP.isequal_canonical.(_coeffs(p), _coeffs(q)))
1721
function _isequal(x::AbstractArray, y::AbstractArray)
1822
return size(x) == size(y) && all(_isequal.(x, y))
1923
end
@@ -25,16 +29,18 @@ _con_constant(a) = a
2529
# `MOI.Utilities.Model` canonicalizes the constraints so we need to
2630
# canonicalize them as well for the printing tests.
2731
function _canon(model, p::MP.AbstractPolynomialLike)
28-
return MP.polynomial(
29-
map(MP.terms(p)) do t
30-
coef = _con_constant(MP.coefficient(t))
31-
moi = JuMP.moi_function(coef)
32-
jump = JuMP.jump_function(model, MOI.Utilities.canonical(moi))
33-
return MP.term(jump, MP.monomial(t))
34-
end,
32+
return MB.algebra_element(
33+
MP.polynomial(
34+
map(MP.terms(p)) do t
35+
coef = _con_constant(MP.coefficient(t))
36+
moi = JuMP.moi_function(coef)
37+
jump = JuMP.jump_function(model, MOI.Utilities.canonical(moi))
38+
return MP.term(jump, MP.monomial(t))
39+
end,
40+
),
3541
)
3642
end
37-
_canon(model, p::Matrix) = _canon.(model, p)
43+
_canon(model, p::Matrix) = MP.polynomial.(_canon.(model, p))
3844

3945
function _test_constraint(
4046
m,
@@ -150,9 +156,9 @@ function test_printing(var)
150156
in_sym = Sys.iswindows() ? "in" : ""
151157
eqref = @constraint(m, p == q)
152158
@test sprint(show, MIME"text/plain"(), eqref) ==
153-
"(-α)y² + (α - β)xy + (-α + β)x² $in_sym PolyJuMP.ZeroPoly()"
159+
"(-α)·y² + (α - β)·xy + (-α + β)·$in_sym PolyJuMP.ZeroPoly()"
154160
@test sprint(show, MIME"text/latex"(), eqref) ==
155-
"\$\$ (-α)y^{2} + (α - β)xy + (-α + β)x^{2} \\in PolyJuMP.ZeroPoly() \$\$"
161+
"\$\$ (-α) \\cdot y^{2} + (α - β) \\cdot xy + (-α + β) \\cdot x^{2} \\in PolyJuMP.ZeroPoly() \$\$"
156162
sdref = @constraint(m, [p q; q p] in PSDCone())
157163
@test sprint(show, MIME"text/plain"(), sdref) ==
158164
"[(α)xy + (β)x² (α)y² + (β)xy + (α)x²;\n (α)y² + (β)xy + (α)x² (α)xy + (β)x²] $in_sym $DummyPolyModule.DummyPosDefMatrix()"

0 commit comments

Comments
 (0)