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

#822 - Unicode operators #3314

Merged
merged 3 commits into from
May 5, 2023
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/approximations.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ approximate
```@docs
box_approximation
interval_hull
□(::LazySet)
symmetric_interval_hull
box_approximation_symmetric
ballinf_approximation
Expand Down
2 changes: 2 additions & 0 deletions docs/src/lib/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ plot3d!
### Globally defined set functions

```@docs
○(c, a)
isconvextype(::Type{<:LazySet})
low(::LazySet)
high(::LazySet)
Expand Down Expand Up @@ -429,6 +430,7 @@ AbstractHyperrectangle
This interface defines the following functions:

```@docs
□(c, r)
norm(::AbstractHyperrectangle, ::Real=Inf)
radius(::AbstractHyperrectangle, ::Real=Inf)
σ(::AbstractVector, ::AbstractHyperrectangle)
Expand Down
2 changes: 1 addition & 1 deletion src/Approximations/Approximations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/Approximations/box_approximation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
24 changes: 23 additions & 1 deletion src/Interfaces/AbstractHyperrectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export AbstractHyperrectangle,
low, high,
isflat,
rectify,
volume
volume,

"""
AbstractHyperrectangle{N} <: AbstractZonotope{N}
Expand Down Expand Up @@ -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

"""
Expand Down
24 changes: 23 additions & 1 deletion src/Interfaces/LazySet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export LazySet,
project,
rectify,
permute,
chebyshev_center_radius
chebyshev_center_radius,

"""
LazySet{N}
Expand Down Expand Up @@ -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})

Expand Down
4 changes: 4 additions & 0 deletions src/Sets/Ball2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

"""
Expand Down
4 changes: 4 additions & 0 deletions src/Sets/BallInf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

"""
Expand Down
10 changes: 8 additions & 2 deletions src/Sets/Ellipsoid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions src/Sets/Hyperrectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions test/Approximations/box_approximation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
# ===================================================================
Expand Down
3 changes: 3 additions & 0 deletions test/Sets/Ball2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
3 changes: 3 additions & 0 deletions test/Sets/BallInf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 3 additions & 0 deletions test/Sets/Ellipsoid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions test/Sets/Hyperrectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down