Skip to content

Commit

Permalink
Merge pull request #1526 from JuliaReach/schillic/817
Browse files Browse the repository at this point in the history
#817 - Add swap methods to binary lazy operations
  • Loading branch information
schillic authored Jul 28, 2019
2 parents 9bc60f0 + c1a7ac5 commit 7e9f0ea
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 6 deletions.
6 changes: 5 additions & 1 deletion docs/src/lib/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ CurrentModule = LazySets
CartesianProduct
×(::LazySet, ::LazySet)
*(::LazySet, ::LazySet)
swap(::CartesianProduct)
dim(::CartesianProduct)
ρ(::AbstractVector{N}, ::CartesianProduct{N}) where {N<:Real}
σ(::AbstractVector{N}, ::CartesianProduct{N}) where {N<:Real}
Expand Down Expand Up @@ -66,6 +67,7 @@ Inherited from [`LazySet`](@ref):
```@docs
ConvexHull
CH
swap(::ConvexHull)
dim(::ConvexHull)
ρ(::AbstractVector{N}, ::ConvexHull{N}) where {N<:Real}
σ(::AbstractVector{N}, ::ConvexHull{N}) where {N<:Real}
Expand Down Expand Up @@ -122,7 +124,7 @@ isempty(::Intersection)
constraints_list(::Intersection{N}) where {N<:Real}
isempty_known(::Intersection)
set_isempty!(::Intersection, ::Bool)
swap(::Intersection)
swap(::Intersection{N, S1, S2}) where {N<:Real, S1, S2}
use_precise_ρ
_line_search
_projection
Expand Down Expand Up @@ -168,6 +170,7 @@ Inherited from [`LazySet`](@ref):
MinkowskiSum
⊕(::LazySet, ::LazySet)
+(::LazySet, ::LazySet)
swap(::MinkowskiSum)
dim(::MinkowskiSum)
ρ(::AbstractVector{N}, ::MinkowskiSum{N}) where {N<:Real}
σ(::AbstractVector{N}, ::MinkowskiSum{N}) where {N<:Real}
Expand Down Expand Up @@ -367,6 +370,7 @@ Hence these set types are not part of the convex-set family `LazySet`.
```@docs
UnionSet
∪(::LazySet, ::LazySet)
swap(::UnionSet)
dim(::UnionSet)
σ(::AbstractVector{N}, ::UnionSet{N}; algorithm="support_vector") where {N<:Real}
ρ(::AbstractVector{N}, ::UnionSet{N}) where {N<:Real}
Expand Down
18 changes: 18 additions & 0 deletions src/CartesianProduct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export CartesianProduct,
CartesianProductArray,
CartesianProduct!,
array,
swap,
same_block_structure

"""
Expand Down Expand Up @@ -67,6 +68,23 @@ Alias for the binary Cartesian product.
"""
×(X::LazySet, Y::LazySet) = CartesianProduct(X, Y)

"""
swap(cp::CartesianProduct)
Return a new `CartesianProduct` object with the arguments swapped.
### Input
- `cp` -- Cartesian product of two convex sets
### Output
A new `CartesianProduct` object with the arguments swapped.
"""
function swap(cp::CartesianProduct)
return CartesianProduct(cp.Y, cp.X)
end

"""
dim(cp::CartesianProduct)::Int
Expand Down
20 changes: 19 additions & 1 deletion src/ConvexHull.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export ConvexHull, CH,
convex_hull!,
ConvexHullArray, CHArray,
ConvexHull!,
array
array,
swap

"""
ConvexHull{N<:Real, S1<:LazySet{N}, S2<:LazySet{N}} <: LazySet{N}
Expand Down Expand Up @@ -64,6 +65,23 @@ Alias for `ConvexHull`.
"""
const CH = ConvexHull

"""
swap(ch::ConvexHull)
Return a new `ConvexHull` object with the arguments swapped.
### Input
- `ch` -- convex hull of two convex sets
### Output
A new `ConvexHull` object with the arguments swapped.
"""
function swap(ch::ConvexHull)
return ConvexHull(ch.Y, ch.X)
end

"""
dim(ch::ConvexHull)::Int
Expand Down
5 changes: 2 additions & 3 deletions src/Intersection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function set_isempty!(cap::Intersection, isempty::Bool)
end

