Skip to content

Commit

Permalink
#1305 - Set difference between axis-aligned boxes (#1790)
Browse files Browse the repository at this point in the history
* add difference for hyperrectangular sets

* update docs and add test for setdiff of hyperrectangles

* Update src/ConcreteOperations/difference.jl

Co-Authored-By: Christian Schilling <[email protected]>

* Update difference.jl

* Update unit_Hyperrectangle.jl
  • Loading branch information
mforets authored Nov 23, 2019
1 parent 375c103 commit 1525fa8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/lib/binary_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,5 @@ pontryagin_difference
```@docs
\(::LazySet, ::LazySet)
difference(::IN, ::IN) where {N, IN<:Interval{N}}
difference(::AbstractHyperrectangle{N}, ::AbstractHyperrectangle{N}) where {N}
```
41 changes: 41 additions & 0 deletions src/ConcreteOperations/difference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export difference
# alias for set difference
import Base: \

const IA = IntervalArithmetic

"""
\\(X::LazySet, Y::LazySet)
Expand Down Expand Up @@ -111,3 +113,42 @@ function difference(I1::IN, I2::IN)::Union{EmptySet{N}, IN, UnionSet{N, IN, IN}}
end
end
end

# ============================================
# Set difference between hyperrectangular sets
# ============================================

"""
difference(X::AbstractHyperrectangle{N}, Y::AbstractHyperrectangle{N}) where {N}
Return the set difference between the given hyperrectangular sets.
### Input
- `X` -- first hyperrectangular set
- `Y` -- second hyperrectangular set
The set difference is defined as:
```math
X \\setminus Y = \\{x: x ∈ X \\text{ and } x ∉ Y \\}
```
### Output
A `UnionSetArray` consisting of the union of hyperrectangles. Note that this
union is in general not convex.
### Algorithm
This function calls the implementation in `IntervalArithmetic.setdiff`.
### Notes
The backslash symbol, `\\`, can be used as an alias.
"""
function difference(X::AbstractHyperrectangle{N}, Y::AbstractHyperrectangle{N}) where {N}
Xib = convert(IA.IntervalBox, X)
Yib = convert(IA.IntervalBox, Y)
return UnionSetArray(convert.(Hyperrectangle, IA.setdiff(Xib, Yib)))
end
9 changes: 9 additions & 0 deletions test/unit_Hyperrectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,13 @@ for N in [Float64, Rational{Int}, Float32]
H1 = Hyperrectangle(N[0, 1], N[2, 3])
H2 = Hyperrectangle(N[3, 2], N[1, 0])
@test minkowski_sum(H1, H2) == Hyperrectangle(N[3, 3], N[3, 3])

# set difference
h = Hyperrectangle(low=N[0], high=N[1])
q = Hyperrectangle(low=N[0], high=N[0.5])
@test convert(Interval, difference(h, q).array[1]) == Interval(N(0.5), N(1))

# another set difference test in higher-dimensions
b = BallInf(N[0, 0, 0], N(1))
@test isempty(difference(b, b))
end

0 comments on commit 1525fa8

Please sign in to comment.