/
certificate.jl
273 lines (262 loc) · 9.86 KB
/
certificate.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import MultivariatePolynomials as MP
import MultivariateBases as MB
@testset "_merge_sorted" begin
@test SumOfSquares.Certificate._merge_sorted([4, 1], [3, 0]) == [4, 3, 1, 0]
@test SumOfSquares.Certificate._merge_sorted((4, 1), (3, 0)) == (4, 3, 1, 0)
@test SumOfSquares.Certificate._merge_sorted([4, 1], [3, 2]) == [4, 3, 2, 1]
@test SumOfSquares.Certificate._merge_sorted((4, 1), (3, 2)) == (4, 3, 2, 1)
end
@testset "with_variables" begin
@polyvar x y z
p = x + z
v = SumOfSquares.Certificate.with_variables(p, y)
@test v.inner === p
@test MP.variables(v.inner) == [x, z]
@test MP.variables(v) == [x, y, z]
@ncpolyvar a b
q = a * b + b * a
v = SumOfSquares.Certificate.with_variables(q, FullSpace())
@test v.inner === q
@test MP.variables(v.inner) == [a, b, a]
@test MP.variables(v) == [a, b, a]
end
@testset "Monomial selection for certificate" begin
@polyvar x y z
@ncpolyvar a b
@testset "Multipartite error not commutative" for parts in
[([a],), ([a], [b])]
err = ArgumentError(
"Multipartite Newton polytope not supported with noncommutative variables.",
)
@test_throws err SumOfSquares.Certificate.monomials_half_newton_polytope(
[a * b, b^2],
parts,
)
end
@testset "Multipartite error not disjoint: $parts" for parts in [
([x], [x]),
([x], [x, y]),
([x], [y], [x, y]),
]
err = ArgumentError(
"Parts are not disjoint in multipartite Newton polytope estimation: $parts.",
)
@test_throws err SumOfSquares.Certificate.monomials_half_newton_polytope(
[x * y, y^2],
parts,
)
end
@testset "Unipartite" begin
@test SumOfSquares.Certificate.monomials_half_newton_polytope(
[x * y, y^2],
tuple(),
) == [y]
@test isempty(
SumOfSquares.Certificate.monomials_half_newton_polytope(
[x, y],
tuple(),
),
)
@test SumOfSquares.Certificate.monomials_half_newton_polytope(
[x^2, y^2],
tuple(),
) == [x, y]
@test SumOfSquares.Certificate.monomials_half_newton_polytope(
[x^2, y^2],
([x, y],),
) == [x, y]
@test SumOfSquares.Certificate.monomials_half_newton_polytope(
[x^2, y^2],
([y, x],),
) == [x, y]
@test SumOfSquares.Certificate.monomials_half_newton_polytope(
[x^2, x^3 * y^2, x^4 * y^4],
tuple(),
) == [x^2 * y^2, x]
end
@testset "Non-commutative" begin
@test SumOfSquares.Certificate.monomials_half_newton_polytope(
[a^4, a^3 * b, a * b * a^2, a * b * a * b],
tuple(),
) == [a^2, a * b]
@test SumOfSquares.Certificate.monomials_half_newton_polytope(
[
a^2,
a^10 * b^20 * a^11,
a^11 * b^20 * a^10,
a^10 * b^20 * a^20 * b^20 * a^10,
],
tuple(),
) == [a^10 * b^20 * a^10, a]
end
@testset "Multipartite" begin
# In the part [y, z], the degree is between 0 and 2
X = [x^4, x^2 * y^2, x^2 * z^2, x^2 * y * z, y * z]
@test SumOfSquares.Certificate.monomials_half_newton_polytope(
X,
tuple(),
apply_post_filter = false,
) == [x^2, x * y, x * z, y * z, x, y, z]
function full_test(X, Y, part1, part2)
@test SumOfSquares.Certificate.monomials_half_newton_polytope(
X,
(part1,),
apply_post_filter = false,
) == Y
@test SumOfSquares.Certificate.monomials_half_newton_polytope(
X,
(part2,),
apply_post_filter = false,
) == Y
a = SumOfSquares.Certificate.monomials_half_newton_polytope(
X,
(part2,),
apply_post_filter = false,
)
@test SumOfSquares.Certificate.monomials_half_newton_polytope(
X,
(part1, part2),
apply_post_filter = false,
) == Y
@test SumOfSquares.Certificate.monomials_half_newton_polytope(
X,
(part2, part1),
apply_post_filter = false,
) == Y
end
full_test(X, monovec([x^2, x * y, x * z, x, y, z]), [x], [y, z])
full_test(X, monovec([x^2, x * y, x * z, y * z, x, z]), [y], [x, z])
full_test(X, monovec([x^2, x * y, x * z, y * z, x, y]), [z], [x, y])
# FIXME: With recursive merging, it should give [x^2, x*y, x*z, x]
@test SumOfSquares.Certificate.monomials_half_newton_polytope(
[x^4, x^2 * y^2, x^2 * z^2, x^2 * y * z, y * z],
([x], [y], [z]),
apply_post_filter = false,
) == [x^2, x * y, x * z, y * z, x, y, z]
end
end
@testset "Random SOS should be SOS" begin
@polyvar x y
x = [1, x, y, x^2, y^2, x * y]
@test_throws ArgumentError randsos(x, monotype = :Unknown)
for i in 1:10
for monotype in [:Classic, :Gram]
p = randsos(x, monotype = monotype)
@test p isa GramMatrix
@test isposdef(Matrix(p.Q))
end
end
end
function _certificate_api(certificate::Certificate.AbstractCertificate)
@test Certificate.cone(certificate) isa SumOfSquares.SOSLikeCone
@test SumOfSquares.matrix_cone_type(typeof(certificate)) <:
MOI.AbstractVectorSet
end
function _basis_check_each(basis::MB.AbstractPolynomialBasis, basis_type)
@test basis isa basis_type
if basis isa MB.AbstractMonomialBasis
# This fails if `basis` is `Vector{Monomial{true}}` instead of `MonomialVector{true}`
# for DynamicPolynomials. This is important as
# `polynomial(::AbstractMatrix, ::MonomialVector, ::Type)` is implemented but
# `polynomial(Q::AbstractMatrix, X::AbstractVector, ::Type)` falls back to
# `dot(X, Q * X)` which does not work (`promote_operation` calls `zero(eltype(X))`
# which gives `Polynomial{true, Int}` which then tries to multiply a
# `ScalarAffineFunction{Float64}` with an `Int`).
monos = basis.monomials
@test typeof(monos) == typeof(monovec(monos))
@test issorted(monos, rev = true)
end
end
function _basis_check(basis, basis_type)
@test basis isa MB.AbstractPolynomialBasis ||
basis isa Vector{<:MB.AbstractPolynomialBasis}
if basis isa Vector
# FIXME `basis_type` is `Vector{MB.MonomialBasis}` instead of `Vector{MB.MonomialBasis{...}}`
# Once this is fixed, we should check
# @test basis isa basis_type
for b in basis
_basis_check_each(b, eltype(basis_type))
end
else
_basis_check_each(basis, basis_type)
end
end
function certificate_api(certificate::Certificate.AbstractIdealCertificate)
_certificate_api(certificate)
@polyvar x
poly = x + 1
domain = @set x == 1
@test Certificate.reduced_polynomial(certificate, poly, domain) isa
MP.AbstractPolynomial
_basis_check(
Certificate.gram_basis(certificate, poly),
Certificate.gram_basis_type(typeof(certificate)),
)
zbasis = Certificate.zero_basis(certificate)
@test zbasis <: MB.AbstractPolynomialBasis
@test zbasis == Certificate.zero_basis_type(typeof(certificate))
end
function certificate_api(certificate::Certificate.AbstractPreorderCertificate)
_certificate_api(certificate)
@polyvar x
poly = x + 1
domain = @set x >= 1
processed = Certificate.preprocessed_domain(certificate, domain, poly)
for idx in Certificate.preorder_indices(certificate, processed)
_basis_check(
Certificate.multiplier_basis(certificate, idx, processed),
Certificate.multiplier_basis_type(typeof(certificate)),
)
@test Certificate.generator(certificate, idx, processed) isa
MP.AbstractPolynomial
end
icert = Certificate.ideal_certificate(certificate)
@test icert isa Certificate.AbstractIdealCertificate
@test typeof(icert) == Certificate.ideal_certificate(typeof(certificate))
end
@testset "API" begin
@polyvar x
cone = SumOfSquares.SOSCone()
BT = MB.MonomialBasis
maxdegree = 2
function _test(certificate::Certificate.AbstractIdealCertificate)
certificate_api(certificate)
preorder = Certificate.Putinar(certificate, cone, BT, maxdegree)
certificate_api(preorder)
sparsities = Sparsity.Pattern[Sparsity.Variable()]
if certificate isa Certificate.MaxDegree
push!(sparsities, Sparsity.Monomial(ChordalCompletion(), 1))
end
@testset "$(typeof(sparsity))" for sparsity in sparsities
certificate_api(Certificate.Sparsity.Preorder(sparsity, preorder))
end
end
basis = BT(monovec([x^2, x]))
@testset "$(typeof(certificate))" for certificate in [
Certificate.MaxDegree(cone, BT, maxdegree),
Certificate.FixedBasis(cone, basis),
Certificate.Newton(cone, BT, tuple()),
]
_test(certificate)
_test(Certificate.Remainder(certificate))
if certificate isa Certificate.MaxDegree
_test(Certificate.Sparsity.Ideal(Sparsity.Variable(), certificate))
end
@testset "$(typeof(sparsity))" for sparsity in [
SignSymmetry(),
Sparsity.Monomial(ChordalCompletion(), 1),
]
_test(Certificate.Sparsity.Ideal(sparsity, certificate))
_test(
Certificate.Sparsity.Ideal(
sparsity,
Certificate.Remainder(certificate),
),
)
end
end
end
include("ceg_test.jl")
include("csp_test.jl")
include("sparsity.jl")
include("symmetry.jl")