Skip to content

Commit

Permalink
outsource convert methods to set modules
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Jul 20, 2024
1 parent ca65308 commit d7531c4
Show file tree
Hide file tree
Showing 32 changed files with 373 additions and 296 deletions.
11 changes: 0 additions & 11 deletions docs/src/lib/conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ CurrentModule = LazySets
```@docs
convert(::Type{Interval}, ::Rectification{N, IN}) where {N, IN<:Interval}
convert(::Type{Interval}, ::MinkowskiSum{N, IT, IT}) where {N, IT<:Interval}
convert(::Type{IA.IntervalBox}, ::AbstractHyperrectangle)
convert(::Type{Hyperrectangle}, ::IA.IntervalBox)
convert(::Type{Hyperrectangle}, ::AbstractHyperrectangle)
convert(::Type{Hyperrectangle}, ::CartesianProduct{N, HN1, HN2}) where {N, HN1<:AbstractHyperrectangle, HN2<:AbstractHyperrectangle}
convert(::Type{Hyperrectangle}, ::CartesianProductArray{N, HN}) where {N, HN<:AbstractHyperrectangle}
convert(::Type{Hyperrectangle}, ::CartesianProductArray{N, IN}) where {N, IN<:Interval}
Expand All @@ -30,13 +27,7 @@ convert(::Type{HPolygonOpt}, ::LazySet)
convert(::Type{HPolygonOpt}, ::VPolygon)
convert(::Type{HPolygonOpt}, ::LineSegment{N}) where {N}
convert(::Type{HPolygonOpt}, ::AbstractSingleton{N}) where {N}
convert(::Type{HPolyhedron}, ::LazySet)
convert(::Type{HPolyhedron}, ::HRep{N}) where {N}
convert(::Type{HPolytope}, ::LazySet)
convert(::Type{VPolygon}, ::LazySet)
convert(::Type{VPolygon}, ::AbstractHPolygon)
convert(::Type{VPolytope}, ::LazySet)
convert(::Type{Zonotope}, ::AbstractZonotope)
convert(::Type{Zonotope}, ::LinearMap{N, ZN}) where {N, ZN<:AbstractZonotope}
convert(::Type{Zonotope}, ::LinearMap{N, CartesianProduct{N, HN1, HN2}}) where {N, HN1<:AbstractHyperrectangle, HN2<:AbstractHyperrectangle}
convert(::Type{Zonotope}, ::LinearMap{N, CartesianProductArray{N, HN}}) where {N, HN<:AbstractHyperrectangle}
Expand All @@ -47,10 +38,8 @@ convert(::Type{Zonotope}, ::CartesianProductArray{N, HN}) where {N, HN<:Abstract
convert(::Type{CartesianProduct{N, Interval{N}, Interval{N}}}, ::AbstractHyperrectangle{N}) where {N}
convert(::Type{CartesianProductArray{N, Interval{N}}}, ::AbstractHyperrectangle{N}) where {N}
convert(::Type{MinkowskiSumArray}, ::MinkowskiSum{N, ST, MinkowskiSumArray{N, ST}}) where {N, ST}
convert(::Type{HParallelotope}, Z::AbstractZonotope{N}) where {N}
convert(::Type{STAR}, ::AbstractPolyhedron{N}) where {N}
convert(::Type{STAR}, ::Star)
convert(::Type{Star}, ::AbstractPolyhedron{N}) where {N}
convert(::Type{SimpleSparsePolynomialZonotope}, ::AbstractZonotope)
convert(::Type{SimpleSparsePolynomialZonotope}, ::SparsePolynomialZonotope)
convert(::Type{SparsePolynomialZonotope}, ::AbstractZonotope{N}) where {N}
Expand Down
6 changes: 6 additions & 0 deletions docs/src/lib/sets/HParallelotope.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ CurrentModule = LazySets.HParallelotopeModule
HParallelotope
```

## Conversion

```@docs
convert(::Type{HParallelotope}, Z::AbstractZonotope{N}) where {N}
```

## Operations

```@docs
Expand Down
7 changes: 7 additions & 0 deletions docs/src/lib/sets/HPolyhedron.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ CurrentModule = LazySets.HPolyhedronModule
HPolyhedron
```

## Conversion

```@docs
convert(::Type{HPolyhedron}, ::LazySet)
convert(::Type{HPolyhedron}, ::HRep{N}) where {N}
```

## Operations

The following methods are shared between `HPolytope` and `HPolyhedron`.
Expand Down
8 changes: 8 additions & 0 deletions docs/src/lib/sets/HPolytope.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ CurrentModule = LazySets.HPolytopeModule
HPolytope
```

## Conversion

```@docs
convert(::Type{HPolytope}, ::LazySet)
```

## Operations

```@meta
CurrentModule = LazySets
```
Expand Down
8 changes: 8 additions & 0 deletions docs/src/lib/sets/Hyperrectangle.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ CurrentModule = LazySets.HyperrectangleModule
Hyperrectangle
```

