From 3bb7b51fc97e999f49f44445a93c68c3451a1a05 Mon Sep 17 00:00:00 2001 From: tomerarnon Date: Sun, 2 Sep 2018 19:58:48 -0700 Subject: [PATCH 1/4] defined == generically to work on LazySet pairs --- src/LazySet.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/LazySet.jl b/src/LazySet.jl index fc9b1caef7..acf16fd374 100644 --- a/src/LazySet.jl +++ b/src/LazySet.jl @@ -1,3 +1,5 @@ +import Base.== + export LazySet, ρ, support_function, σ, support_vector, @@ -187,3 +189,14 @@ An element of a convex set. function an_element(S::LazySet{N}) where {N<:Real} return σ(sparsevec([1], [one(N)], dim(S)), S) end + + +# catch-all definition for == +function ==(a::L, b::L) where L<:LazySet + for f in fieldnames(a) + if getfield(a, f) != getfield(b, f) + return false + end + end + return true +end From 1ad3601173d896eb571e38ffbfab750c080556c4 Mon Sep 17 00:00:00 2001 From: tomerarnon Date: Tue, 4 Sep 2018 00:53:44 -0700 Subject: [PATCH 2/4] Add docstring and define == more generically --- src/LazySet.jl | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/LazySet.jl b/src/LazySet.jl index acf16fd374..225ba5d49a 100644 --- a/src/LazySet.jl +++ b/src/LazySet.jl @@ -191,12 +191,50 @@ function an_element(S::LazySet{N}) where {N<:Real} end -# catch-all definition for == -function ==(a::L, b::L) where L<:LazySet - for f in fieldnames(a) - if getfield(a, f) != getfield(b, f) +""" + ==(a::LazySet, b::LazySet) + +Return whether two LazySets of the same type are exactly equal by recursively +comparing their fields until a mismatch is found. + +### Input + +- `a` -- any `LazySet` +- `b` -- another `LazySet` of the same type as `a` + +### Output + +- `true` iff `a` is equal to `b`. + +### Notes + +The check is purely syntactic and the sets need to have the same base type. +I.e. `a::VPolytope == b::HPolytope` returns `false` even if `a` and `b` represent the +same polytope. However `a::HPolytope{Int64} == b::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 a and b is abstract, they cannot be compared + if Compat.isabstracttype(promote_type(typeof(X), typeof(Y))) + return false + end + + for f in fieldnames(X) + if getfield(X, f) != getfield(Y, f) return false end end + return true end From d141b807c14f82962152255f9eb079528f42f206 Mon Sep 17 00:00:00 2001 From: tomerarnon Date: Tue, 4 Sep 2018 01:11:22 -0700 Subject: [PATCH 3/4] rename a, b to X, Y --- src/LazySet.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/LazySet.jl b/src/LazySet.jl index 225ba5d49a..487430ac7a 100644 --- a/src/LazySet.jl +++ b/src/LazySet.jl @@ -192,25 +192,25 @@ end """ - ==(a::LazySet, b::LazySet) + ==(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 -- `a` -- any `LazySet` -- `b` -- another `LazySet` of the same type as `a` +- `X` -- any `LazySet` +- `Y` -- another `LazySet` of the same type as `X` ### Output -- `true` iff `a` is equal to `b`. +- `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. `a::VPolytope == b::HPolytope` returns `false` even if `a` and `b` represent the -same polytope. However `a::HPolytope{Int64} == b::HPolytope{Float64}` is a valid comparison. +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 @@ -225,7 +225,7 @@ false ``` """ function ==(X::LazySet, Y::LazySet) - # if the common supertype of a and b is abstract, they cannot be compared + # 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 From 0e9996dbb3fd4fcb876f218ef29de93b6b8bb16f Mon Sep 17 00:00:00 2001 From: tomerarnon Date: Tue, 4 Sep 2018 01:31:18 -0700 Subject: [PATCH 4/4] list == in interfaces.md and fix v0.7 deprecation warning for fieldnames() --- docs/src/lib/interfaces.md | 1 + src/LazySet.jl | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/src/lib/interfaces.md b/docs/src/lib/interfaces.md index 7c8f664b5a..aafb05b48b 100644 --- a/docs/src/lib/interfaces.md +++ b/docs/src/lib/interfaces.md @@ -56,6 +56,7 @@ norm(::LazySet, ::Real) radius(::LazySet, ::Real) diameter(::LazySet, ::Real) an_element(::LazySet{Real}) +==(::LazySet, ::LazySet) ``` ## Centrally symmetric set diff --git a/src/LazySet.jl b/src/LazySet.jl index 487430ac7a..1a589424bd 100644 --- a/src/LazySet.jl +++ b/src/LazySet.jl @@ -230,7 +230,7 @@ function ==(X::LazySet, Y::LazySet) return false end - for f in fieldnames(X) + for f in fieldnames(typeof(X)) if getfield(X, f) != getfield(Y, f) return false end