Skip to content

Commit 2848f49

Browse files
committed
change default convert from zonotope to SPZ
1 parent e748f00 commit 2848f49

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

docs/src/lib/conversion.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ convert(::Type{MinkowskiSumArray}, ::MinkowskiSum{N, ST, MinkowskiSumArray{N, ST
3939
convert(::Type{STAR}, ::AbstractPolyhedron{N}) where {N}
4040
convert(::Type{STAR}, ::Star)
4141
convert(::Type{SimpleSparsePolynomialZonotope}, ::AbstractZonotope)
42-
convert(::Type{SparsePolynomialZonotope}, ::AbstractZonotope{N}) where {N}
42+
convert(::Type{SparsePolynomialZonotope}, ::AbstractZonotope)
4343
convert(::Type{SparsePolynomialZonotope}, ::SimpleSparsePolynomialZonotope{N}) where {N}
4444
```
4545

src/Convert/SparsePolynomialZonotope.jl

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
"""
2-
convert(::Type{SparsePolynomialZonotope}, Z::AbstractZonotope{N}) where {N}
2+
convert(::Type{SparsePolynomialZonotope}, Z::AbstractZonotope; [algorithm]="GI")
33
44
Convert a zonotope to sparse polynomial zonotope.
55
66
### Input
77
88
- `SparsePolynomialZonotope` -- target type
99
- `Z` -- zonotopic set
10+
- `algorithm` -- (optional, default: `"GI"`) algorithm
1011
1112
### Output
1213
1314
A sparse polynomial zonotope.
1415
1516
### Algorithm
1617
17-
The method implements [Kochdumper21a; Proposition 3.1.19](@citet).
18+
The `"GI"` method creates a polynomial zonotope with only independent generators.
19+
20+
The `"K21"` method creates a polynomial zonotope with only dependent generators,
21+
implementing [Kochdumper21a; Proposition 3.1.9](@citet).
1822
"""
19-
function convert(::Type{SparsePolynomialZonotope}, Z::AbstractZonotope{N}) where {N}
23+
function convert(::Type{SparsePolynomialZonotope}, Z::AbstractZonotope;
24+
algorithm="GI")
25+
if algorithm == "GI"
26+
return _convert_SPZ_Z_GI(Z)
27+
elseif algorithm == "K21"
28+
return _convert_SPZ_Z_K21(Z)
29+
end
30+
throw(ArgumentError("invalid algorithm $algorithm"))
31+
end
32+
33+
function _convert_SPZ_Z_K21(Z::AbstractZonotope{N}) where {N}
2034
c = center(Z)
2135
G = genmat(Z)
2236
p = ngens(Z)
@@ -25,6 +39,14 @@ function convert(::Type{SparsePolynomialZonotope}, Z::AbstractZonotope{N}) where
2539
return SparsePolynomialZonotope(c, G, GI, E)
2640
end
2741

42+
function _convert_SPZ_Z_GI(Z::AbstractZonotope{N}) where {N}
43+
c = center(Z)
44+
GI = genmat(Z)
45+
E = zeros(Int, 0, 0)
46+
G = zeros(N, length(c), 0)
47+
return SparsePolynomialZonotope(c, G, GI, E)
48+
end
49+
2850
"""
2951
convert(::Type{SparsePolynomialZonotope}, SSPZ::SimpleSparsePolynomialZonotope)
3052

test/ConcreteOperations/minkowski_sum.jl

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,21 @@ for N in @tN([Float64, Float32, Rational{Int}])
2323
H2 = Hyperrectangle(N[5, 6], N[7, 8])
2424
@test minkowski_sum(H1, H2) == Hyperrectangle(N[6, 8], N[10, 12])
2525
SPZ = convert(SparsePolynomialZonotope, H1)
26-
for P in (SPZ, convert(SSPZ, SPZ))
27-
P = minkowski_sum(P, H2)
28-
# equality is not required but approximates the equivalence check
29-
@test P == SparsePolynomialZonotope(N[6, 8], N[3 0; 0 4], N[7 0; 0 8], [1 0; 0 1], 1:2)
30-
P = minkowski_sum(H1, convert(SparsePolynomialZonotope, H2))
31-
@test P == SparsePolynomialZonotope(N[6, 8], N[7 0; 0 8], N[3 0; 0 4], [1 0; 0 1], 1:2)
32-
end
33-
34-
P = SparsePolynomialZonotope(N[1, -1], N[1 1; 0 -1], N[1 0; 0 -1], [2 1; 0 1; 1 0], [3, 4, 5])
35-
Z = Zonotope(N[0, 1], N[2 1; 1 -1])
36-
ms = minkowski_sum(P, Z)
37-
@test indexvector(ms) == indexvector(P)
26+
# SPZ + Z
27+
P = SPZ
28+
P2 = minkowski_sum(P, H2)
29+
@test indexvector(P2) == indexvector(SPZ)
30+
# equality is not required but approximates the equivalence check
31+
@test P2 == SparsePolynomialZonotope(N[6, 8], zeros(N, 2, 0), N[3 0 7 0; 0 4 0 8], zeros(Int, 0, 0), Int[])
32+
P2 = minkowski_sum(H1, convert(SparsePolynomialZonotope, H2))
33+
@test P2 == SparsePolynomialZonotope(N[6, 8], zeros(N, 2, 0), N[7 0 3 0; 0 8 0 4], zeros(Int, 0, 0), Int[])
34+
# SSPZ + Z
35+
P = convert(SSPZ, SPZ)
36+
P2 = minkowski_sum(P, H2)
37+
# equality is not required but approximates the equivalence check
38+
@test P2 == SparsePolynomialZonotope(N[6, 8], N[3 0; 0 4], N[7 0; 0 8], [1 0; 0 1], 1:2)
39+
P2 = minkowski_sum(H1, convert(SparsePolynomialZonotope, H2))
40+
@test P2 == SparsePolynomialZonotope(N[6, 8], zeros(N, 2, 0), N[7 0 3 0; 0 8 0 4], zeros(Int, 0, 0), Int[])
3841
end
3942

4043
for N in @tN([Float64, Float32])

test/Sets/SparsePolynomialZonotope.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,13 @@ for N in @tN([Float64, Float32])
259259
end
260260

261261
for Z in [rand(Zonotope), rand(Hyperrectangle)]
262-
ZS = convert(SparsePolynomialZonotope, Z)
262+
ZS = convert(SparsePolynomialZonotope, Z; algorithm="GI")
263+
@test center(ZS) == center(Z)
264+
@test isempty(genmat_dep(ZS))
265+
@test genmat_indep(ZS) == genmat(Z)
266+
@test isempty(expmat(ZS))
267+
268+
ZS = convert(SparsePolynomialZonotope, Z; algorithm="K21")
263269
@test center(ZS) == center(Z)
264270
@test genmat_dep(ZS) == genmat(Z)
265271
@test isempty(genmat_indep(ZS))

0 commit comments

Comments
 (0)