Skip to content

Commit

Permalink
Merge pull request #1228 from JuliaReach/mforets/1226
Browse files Browse the repository at this point in the history
#1226 - Convert cartesian product of zonotopes to a new zonotope
  • Loading branch information
schillic authored Mar 12, 2019
2 parents 1225724 + 89d9d1a commit 4173669
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/lib/conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ convert(::Type{VPolytope}, ::HPolytope)
convert(::Type{Zonotope}, ::AbstractHyperrectangle)
convert(::Type{IntervalArithmetic.IntervalBox}, ::AbstractHyperrectangle)
convert(::Type{Hyperrectangle}, ::IntervalArithmetic.IntervalBox)
convert(::Type{Zonotope}, ::CartesianProduct{N, Zonotope{N}, Zonotope{N}}) where {N<:Real}
```
1 change: 1 addition & 0 deletions src/compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export ×
@inline _At_mul_B(A, B) = At_mul_B(A, B)
@inline _At_ldiv_B(A, B) = At_ldiv_B(A, B)
expmat = expm
blockdiag = Base.SparseArrays.blkdiag
else
using SparseArrays
using Random
Expand Down
30 changes: 30 additions & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,36 @@ function convert(::Type{Hyperrectangle},
return Hyperrectangle(low=l, high=h)
end

"""
convert(::Type{Zonotope}, cp::CartesianProduct{N, Zonotope{N}, Zonotope{N}}) where {N<:Real}
Converts the cartesian product of two zonotopes to a new zonotope.
### Input
- `Zonotope` -- type used for dispatch
- `S` -- cartesian product of two zonotopes
### Output
A zonotope.
### Algorithm
The cartesian product is obtained by:
- Concatenating the centers of each input zonotope.
- Arranging the generators in block-diagional fashion, and filled with zeros
in the off-diagonal; for this reason, the generator matrix of the returned
zonotope is built as a sparse matrix.
"""
function convert(::Type{Zonotope}, cp::CartesianProduct{N, Zonotope{N}, Zonotope{N}}) where {N<:Real}
Z1, Z2 = cp.X, cp.Y
c = vcat(Z1.center, Z2.center)
G = blockdiag(sparse(Z1.generators), sparse(Z2.generators))
return Zonotope(c, G)
end

"""
convert(::Type{HPolytope}, H::AbstractHyperrectangle)
Expand Down
6 changes: 6 additions & 0 deletions test/unit_Zonotope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ for N in [Float64, Rational{Int}, Float32]
Z1, Z2 = split(Z, 1) # in this case the splitting is exact
@test Z1 Z && Z2 Z

# converts the cartesian product of two zonotopes to a new zonotope
Z1 = Zonotope(N[0], hcat(N[1]))
Z2 = Zonotope(N[1/2], hcat(N[1/2]))
Z = convert(Zonotope, Z1×Z2)
@test Z isa Zonotope && Z.center == N[0, 1/2] && Matrix(Z.generators) == N[1 0; 0 1/2]

# list of constraints
Z = Zonotope(zeros(N, 3), Matrix(N(1)*I, 3, 3))
B = BallInf(zeros(N, 3), N(1)) # equivalent to Z
Expand Down

0 comments on commit 4173669

Please sign in to comment.