## Conversion

```@docs
convert(::Type{Hyperrectangle}, ::AbstractHyperrectangle)
convert(::Type{Hyperrectangle}, ::IA.IntervalBox)
convert(::Type{IA.IntervalBox}, ::AbstractHyperrectangle)
```

## Operations

```@docs
Expand Down
6 changes: 6 additions & 0 deletions docs/src/lib/sets/Star.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ CurrentModule = LazySets.StarModule
Star
```

## Conversion

```@docs
convert(::Type{Star}, ::AbstractPolyhedron{N}) where {N}
```

## Operations

```@docs
Expand Down
6 changes: 6 additions & 0 deletions docs/src/lib/sets/VPolygon.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ CurrentModule = LazySets.VPolygonModule
VPolygon
```

## Conversion

```@docs
convert(::Type{VPolygon}, ::LazySet)
```

## Operations

```@docs
Expand Down
6 changes: 6 additions & 0 deletions docs/src/lib/sets/VPolytope.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ CurrentModule = LazySets.VPolytopeModule
VPolytope
```

## Conversion

```@docs
convert(::Type{VPolytope}, ::LazySet)
```

## Operations

```@docs
Expand Down
6 changes: 6 additions & 0 deletions docs/src/lib/sets/Zonotope.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ CurrentModule = LazySets.ZonotopeModule
Zonotope
```

## Conversion

```@docs
convert(::Type{Zonotope}, ::AbstractZonotope)
```

## Operations

