Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1305 - Set difference between axis-aligned boxes #1790

Merged
merged 5 commits into from Nov 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/lib/binary_functions.md
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}
```
39 changes: 39 additions & 0 deletions src/ConcreteOperations/difference.jl
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,40 @@ function difference(I1::IN, I2::IN)::Union{EmptySet{N}, IN, UnionSet{N, IN, IN}}
end
end
end

# ============================================
# Set difference between hyperrectangular sets
# =============================================
mforets marked this conversation as resolved.
Show resolved Hide resolved

"""
difference(X::AbstractHyperrectangle{N}, Y::AbstractHyperrectangle{N}) where {N}

Return the set difference between the given hyperrectangular sets.

The backslash symbol, `\\`, can be used as an alias.
mforets marked this conversation as resolved.
Show resolved Hide resolved

### 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`.
"""
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
5 changes: 5 additions & 0 deletions test/unit_Hyperrectangle.jl
Expand Up @@ -230,4 +230,9 @@ 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))
mforets marked this conversation as resolved.
Show resolved Hide resolved
end