Skip to content

Commit 6aaa693

Browse files
authored
Merge pull request #4010 from JuliaReach/schillic/convert_sspz
Move `convert` method to `SimpleSparsePolynomialZonotopeModule`
2 parents 91ea029 + cb7f0d5 commit 6aaa693

File tree

6 files changed

+41
-39
lines changed

6 files changed

+41
-39
lines changed

docs/src/lib/conversion.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ 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{SimpleSparsePolynomialZonotope}, ::AbstractSparsePolynomialZonotope)
4342
convert(::Type{SparsePolynomialZonotope}, ::AbstractZonotope{N}) where {N}
4443
convert(::Type{SparsePolynomialZonotope}, ::SimpleSparsePolynomialZonotope{N}) where {N}
4544
```

docs/src/lib/sets/SimpleSparsePolynomialZonotope.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ CurrentModule = LazySets.SimpleSparsePolynomialZonotopeModule
88
SimpleSparsePolynomialZonotope
99
```
1010

11-
```@meta
12-
CurrentModule = LazySets
11+
## Conversion
12+
13+
```julia
14+
convert(::Type{SimpleSparsePolynomialZonotope}, ::AbstractSparsePolynomialZonotope)
1315
```
1416

1517
## Operations

src/Convert/SimpleSparsePolynomialZonotope.jl

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,10 @@ A simple sparse polynomial zonotope.
1616
1717
This method implements [KochdumperA21; Proposition 3](@citet).
1818
"""
19-
function Base.convert(::Type{SimpleSparsePolynomialZonotope}, Z::AbstractZonotope)
19+
function convert(::Type{SimpleSparsePolynomialZonotope}, Z::AbstractZonotope)
2020
c = center(Z)
2121
G = genmat(Z)
2222
n = ngens(Z)
2323
E = Matrix(1 * I, n, n)
2424
return SimpleSparsePolynomialZonotope(c, G, E)
2525
end
26-
27-
"""
28-
convert(::Type{SimpleSparsePolynomialZonotope}, SPZ::AbstractSparsePolynomialZonotope)
29-
30-
Convert a sparse polynomial zonotope to simple sparse polynomial zonotope.
31-
32-
### Input
33-
34-
- `SimpleSparsePolynomialZonotope` -- target type
35-
- `SPZ` -- sparse polynomial zonotope
36-
37-
### Output
38-
39-
A simple sparse polynomial zonotope.
40-
41-
### Algorithm
42-
43-
The method implements [Kochdumper21a; Proposition 3.1.4](@citet).
44-
"""
45-
function Base.convert(::Type{SimpleSparsePolynomialZonotope}, SPZ::AbstractSparsePolynomialZonotope)
46-
c = center(SPZ)
47-
G = hcat(genmat_dep(SPZ), genmat_indep(SPZ))
48-
n = ngens_indep(SPZ)
49-
E = cat(expmat(SPZ), Matrix(1 * I, n, n); dims=(1, 2))
50-
return SimpleSparsePolynomialZonotope(c, G, E)
51-
end

src/Sets/SimpleSparsePolynomialZonotope/SimpleSparsePolynomialZonotopeModule.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using Reexport
44

55
using ..LazySets: AbstractSparsePolynomialZonotope, ngens_dep, nparams,
66
_remove_redundant_generators_polyzono, @validate
7-
using LinearAlgebra: dot
7+
using LinearAlgebra: dot, I
88
using Random: AbstractRNG, GLOBAL_RNG
99
using ReachabilityBase.Distribution: reseed!
1010
using ReachabilityBase.Comparison: isapproxzero
@@ -15,6 +15,7 @@ using ReachabilityBase.Comparison: isapproxzero
1515
@reexport import ..LazySets: expmat, genmat, genmat_dep, genmat_indep, ngens,
1616
ngens_indep, polynomial_order,
1717
remove_redundant_generators
18+
import Base: convert
1819
@reexport using ..API
1920

2021
export SimpleSparsePolynomialZonotope,
@@ -46,4 +47,6 @@ include("remove_redundant_generators.jl")
4647
include("expmat.jl")
4748
include("quadratic_map.jl")
4849

50+
include("convert.jl")
51+
4952
end # module
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
convert(::Type{SimpleSparsePolynomialZonotope}, SPZ::AbstractSparsePolynomialZonotope)
3+
4+
Convert a sparse polynomial zonotope to simple sparse polynomial zonotope.
5+
6+
### Input
7+
8+
- `SimpleSparsePolynomialZonotope` -- target type
9+
- `SPZ` -- sparse polynomial zonotope
10+
11+
### Output
12+
13+
A simple sparse polynomial zonotope.
14+
15+
### Algorithm
16+
17+
The method implements [Kochdumper21a; Proposition 3.1.4](@citet).
18+
"""
19+
function Base.convert(::Type{SimpleSparsePolynomialZonotope}, SPZ::AbstractSparsePolynomialZonotope)
20+
c = center(SPZ)
21+
G = hcat(genmat_dep(SPZ), genmat_indep(SPZ))
22+
n = ngens_indep(SPZ)
23+
E = cat(expmat(SPZ), Matrix(1 * I, n, n); dims=(1, 2))
24+
return SimpleSparsePolynomialZonotope(c, G, E)
25+
end

test/Sets/SimpleSparsePolynomialZonotope.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ for N in @tN([Float64, Float32, Rational{Int}])
149149
P2 = copy(P)
150150
translate!(P2, N[1, 2])
151151
@test P2 == TPZ
152+
153+
# convert
154+
SPZ = SparsePolynomialZonotope(N[4, 4], N[2 1 2; 0 2 2], hcat(N[1; 0]), [1 0 3; 0 1 1])
155+
SSPZ = convert(SimpleSparsePolynomialZonotope, SPZ)
156+
@test center(SSPZ) == center(SPZ)
157+
@test genmat(SSPZ) == hcat(SPZ.G, SPZ.GI)
158+
@test expmat(SSPZ) == [1 0 3 0; 0 1 1 0; 0 0 0 1]
152159
end
153160

154161
for N in @tN([Float64, Float32])
@@ -162,11 +169,3 @@ for Z in [rand(Zonotope), rand(Hyperrectangle)]
162169
@test genmat(ZS) == genmat(Z)
163170
@test expmat(ZS) == I
164171
end
165-
166-
let
167-
SPZ = SparsePolynomialZonotope([4.0, 4], [2.0 1 2; 0 2 2], hcat([1.0; 0]), [1 0 3; 0 1 1])
168-
SSPZ = convert(SimpleSparsePolynomialZonotope, SPZ)
169-
@test center(SSPZ) == center(SPZ)
170-
@test genmat(SSPZ) == hcat(SPZ.G, SPZ.GI)
171-
@test expmat(SSPZ) == [1 0 3 0; 0 1 1 0; 0 0 0 1]
172-
end

0 commit comments

Comments
 (0)