```@docs
Expand Down
6 changes: 5 additions & 1 deletion src/Sets/HParallelotope/HParallelotopeModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module HParallelotopeModule

using Reexport, Requires

using ..LazySets: AbstractZonotope, HalfSpace, generators_fallback
using ..LazySets: AbstractZonotope, HalfSpace, generators_fallback, order,
_constraints_list_zonotope
using LinearAlgebra: checksquare, det
using Random: AbstractRNG, GLOBAL_RNG
using ReachabilityBase.Arrays: to_negative_vector
Expand All @@ -12,6 +13,7 @@ using ReachabilityBase.Require: require
@reexport import ..API: center, constraints_list, dim, isoperationtype, rand,
volume
@reexport import ..LazySets: generators, genmat
import Base: convert
@reexport using ..API

export HParallelotope,
Expand All @@ -35,6 +37,8 @@ include("offset.jl")
include("rand.jl")
include("volume.jl")

include("convert.jl")

include("init.jl")

end # module
39 changes: 39 additions & 0 deletions src/Sets/HParallelotope/convert.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
convert(::Type{HParallelotope}, Z::AbstractZonotope{N}) where {N}
Convert a zonotopic set of order one to a parallelotope in constraint
representation.
### Input
- `HParallelotope` -- target type
- `Z` -- zonotopic set of order one
### Output
A parallelotope in constraint representation.
### Notes
This function requires that the list of constraints of `Z` are obtained in
the particular order returned from the `constraints_list` function of a
`Zonotope`. Hence it first converts `Z` to a `Zonotope`.
"""
function convert(::Type{HParallelotope}, Z::AbstractZonotope{N}) where {N}
@assert order(Z) == 1 "cannot convert a zonotope that is not of order 1 " *
"to a parallelotope"
n = dim(Z)

constraints = _constraints_list_zonotope(Z)

D = Matrix{N}(undef, n, n)
c = Vector{N}(undef, 2n)
j = 1
@inbounds for i in 1:n
D[i, :] = constraints[j].a
c[i] = constraints[j].b
c[i + n] = constraints[j + 1].b
j += 2
end
return HParallelotope(D, c; check_consistency=false)
end
4 changes: 3 additions & 1 deletion src/Sets/HPolyhedron/HPolyhedronModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module HPolyhedronModule

using Reexport, Requires

using ..LazySets: AbstractPolyhedron, default_lp_solver,
using ..LazySets: AbstractPolyhedron, LazySet, default_lp_solver,
default_polyhedra_backend, iscomplement, is_lp_infeasible,
is_lp_optimal, is_lp_unbounded, has_lp_infeasibility_ray,
linprog, tosimplehrep, _isempty_polyhedron, _normal_Vector
Expand Down Expand Up @@ -49,6 +49,8 @@ include("tohrep.jl")
include("tovrep.jl")
include("addconstraint.jl")

include("convert.jl")

function load_polyhedra_hpolyhedron() # function to be loaded by Requires
return quote
using .Polyhedra: HRep,
Expand Down
33 changes: 33 additions & 0 deletions src/Sets/HPolyhedron/convert.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
convert(::Type{HPolyhedron}, X::LazySet)
Convert a polyhedral set to a polyhedron in constraint representation.
### Input
- `HPolyhedron` -- target type
- `X` -- polyhedral set
### Output
The given set represented as a polyhedron in constraint representation.
### Algorithm
This method uses `constraints_list`.
"""
function convert(::Type{HPolyhedron}, X::LazySet)
if !is_polyhedral(X)
error("conversion to `HPolyhedron` requires a polyhedral set")
end
return HPolyhedron(constraints_list(X))
end

convert(::Type{HPolyhedron{N,VT}}, P::HPolyhedron{N,VT}) where {N,VT} = P

function convert(::Type{HPolyhedron{N,VT}}, X::LazySet) where {N,VT}
if !is_polyhedral(X)
error("conversion to `HPolyhedron` requires a polyhedral set")
end
return HPolyhedron([HalfSpace(VT(c.a), N(c.b)) for c in constraints(X)])
end
6 changes: 4 additions & 2 deletions src/Sets/HPolytope/HPolytopeModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module HPolytopeModule

using Reexport, Requires

using ..LazySets: AbstractPolytope, AbstractLinearMapAlgorithm,
using ..LazySets: AbstractPolytope, LazySet, AbstractLinearMapAlgorithm,
default_polyhedra_backend, vertices_list_1d, _linear_map_hrep,
_normal_Vector
using ..HalfSpaceModule: HalfSpace
Expand All @@ -13,7 +13,7 @@ using ReachabilityBase.Require: require

@reexport import ..API: isbounded, isoperationtype, rand, vertices_list
@reexport import ..LazySets: _linear_map_hrep_helper, _vertices_list
@reexport import Base: convert
import Base: convert
@reexport using ..API

export HPolytope
Expand All @@ -26,6 +26,8 @@ include("rand.jl")
include("vertices_list.jl")
include("linear_map.jl")

include("convert.jl")

function load_polyhedra_hpolytope() # function to be loaded by Requires
return quote
using .Polyhedra: HRep
Expand Down
33 changes: 33 additions & 0 deletions src/Sets/HPolytope/convert.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
convert(::Type{HPolytope}, X::LazySet)
Convert a polytopic set to a polytope in constraint representation.
### Input
- `HPolytope` -- target type
- `X` -- polytopic set
### Output
The given polytope represented as a polytope in constraint representation.
### Algorithm
This method uses `constraints_list`.
"""
function convert(::Type{HPolytope}, X::LazySet)
if !isboundedtype(typeof(X)) || !is_polyhedral(X)
error("conversion to `HPolytope` requires a polytopic set")
end
return HPolytope(constraints_list(X))
end

convert(::Type{HPolytope{N,VT}}, P::HPolytope{N,VT}) where {N,VT} = P

function convert(::Type{HPolytope{N,VT}}, X::LazySet) where {N,VT}
if !isboundedtype(typeof(X)) || !is_polyhedral(X)
error("conversion to `HPolytope` requires a polytopic set")
end
return HPolytope([HalfSpace(VT(c.a), N(c.b)) for c in constraints_list(X)])
end
2 changes: 2 additions & 0 deletions src/Sets/HalfSpace/HalfSpaceModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ include("halfspace_right.jl")
include("iscomplement.jl")
include("normalize.jl")

include("convert.jl")

"""
LinearConstraint
Expand Down
5 changes: 5 additions & 0 deletions src/Sets/HalfSpace/convert.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# convert to concrete Vector representation
function convert(::Type{HalfSpace{N,Vector{N}}},
hs::HalfSpace{N,<:AbstractVector{N}}) where {N}
return HalfSpace(Vector(hs.a), hs.b)
end
3 changes: 3 additions & 0 deletions src/Sets/Hyperrectangle/HyperrectangleModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using SparseArrays: SparseVector, findnz, sparse
@reexport import ..API: center, isoperationtype, rand, permute, scale!, ρ, σ,
translate, translate!
@reexport import ..LazySets: genmat, radius_hyperrectangle, □, _genmat_static
import Base: convert
@reexport using ..API

export Hyperrectangle
Expand All @@ -31,6 +32,8 @@ include("translate.jl")
include("genmat.jl")
include("radius_hyperrectangle.jl")

include("convert.jl")

function (c::VNC, r::VNR) where {N,VNC<:AbstractVector{N},VNR<:AbstractVector{N}}
return Hyperrectangle(c, r)
end
Expand Down
Loading

0 comments on commit d7531c4

Please sign in to comment.