From a690b637e1260d73d193af8ce754f210db530b32 Mon Sep 17 00:00:00 2001 From: schillic Date: Thu, 27 Apr 2023 22:16:41 +0200 Subject: [PATCH 1/3] unicode constructor for hyperrectangles --- docs/src/lib/interfaces.md | 1 + src/Interfaces/AbstractHyperrectangle.jl | 24 +++++++++++++++++++++++- src/Sets/BallInf.jl | 4 ++++ src/Sets/Hyperrectangle.jl | 4 ++++ test/Sets/BallInf.jl | 3 +++ test/Sets/Hyperrectangle.jl | 3 +++ 6 files changed, 38 insertions(+), 1 deletion(-) diff --git a/docs/src/lib/interfaces.md b/docs/src/lib/interfaces.md index 5d25bc6353..a3996381ab 100644 --- a/docs/src/lib/interfaces.md +++ b/docs/src/lib/interfaces.md @@ -429,6 +429,7 @@ AbstractHyperrectangle This interface defines the following functions: ```@docs +□(c, r) norm(::AbstractHyperrectangle, ::Real=Inf) radius(::AbstractHyperrectangle, ::Real=Inf) σ(::AbstractVector, ::AbstractHyperrectangle) diff --git a/src/Interfaces/AbstractHyperrectangle.jl b/src/Interfaces/AbstractHyperrectangle.jl index f659476887..fe46d233cb 100644 --- a/src/Interfaces/AbstractHyperrectangle.jl +++ b/src/Interfaces/AbstractHyperrectangle.jl @@ -7,7 +7,8 @@ export AbstractHyperrectangle, low, high, isflat, rectify, - volume + volume, + □ """ AbstractHyperrectangle{N} <: AbstractZonotope{N} @@ -46,6 +47,27 @@ julia> subtypes(AbstractHyperrectangle) """ abstract type AbstractHyperrectangle{N} <: AbstractZonotope{N} end +""" + □(c, r) + +Convenience constructor of `Hyperrectangle`s or `BallInf`s depending on the type +of `r`. + +### Input + +- `c` -- center +- `r` -- radius (either a vector for `Hyperrectangle` or a number for `BallInf`) + +### Output + +A `Hyperrectangle`s or `BallInf`s depending on the type of `r`. + +### Notes + +The function symbol can be typed via `\\square[TAB]`. +""" +function □(c, r) end + isconvextype(::Type{<:AbstractHyperrectangle}) = true """ diff --git a/src/Sets/BallInf.jl b/src/Sets/BallInf.jl index 95ca9d1bda..8e09639ea7 100644 --- a/src/Sets/BallInf.jl +++ b/src/Sets/BallInf.jl @@ -55,6 +55,10 @@ struct BallInf{N, VN<:AbstractVector{N}} <: AbstractHyperrectangle{N} end end +function □(c::VN, r::N) where {N, VN<:AbstractVector{N}} + return BallInf(c, r) +end + isoperationtype(::Type{<:BallInf}) = false """ diff --git a/src/Sets/Hyperrectangle.jl b/src/Sets/Hyperrectangle.jl index 2b9a82fb7d..10a4b55892 100644 --- a/src/Sets/Hyperrectangle.jl +++ b/src/Sets/Hyperrectangle.jl @@ -106,6 +106,10 @@ function Hyperrectangle(; high::AbstractVector, return Hyperrectangle(center, radius, check_bounds=check_bounds) end +function □(c::VNC, r::VNR) where {N, VNC<:AbstractVector{N}, VNR<:AbstractVector{N}} + return Hyperrectangle(c, r) +end + """ radius_hyperrectangle(H::Hyperrectangle, i::Int) diff --git a/test/Sets/BallInf.jl b/test/Sets/BallInf.jl index 76d951ff99..5fa5ae1033 100644 --- a/test/Sets/BallInf.jl +++ b/test/Sets/BallInf.jl @@ -54,6 +54,9 @@ for N in [Float64, Rational{Int}, Float32] d = N[1, -1] @test σ(d, b) == N[2, -2] + # unicode constructor + @test □(center(b), radius(b)) == b + # center c = N[1, 2] b = BallInf(c, N(2)) diff --git a/test/Sets/Hyperrectangle.jl b/test/Sets/Hyperrectangle.jl index 273a1af48c..25184082ce 100644 --- a/test/Sets/Hyperrectangle.jl +++ b/test/Sets/Hyperrectangle.jl @@ -95,6 +95,9 @@ for N in [Float64, Rational{Int}, Float32] # diameter @test diameter(h) == norm(N[5, 3] - N[1, 1], Inf) + # unicode constructor + @test □(center(h), radius_hyperrectangle(h)) == h + # generators matrix for sparse hyperrectangle @test genmat(Hyperrectangle(sparsevec(N[3, 2]), sparsevec(N[2, 1]))) isa SparseMatrixCSC From 0da9d027e9a1c8b154b8e683a709dc37aad53e27 Mon Sep 17 00:00:00 2001 From: schillic Date: Thu, 27 Apr 2023 22:27:59 +0200 Subject: [PATCH 2/3] unicode constructor for ellipsoids --- docs/src/lib/interfaces.md | 1 + src/Interfaces/LazySet.jl | 24 +++++++++++++++++++++++- src/Sets/Ball2.jl | 4 ++++ src/Sets/Ellipsoid.jl | 10 ++++++++-- test/Sets/Ball2.jl | 3 +++ test/Sets/Ellipsoid.jl | 3 +++ 6 files changed, 42 insertions(+), 3 deletions(-) diff --git a/docs/src/lib/interfaces.md b/docs/src/lib/interfaces.md index a3996381ab..88d8aaf83f 100644 --- a/docs/src/lib/interfaces.md +++ b/docs/src/lib/interfaces.md @@ -62,6 +62,7 @@ plot3d! ### Globally defined set functions ```@docs +○(c, a) isconvextype(::Type{<:LazySet}) low(::LazySet) high(::LazySet) diff --git a/src/Interfaces/LazySet.jl b/src/Interfaces/LazySet.jl index 7a3c80eb31..5642fc6878 100644 --- a/src/Interfaces/LazySet.jl +++ b/src/Interfaces/LazySet.jl @@ -40,7 +40,8 @@ export LazySet, project, rectify, permute, - chebyshev_center_radius + chebyshev_center_radius, + ○ """ LazySet{N} @@ -162,6 +163,27 @@ Zonotope """ abstract type LazySet{N} end +""" + ○(c, a) + +Convenience constructor of `Ellipsoid`s or `Ball2`s depending on the type of `a`. + +### Input + +- `c` -- center +- `a` -- additional parameter (either a shape matrix for `Ellipsoid` or a radius + for `Ball2`) + +### Output + +A `Ellipsoid`s or `Ball2`s depending on the type of `a`. + +### Notes + +The function symbol can be typed via `\\bigcirc[TAB]`. +""" +function ○(c, a) end + """ isconvextype(X::Type{<:LazySet}) diff --git a/src/Sets/Ball2.jl b/src/Sets/Ball2.jl index f553beb70f..0f4ef9510a 100644 --- a/src/Sets/Ball2.jl +++ b/src/Sets/Ball2.jl @@ -63,6 +63,10 @@ struct Ball2{N<:AbstractFloat, VN<:AbstractVector{N}} <: AbstractCentrallySymmet end end +function ○(c::VN, r::N) where {N<:AbstractFloat, VN<:AbstractVector{N}} + return Ball2(c, r) +end + isoperationtype(::Type{<:Ball2}) = false """ diff --git a/src/Sets/Ellipsoid.jl b/src/Sets/Ellipsoid.jl index 773da04199..9a33716bff 100644 --- a/src/Sets/Ellipsoid.jl +++ b/src/Sets/Ellipsoid.jl @@ -114,14 +114,20 @@ struct Ellipsoid{N<:AbstractFloat, VN<:AbstractVector{N}, end end -isoperationtype(::Type{<:Ellipsoid}) = false - # convenience constructor for an ellipsoid centered in the origin function Ellipsoid(Q::AbstractMatrix{N}; check_posdef::Bool=true) where {N} # TODO: use similar vector type for the center, see #2032 return Ellipsoid(zeros(N, size(Q, 1)), Q; check_posdef=check_posdef) end +function ○(c::VN, shape_matrix::MN) where {N<:AbstractFloat, + VN<:AbstractVector{N}, + MN<:AbstractMatrix{N}} + return Ellipsoid(c, shape_matrix) +end + +isoperationtype(::Type{<:Ellipsoid}) = false + """ center(E::Ellipsoid) diff --git a/test/Sets/Ball2.jl b/test/Sets/Ball2.jl index 6c90c2b41a..f00a152635 100644 --- a/test/Sets/Ball2.jl +++ b/test/Sets/Ball2.jl @@ -58,6 +58,9 @@ for N in [Float64, Float32] d = N[0, -1] @test σ(d, b) == N[0, -2] + # unicode constructor + @test ○(center(b), radius(b)) == b + # boundedness @test isbounded(b) && isboundedtype(typeof(b)) diff --git a/test/Sets/Ellipsoid.jl b/test/Sets/Ellipsoid.jl index b699b62a1d..eb732aa91f 100644 --- a/test/Sets/Ellipsoid.jl +++ b/test/Sets/Ellipsoid.jl @@ -62,6 +62,9 @@ for N in [Float64, Float32] d = N[0, -1] @test σ(d, E) ≈ N[1, 2 - sqrt(2)] + # unicode constructor + @test ○(center(E), shape_matrix(E)) == E + # boundedness @test isbounded(E) From ca640f5709193a886b077d019a9f711d7db77090 Mon Sep 17 00:00:00 2001 From: schillic Date: Sat, 29 Apr 2023 08:39:43 +0200 Subject: [PATCH 3/3] unicode alias for box_approximation --- docs/src/lib/approximations.md | 1 + src/Approximations/Approximations.jl | 2 +- src/Approximations/box_approximation.jl | 7 +++++++ test/Approximations/box_approximation.jl | 3 +++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/src/lib/approximations.md b/docs/src/lib/approximations.md index 5d5adbe3ef..3c815f071d 100644 --- a/docs/src/lib/approximations.md +++ b/docs/src/lib/approximations.md @@ -47,6 +47,7 @@ approximate ```@docs box_approximation interval_hull +□(::LazySet) symmetric_interval_hull box_approximation_symmetric ballinf_approximation diff --git a/src/Approximations/Approximations.jl b/src/Approximations/Approximations.jl index 41f7181afe..e7467588aa 100644 --- a/src/Approximations/Approximations.jl +++ b/src/Approximations/Approximations.jl @@ -16,7 +16,7 @@ using LazySets.JuMP: Model, set_silent, @variable, @constraint, optimize!, value, @NLobjective import Base: convert -import LazySets: project +import LazySets: project, □ using ..LazySets: @assert, activate_assertions # activate assertions by default diff --git a/src/Approximations/box_approximation.jl b/src/Approximations/box_approximation.jl index 8cd3d56772..db60500a65 100644 --- a/src/Approximations/box_approximation.jl +++ b/src/Approximations/box_approximation.jl @@ -78,6 +78,13 @@ Alias for `box_approximation`. """ interval_hull = box_approximation +""" + □(X::LazySet) + +Alias for `box_approximation(X)`. +""" +□(X::LazySet) = box_approximation(X) + # AbstractHyperrectangle specialization function box_approximation(S::AbstractHyperrectangle) return Hyperrectangle(center(S), radius_hyperrectangle(S)) diff --git a/test/Approximations/box_approximation.jl b/test/Approximations/box_approximation.jl index d59a0f6e14..f025868ca9 100644 --- a/test/Approximations/box_approximation.jl +++ b/test/Approximations/box_approximation.jl @@ -38,6 +38,9 @@ for N in [Float64, Rational{Int}, Float32] E = EmptySet{N}(2) @test box_approximation(E) == E + # alias constructors + @test interval_hull(b) == □(b) == box_approximation(b) + # =================================================================== # Testing box_approximation_symmetric (= symmetric interval hull) # ===================================================================