Skip to content

Commit

Permalink
#58 - modified AbstractHyperrectangle types
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Dec 16, 2017
1 parent e0ce865 commit 3ba2fad
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 249 deletions.
163 changes: 30 additions & 133 deletions src/BallInf.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import Base.LinAlg:norm,
Base.∈
import Base.LinAlg:norm

export BallInf,
vertices_list,
norm,
radius,
diameter,
export BallInf

"""
BallInf{N<:Real} <: LazySet
BallInf{N<:Real} <: AbstractHyperrectangle{N}
Type that represents a ball in the infinity norm.
Expand Down Expand Up @@ -44,7 +38,7 @@ julia> ρ([1., 1.], B)
2.0
```
"""
struct BallInf{N<:Real} <: LazySet
struct BallInf{N<:Real} <: AbstractHyperrectangle{N}
center::Vector{N}
radius::N

Expand All @@ -56,100 +50,70 @@ end
BallInf(center::Vector{N}, radius::N) where {N<:Real} =
BallInf{N}(center, radius)

"""
dim(B::BallInf)::Int
Return the dimension of a ball in the infinity norm.
### Input

- `B` -- ball in the infinity norm
### Output
# --- AbstractHyperrectangle interface functions ---

The ambient dimension of the ball.
"""
function dim(B::BallInf)::Int
return length(B.center)
end

"""
σ(d::AbstractVector{<:Real}, B::BallInf)::AbstractVector{<:Real}
radius_b(B::BallInf{N}, i::Int)::N where {N<:Real}
Return the support vector of an infinity norm ball in a given direction.
Return the box radius of a infinity norm ball in a given dimension.
### Input
- `d` -- direction
- `B` -- ball in the infinity norm
- `B` -- infinity norm ball
### Output
The support vector in the given direction.
If the direction has norm zero, the vertex with biggest values is returned.
Zero.
"""
function σ(d::AbstractVector{<:Real}, B::BallInf)::AbstractVector{<:Real}
return @. B.center + sign_cadlag(d) * B.radius
function radius_b(B::BallInf{N}, i::Int)::N where {N<:Real}
return B.radius
end

"""
vertices_list(B::BallInf{N})::Vector{Vector{N}} where {N<:Real}
radius_b(B::BallInf{N})::N where {N<:Real}
Return the list of vertices of a ball in the infinity norm.
Return the box radius of a infinity norm ball, which is the same in every
dimension.
### Input
- `B` -- ball in the infinity norm
- `B` -- infinity norm ball
### Output
A list of vertices.
### Notes
The box radius of the ball in the infinity norm (a number).
"""
function radius_b(B::BallInf{N})::N where {N<:Real}
return B.radius
end

For high dimensions, it is preferable to develop a `vertex_iterator` approach.

### Examples
# --- AbstractPointSymmetric interface functions ---

```jldoctest
julia> B = BallInf(zeros(2), 0.1)
LazySets.BallInf{Float64}([0.0, 0.0], 0.1)
julia> vertices_list(B)
4-element Array{Array{Float64,1},1}:
[0.1, 0.1]
[-0.1, 0.1]
[0.1, -0.1]
[-0.1, -0.1]
```
"""
function vertices_list(B::BallInf{N})::Vector{Vector{N}} where {N<:Real}
return [B.center .+ si .* B.radius
for si in IterTools.product([[1, -1] for i = 1:dim(B)]...)]
end

"""
norm(B::BallInf, [p]::Real=Inf)::Real
center(B::BallInf{N})::Vector{N} where {N<:Real}
Return the norm of a ball in the infinity norm.
Return the center of a ball in the infinity norm.
### Input
- `B` -- ball in the infinity norm
- `p` -- (optional, default: `Inf`) norm
### Output
A real number representing the norm.
### Notes
The norm of an infinity ball is defined as the norm of the enclosing ball, of
the given ``p``-norm, of minimal volume.
The center of the ball in the infinity norm.
"""
function norm(B::BallInf, p::Real=Inf)::Real
return maximum(map(x -> norm(x, p), vertices_list(B)))
function center(B::BallInf{N})::Vector{N} where {N<:Real}
return B.center
end


# --- LazySet interface functions ---


"""
radius(B::BallInf, [p]::Real=Inf)::Real
Expand All @@ -172,70 +136,3 @@ The radius is defined as the radius of the enclosing ball of the given
function radius(B::BallInf, p::Real=Inf)::Real
return (p == Inf) ? B.radius : norm(fill(B.radius, dim(B)), p)
end

"""
diameter(B::BallInf, [p]::Real=Inf)::Real
Return the diameter of a ball in the infinity norm.
### Input
- `B` -- ball in the infinity norm
- `p` -- (optional, default: `Inf`) norm
### Output
A real number representing the diameter.
### Notes
The diameter is defined as the maximum distance in the given ``p``-norm between
any two elements of the set.
Equivalently, it is the diameter of the enclosing ball of the given ``p``-norm
of minimal volume with the same center.
"""
function diameter(B::BallInf, p::Real=Inf)::Real
return radius(B, p) * 2
end

"""
∈(x::AbstractVector{N}, B::BallInf{N})::Bool where {N<:Real}
Check whether a given point is contained in a ball in the infinity norm.
### Input
- `x` -- point/vector
- `B` -- ball in the infinity norm
### Output
`true` iff ``x ∈ B``.
### Algorithm
Let ``B`` be an ``n``-dimensional ball in the infinity norm with radius ``r``
and let ``c_i`` and ``x_i`` be the ball's center and the vector ``x`` in
dimension ``i``, respectively.
Then ``x ∈ B`` iff ``|c_i - x_i| ≤ r`` for all ``i=1,…,n``.
### Examples
```jldoctest
julia> B = BallInf([1., 1.], 1.);
julia> ∈([.5, -.5], B)
false
julia> ∈([.5, 1.5], B)
true
```
"""
function (x::AbstractVector{N}, B::BallInf{N})::Bool where {N<:Real}
@assert length(x) == dim(B)
for i in eachindex(x)
if abs(B.center[i] - x[i]) > B.radius
return false
end
end
return true
end
Loading

0 comments on commit 3ba2fad

Please sign in to comment.