From f253d5d1bd0aa621b0b180bc23ebd61d3775ce3b Mon Sep 17 00:00:00 2001 From: schillic Date: Mon, 23 Jul 2018 17:40:12 +0200 Subject: [PATCH 1/3] add IntersectionArray type --- src/Intersection.jl | 139 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 136 insertions(+), 3 deletions(-) diff --git a/src/Intersection.jl b/src/Intersection.jl index 87906a1694..a8b6668920 100644 --- a/src/Intersection.jl +++ b/src/Intersection.jl @@ -1,6 +1,8 @@ import Base: isempty, ∈, ∩ -export Intersection +export Intersection, + IntersectionArray, + array """ Intersection{N<:Real, S1<:LazySet{N}, S2<:LazySet{N}} <: LazySet{N} @@ -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 @@ -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 From 2568bd65af803282cc4af449c24963294c739315 Mon Sep 17 00:00:00 2001 From: schillic Date: Mon, 23 Jul 2018 17:40:32 +0200 Subject: [PATCH 2/3] docs for IntersectionArray --- docs/src/lib/operations.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/src/lib/operations.md b/docs/src/lib/operations.md index 9ab30448cf..e46ec20fc6 100644 --- a/docs/src/lib/operations.md +++ b/docs/src/lib/operations.md @@ -69,6 +69,8 @@ monotone_chain! ## Intersection +### Binary Intersection + ```@docs Intersection dim(::Intersection{Float64, LazySet{Float64}, LazySet{Float64}}) @@ -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 From 99a214378ec8a0226ace8bdfe5da8d792c322132 Mon Sep 17 00:00:00 2001 From: schillic Date: Mon, 23 Jul 2018 17:41:23 +0200 Subject: [PATCH 3/3] unit tests for IntersectionArray --- test/unit_Intersection.jl | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/test/unit_Intersection.jl b/test/unit_Intersection.jl index fae6cea568..d8489569f6 100644 --- a/test/unit_Intersection.jl +++ b/test/unit_Intersection.jl @@ -1,12 +1,15 @@ 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 @@ -14,4 +17,27 @@ for N in [Float64, Rational{Int}, Float32] # 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