"""
swap(cap::Intersection{N, S1, S2})::Intersection{N} where {N<:Real, S1, S2}
swap(cap::Intersection{N, S1, S2}) where {N<:Real, S1, S2}
Return a new `Intersection` object with the arguments swapped.
Expand All @@ -178,8 +178,7 @@ The old cache is shared between the old and new objects.
The advantage of using this function instead of manually swapping the arguments
is that the cache is shared.
"""
function swap(cap::Intersection{N, S1, S2}
)::Intersection{N} where {N<:Real, S1, S2}
function swap(cap::Intersection{N, S1, S2}) where {N<:Real, S1, S2}
return Intersection{N, S2, S1}(cap.Y, cap.X, cache=cap.cache)
end

Expand Down
18 changes: 18 additions & 0 deletions src/MinkowskiSum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export MinkowskiSum, ⊕,
MinkowskiSumArray,
MinkowskiSum!,
array,
swap,
CacheMinkowskiSum,
forget_sets!

Expand Down Expand Up @@ -69,6 +70,23 @@ Unicode alias constructor ⊕ (`oplus`) for the lazy Minkowski sum operator.
"""
(X::LazySet, Y::LazySet) = MinkowskiSum(X, Y)

"""
swap(ms::MinkowskiSum)
Return a new `MinkowskiSum` object with the arguments swapped.
### Input
- `ms` -- Minkowski sum of two convex sets
### Output
A new `MinkowskiSum` object with the arguments swapped.
"""
function swap(ms::MinkowskiSum)
return MinkowskiSum(ms.Y, ms.X)
end

"""
dim(ms::MinkowskiSum)::Int
Expand Down
20 changes: 19 additions & 1 deletion src/UnionSet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import Base: isempty, ∈, ∪

export UnionSet,
UnionSetArray,
array
array,
swap

# ========================================
# Binary set union
Expand Down Expand Up @@ -42,6 +43,23 @@ Alias for `UnionSet`.
"""
(X::LazySet, Y::LazySet) = UnionSet(X, Y)

"""
swap(cup::UnionSet)
Return a new `UnionSet` object with the arguments swapped.
### Input
- `cup` -- union of two convex sets
### Output
A new `UnionSet` object with the arguments swapped.
"""
function swap(cup::UnionSet)
return UnionSet(cup.Y, cup.X)
end

"""
dim(cup::UnionSet)::Int
Expand Down
5 changes: 5 additions & 0 deletions test/unit_CartesianProduct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ for N in [Float64, Float32, Rational{Int}]
cp = CartesianProduct(b1, b2)
@test cp.X == b1
@test cp.Y == b2

# swap
cp2 = swap(cp)
@test cp.X == cp2.Y && cp.Y == cp2.X

# Test Dimension
@test dim(cp) == 3
# Test Support Vector
Expand Down
5 changes: 5 additions & 0 deletions test/unit_ConvexHull.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ for N in [Float64, Rational{Int}, Float32]
# Test Construction
ch = ConvexHull(b1, b2)
@test ch == CH(b1, b2)

# swap
ch2 = swap(ch)
@test ch.X == ch2.Y && ch.Y == ch2.X

# Test Dimension
@test dim(ch) == 2
# Test Support Vector
Expand Down
4 changes: 4 additions & 0 deletions test/unit_Intersection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ for N in [Float64, Rational{Int}, Float32]
# intersection of two sets
I = Intersection(B, H)

# swap
I2 = swap(I)
@test I.X == I2.Y && I.Y == I2.X

# dim
@test dim(I) == 2

Expand Down
5 changes: 5 additions & 0 deletions test/unit_MinkowskiSum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ for N in [Float64, Rational{Int}, Float32]
X = MinkowskiSum(b1, b2)
@test X.X == b1
@test X.Y == b2

# swap
ms2 = swap(X)
@test X.X == ms2.Y && X.Y == ms2.X

# Test Dimension
@test dim(X) == 2
# Test Support Vector
Expand Down
4 changes: 4 additions & 0 deletions test/unit_UnionSet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ for N in [Float64, Rational{Int}, Float32]
# array type (union of a finite number of convex sets)
Uarr = UnionSetArray([B1, B2])

# swap
U2 = swap(UXY)
@test UXY.X == U2.Y && UXY.Y == U2.X

for U in [UXY, Uarr]
# dimension
@test dim(U) == dim(B1)
Expand Down

0 comments on commit 7e9f0ea

Please sign in to comment.