Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert LinearMap of CPA to zonotope #1237

Merged
merged 2 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/src/lib/conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,9 @@ convert(::Type{Zonotope}, ::CartesianProduct{N, Zonotope{N}, Zonotope{N}}) where
convert(::Type{Hyperrectangle}, ::CartesianProduct{N, HN1, HN2}) where {N<:Real, HN1<:AbstractHyperrectangle{N}, HN2<:AbstractHyperrectangle{N}}
convert(::Type{Zonotope}, ::CartesianProduct{N, HN1, HN2}) where {N<:Real, HN1<:AbstractHyperrectangle{N}, HN2<:AbstractHyperrectangle{N}}
convert(::Type{Zonotope}, ::CartesianProductArray{N, HN}) where {N<:Real, HN<:AbstractHyperrectangle{N}}
convert(::Type{Zonotope}, ::LinearMap{N, SN, NM, MAT}) where {N, SN<:AbstractHyperrectangle{N}, NM, MAT<:AbstractMatrix{N}}
convert(::Type{Zonotope}, ::LinearMap{N, SN}) where {N, SN<:AbstractHyperrectangle{N}}
convert(::Type{Zonotope}, ::LinearMap{N, CartesianProduct{N, HN1, HN2}}) where {N, HN1<:AbstractHyperrectangle{N}, HN2<:AbstractHyperrectangle{N}}
convert(::Type{Zonotope}, ::LinearMap{N, CartesianProductArray{N, HN}}) where {N, HN<:AbstractHyperrectangle{N}}
```


65 changes: 59 additions & 6 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ end

"""
convert(::Type{Zonotope},
S::LinearMap{N, SN, NM, MAT}
) where {N, SN<:AbstractHyperrectangle{N}, NM, MAT<:AbstractMatrix{N}}
S::LinearMap{N, SN}
) where {N, SN<:AbstractHyperrectangle{N}}

Converts the lazy linear map of a hyperrectangular set to a zonotope.

Expand All @@ -324,11 +324,64 @@ A zonotope.
This method first converts the hyperrectangular set to a zonotope, and then
applies the (concrete) linear map to the zonotope.
"""
function convert(::Type{Zonotope}, S::LinearMap{N, SN}
) where {N, SN<:AbstractHyperrectangle{N}}
return linear_map(S.M, convert(Zonotope, S.X))
end

"""
convert(::Type{Zonotope}, S::LinearMap{N, CartesianProduct{N, HN1, HN2}}
) where {N, HN1<:AbstractHyperrectangle{N},
HN2<:AbstractHyperrectangle{N}}

Converts the lazy linear map of the cartesian product of two hyperrectangular
sets to a zonotope.

### Input

- `Zonotope` -- type, used for dispatch
- `S` -- linear map of the cartesian product of hyperrectangular sets

### Output

A zonotope.

### Algorithm

This method first converts the cartesian product to a zonotope, and then
applies the (concrete) linear map to the zonotope.
"""
function convert(::Type{Zonotope},
S::LinearMap{N, SN, NM, MAT}
) where {N, SN<:AbstractHyperrectangle{N}, NM, MAT<:AbstractMatrix{N}}
Xz = convert(Zonotope, S.X)
return linear_map(S.M, Xz)
S::LinearMap{N, CartesianProduct{N, HN1, HN2}}
) where {N, HN1<:AbstractHyperrectangle{N},
HN2<:AbstractHyperrectangle{N}}
return linear_map(S.M, convert(Zonotope, S.X))
end

"""
convert(::Type{Zonotope},S::LinearMap{N, CartesianProductArray{N, HN}}
) where {N, HN<:AbstractHyperrectangle{N}}

Converts the lazy linear map of the cartesian product of a finite number of
hyperrectangular sets to a zonotope.

### Input

- `Zonotope` -- type, used for dispatch
- `S` -- linear map of a `CartesianProductArray` of hyperrectangular sets

### Output

A zonotope.

### Algorithm

This method first converts the cartesian product array to a zonotope, and then
applies the (concrete) linear map to the zonotope.
"""
function convert(::Type{Zonotope}, S::LinearMap{N, CartesianProductArray{N, HN}}
) where {N, HN<:AbstractHyperrectangle{N}}
return linear_map(S.M, convert(Zonotope, S.X))
end

"""
Expand Down
11 changes: 11 additions & 0 deletions test/unit_Zonotope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ for N in [Float64, Rational{Int}, Float32]
Z = convert(Zonotope, M * B)
@test Z isa Zonotope && Z.center == N[0] && Z.generators == hcat(N[1])

# conversion of the lazy linear map of the cartesian product of hyperrectangular
# sets to a zonotope
B = BallInf(N[0], N(1))
M = N[1 0; 0 -1]
Z = convert(Zonotope, M * (B × B))
@test Z isa Zonotope && Z.center == N[0, 0] && Z.generators == M

# same for CPA
Z2 = convert(Zonotope, M * CartesianProductArray([B, B]))
@test Z2 == Z

# 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