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

defines == generically to work on LazySet pairs #604

Merged
merged 4 commits into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/lib/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ norm(::LazySet, ::Real)
radius(::LazySet, ::Real)
diameter(::LazySet, ::Real)
an_element(::LazySet{Real})
==(::LazySet, ::LazySet)
```

## Centrally symmetric set
Expand Down
51 changes: 51 additions & 0 deletions src/LazySet.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Base.==

export LazySet,
ρ, support_function,
σ, support_vector,
Expand Down Expand Up @@ -187,3 +189,52 @@ An element of a convex set.
function an_element(S::LazySet{N}) where {N<:Real}
return σ(sparsevec([1], [one(N)], dim(S)), S)
end


"""
==(X::LazySet, Y::LazySet)

Return whether two LazySets of the same type are exactly equal by recursively
comparing their fields until a mismatch is found.

### Input

- `X` -- any `LazySet`
- `Y` -- another `LazySet` of the same type as `X`

### Output

- `true` iff `X` is equal to `Y`.

### Notes

The check is purely syntactic and the sets need to have the same base type.
I.e. `X::VPolytope == Y::HPolytope` returns `false` even if `X` and `Y` represent the
same polytope. However `X::HPolytope{Int64} == Y::HPolytope{Float64}` is a valid comparison.

### Examples
```jldoctest
julia> HalfSpace([1], 1) == HalfSpace([1], 1)
true

julia> HalfSpace([1], 1) == HalfSpace([1.0], 1.0)
true

julia> Ball1([0.], 1.) == Ball2([0.], 1.)
false
```
"""
function ==(X::LazySet, Y::LazySet)
# if the common supertype of X and Y is abstract, they cannot be compared
if Compat.isabstracttype(promote_type(typeof(X), typeof(Y)))
return false
end

for f in fieldnames(typeof(X))
if getfield(X, f) != getfield(Y, f)
return false
end
end

return true
end