Skip to content

Commit

Permalink
Merge pull request #1409 from JuliaReach/schillic/378
Browse files Browse the repository at this point in the history
#378 - Add isapprox between sets
  • Loading branch information
schillic authored Jun 6, 2019
2 parents 9bc6422 + fd2f305 commit 875ecaf
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/src/lib/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ RecipesBase.apply_recipe(::Dict{Symbol,Any}, ::AbstractVector{VN}, ::N=N(1e-3),

```@docs
==(::LazySet, ::LazySet)
≈(::LazySet, ::LazySet)
copy(::LazySet)
```

Expand Down
71 changes: 65 additions & 6 deletions src/LazySet.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Base: ==, copy
import Base: ==, , copy
import Random.rand

export LazySet,
Expand Down Expand Up @@ -293,12 +293,10 @@ 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.
Return whether two LazySets of the same type are exactly equal.
### Input
Expand All @@ -312,10 +310,16 @@ comparing their fields until a mismatch is found.
### 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.
For instance, `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.
### Algorithm
We recursively compare the fields of `X` and `Y` until a mismatch is found.
### Examples
```jldoctest
julia> HalfSpace([1], 1) == HalfSpace([1], 1)
true
Expand All @@ -341,6 +345,61 @@ function ==(X::LazySet, Y::LazySet)

return true
end
"""
≈(X::LazySet, Y::LazySet)
Return whether two LazySets of the same type are approximately equal.
### 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.
For instance, `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.
### Algorithm
We recursively compare the fields of `X` and `Y` until a mismatch is found.
### Examples
```jldoctest
julia> HalfSpace([1], 1) ≈ HalfSpace([1], 1)
true
julia> HalfSpace([1], 1) ≈ HalfSpace([1.00000001], 0.99999999)
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 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

# hook into random API
function rand(rng::AbstractRNG, ::SamplerType{T}) where T<:LazySet
Expand Down

0 comments on commit 875ecaf

Please sign in to comment.