Skip to content

Commit

Permalink
Merge pull request #390 from JuliaReach/schillic/323
Browse files Browse the repository at this point in the history
#323 - Intersection Array
  • Loading branch information
schillic authored Jul 25, 2018
2 parents b9641fb + 99a2143 commit 2c4d18b
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 4 deletions.
11 changes: 11 additions & 0 deletions docs/src/lib/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ monotone_chain!

## Intersection

### Binary Intersection

```@docs
Intersection
dim(::Intersection{Float64, LazySet{Float64}, LazySet{Float64}})
Expand All @@ -77,6 +79,15 @@ dim(::Intersection{Float64, LazySet{Float64}, LazySet{Float64}})
isempty(::Intersection{Float64, LazySet{Float64}, LazySet{Float64}})
```

### ``n``-ary Intersection

```@docs
IntersectionArray
array(::IntersectionArray{Float64, LazySet{Float64}})
dim(::IntersectionArray{Float64, LazySet{Float64}})
σ(::AbstractVector{Float64}, ::IntersectionArray{Float64, LazySet{Float64}})
```

## Minkowski Sum

### Binary Minkowski Sum
Expand Down
139 changes: 136 additions & 3 deletions src/Intersection.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Base: isempty, ,

export Intersection
export Intersection,
IntersectionArray,
array

"""
Intersection{N<:Real, S1<:LazySet{N}, S2<:LazySet{N}} <: LazySet{N}
Expand Down Expand Up @@ -81,11 +83,11 @@ end
"""
∈(x::AbstractVector{N}, cap::Intersection{N})::Bool where {N<:Real}
Check whether a given point is contained in a intersection of two convex sets.
Check whether a given point is contained in an intersection of two convex sets.
### Input
- `x` -- point/vector
- `x` -- point/vector
- `cap` -- intersection of two convex sets
### Output
Expand Down Expand Up @@ -116,3 +118,134 @@ Return if the intersection is empty or not.
function isempty(cap::Intersection)::Bool
return is_intersection_empty(cap.X, cap.Y)
end


# ================================
# intersection of an array of sets
# ================================

"""
IntersectionArray{N<:Real, S<:LazySet{N}} <: LazySet{N}
Type that represents the intersection of a finite number of convex sets.
### Fields
- `array` -- array of convex sets
### Notes
This type assumes that the dimensions of all elements match.
The `EmptySet` is the absorbing element for `IntersectionArray`.
Constructors:
- `IntersectionArray(array::Vector{<:LazySet})` -- default constructor
- `IntersectionArray([n]::Int=0, [N]::Type=Float64)`
-- constructor for an empty sum with optional size hint and numeric type
"""
struct IntersectionArray{N<:Real, S<:LazySet{N}} <: LazySet{N}
array::Vector{S}
end

# type-less convenience constructor
IntersectionArray(arr::Vector{S}) where {S<:LazySet{N}} where {N<:Real} =
IntersectionArray{N, S}(arr)

# constructor for an empty sum with optional size hint and numeric type
function IntersectionArray(n::Int=0, N::Type=Float64)::IntersectionArray
arr = Vector{LazySet{N}}(0)
sizehint!(arr, n)
return IntersectionArray(arr)
end

# EmptySet is the absorbing element for IntersectionArray
@absorbing(IntersectionArray, EmptySet)

# add functions connecting Intersection and IntersectionArray
@declare_array_version(Intersection, IntersectionArray)

"""
array(ia::IntersectionArray{N, S})::Vector{S} where {N<:Real, S<:LazySet{N}}
Return the array of an intersection of a finite number of convex sets.
### Input
- `ia` -- intersection of a finite number of convex sets
### Output
The array of an intersection of a finite number of convex sets.
"""
function array(ia::IntersectionArray{N, S})::Vector{S} where {N<:Real, S<:LazySet{N}}
return ia.array
end


# --- LazySet interface functions ---


"""
dim(ia::IntersectionArray)::Int
Return the dimension of an intersection of a finite number of sets.
### Input
- `ia` -- intersection of a finite number of convex sets
### Output
The ambient dimension of the intersection of a finite number of sets.
"""
function dim(ia::IntersectionArray)::Int
return length(ia.array) == 0 ? 0 : dim(ia.array[1])
end

"""
σ(d::AbstractVector{<:Real}, ia::IntersectionArray)::Vector{<:Real}
Return the support vector of an intersection of a finite number of sets in a
given direction.
### Input
- `d` -- direction
- `ia` -- intersection of a finite number of convex sets
### Output
The support vector in the given direction.
If the direction has norm zero, the result depends on the individual sets.
"""
function σ(d::AbstractVector{<:Real}, ia::IntersectionArray)::Vector{<:Real}
# TODO implement
error("not implemented yet")
end

"""
∈(x::AbstractVector{N}, ia::IntersectionArray{N})::Bool where {N<:Real}
Check whether a given point is contained in an intersection of a finite number
of convex sets.
### Input
- `x` -- point/vector
- `ia` -- intersection of a finite number of convex sets
### Output
`true` iff ``x ∈ ia``.
"""
function (x::AbstractVector{N}, ia::IntersectionArray{N})::Bool where {N<:Real}
for S in ia.array
if x S
return false
end
end
return true
end
28 changes: 27 additions & 1 deletion test/unit_Intersection.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
for N in [Float64, Rational{Int}, Float32]
B = BallInf(ones(N, 2), N(3.))
H = Hyperrectangle(ones(N, 2), ones(N, 2))
E = EmptySet{N}()

# intersection of two sets
I = Intersection(B, H)

# dim
@test dim(I) == 2

# support vector (error)
# support vector (currently throws an error)
@test_throws ErrorException σ(ones(N, 2), I)

# membership
@test (ones(N, 2), I) && !(N[5., 5.], I)

# emptiness of intersection
@test !isempty(I)

# ---

# intersection of an array of sets
IA = IntersectionArray([B, H])

# dim
@test dim(IA) == 2

# support vector (currently throws an error)
@test_throws ErrorException σ(ones(N, 2), IA)

# membership
@test (ones(N, 2), IA) && !(N[5., 5.], IA)

# array getter
v = Vector{LazySet{N}}(0)
@test array(IntersectionArray(v)) v

# ---

# absorbing element
@test I E == E I == IA E == E IA == E E == E
end

0 comments on commit 2c4d18b

Please sign in to comment.