-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3368 from JuliaReach/schillic/1629
Add AbstractBallp interface
- Loading branch information
Showing
18 changed files
with
482 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
export AbstractBallp | ||
|
||
""" | ||
AbstractBallp{N} <: AbstractCentrallySymmetric{N} | ||
Abstract type for p-norm balls. | ||
### Notes | ||
See [`Ballp`](@ref) for a standard implementation of this interface. | ||
Every concrete `AbstractBallp` must define the following methods: | ||
- `center(::AbstractBallp)` -- return the center | ||
- `radius_ball(::AbstractBallp)` -- return the ball radius | ||
- `ball_norm(::AbstractBallp)` -- return the characteristic norm | ||
The subtypes of `AbstractBallp`: | ||
```jldoctest; setup = :(using LazySets: subtypes) | ||
julia> subtypes(AbstractBallp) | ||
2-element Vector{Any}: | ||
Ball2 | ||
Ballp | ||
``` | ||
There are two further set types implementing the `AbstractBallp` interface, but | ||
they also implement other interfaces and hence cannot be subtypes: `Ball1` and | ||
`BallInf`. | ||
""" | ||
abstract type AbstractBallp{N} <: AbstractCentrallySymmetric{N} end | ||
|
||
function low(B::AbstractBallp) | ||
return _low_AbstractBallp(B) | ||
end | ||
|
||
function _low_AbstractBallp(B::LazySet) | ||
return center(B) .- radius_ball(B) | ||
end | ||
|
||
function low(B::AbstractBallp, i::Int) | ||
return _low_AbstractBallp(B, i) | ||
end | ||
|
||
function _low_AbstractBallp(B::LazySet, i::Int) | ||
return center(B, i) - radius_ball(B) | ||
end | ||
|
||
function high(B::AbstractBallp) | ||
return _high_AbstractBallp(B) | ||
end | ||
|
||
function _high_AbstractBallp(B::LazySet) | ||
return center(B) .+ radius_ball(B) | ||
end | ||
|
||
function high(B::AbstractBallp, i::Int) | ||
return _high_AbstractBallp(B, i) | ||
end | ||
|
||
function _high_AbstractBallp(B::LazySet, i::Int) | ||
return center(B, i) + radius_ball(B) | ||
end | ||
|
||
|
||
""" | ||
σ(d::AbstractVector, B::AbstractBallp) | ||
Return the support vector of a ball in the p-norm in a given direction. | ||
### Input | ||
- `d` -- direction | ||
- `B` -- ball in the p-norm | ||
### Output | ||
The support vector in the given direction. | ||
If the direction has norm zero, the center of the ball is returned. | ||
### Algorithm | ||
The support vector of the unit ball in the ``p``-norm along direction ``d`` is: | ||
```math | ||
σ(d, \\mathcal{B}_p^n(0, 1)) = \\dfrac{\\tilde{v}}{‖\\tilde{v}‖_q}, | ||
``` | ||
where ``\\tilde{v}_i = \\frac{|d_i|^q}{d_i}`` if ``d_i ≠ 0`` and | ||
``\\tilde{v}_i = 0`` otherwise, for all ``i=1,…,n``, and ``q`` is the conjugate | ||
number of ``p``. | ||
By the affine transformation ``x = r\\tilde{x} + c``, one obtains that | ||
the support vector of ``\\mathcal{B}_p^n(c, r)`` is | ||
```math | ||
σ(d, \\mathcal{B}_p^n(c, r)) = \\dfrac{v}{‖v‖_q}, | ||
``` | ||
where ``v_i = c_i + r\\frac{|d_i|^q}{d_i}`` if ``d_i ≠ 0`` and ``v_i = 0`` | ||
otherwise, for all ``i = 1, …, n``. | ||
""" | ||
function σ(d::AbstractVector, B::AbstractBallp) | ||
p = ball_norm(B) | ||
q = p / (p - 1) | ||
v = similar(d) | ||
N = promote_type(eltype(d), eltype(B)) | ||
@inbounds for (i, di) in enumerate(d) | ||
v[i] = di == zero(N) ? di : abs.(di) .^ q / di | ||
end | ||
vnorm = norm(v, p) | ||
if isapproxzero(vnorm) | ||
svec = copy(center(B)) | ||
else | ||
svec = center(B) .+ radius_ball(B) .* (v ./ vnorm) | ||
end | ||
return svec | ||
end | ||
|
||
""" | ||
ρ(d::AbstractVector, B::AbstractBallp) | ||
Evaluate the support function of a ball in the p-norm in the given direction. | ||
### Input | ||
- `d` -- direction | ||
- `B` -- ball in the p-norm | ||
### Output | ||
Evaluation of the support function in the given direction. | ||
### Algorithm | ||
Let ``c`` and ``r`` be the center and radius of the ball ``B`` in the p-norm, | ||
respectively, and let ``q = \\frac{p}{p-1}``. Then: | ||
```math | ||
ρ(d, B) = ⟨d, c⟩ + r ‖d‖_q. | ||
``` | ||
""" | ||
function ρ(d::AbstractVector, B::AbstractBallp) | ||
p = ball_norm(B) | ||
q = p / (p - 1) | ||
return dot(d, center(B)) + radius_ball(B) * norm(d, q) | ||
end | ||
|
||
""" | ||
∈(x::AbstractVector, B::AbstractBallp) | ||
Check whether a given point is contained in a ball in the p-norm. | ||
### Input | ||
- `x` -- point/vector | ||
- `B` -- ball in the p-norm | ||
### Output | ||
`true` iff ``x ∈ B``. | ||
### Notes | ||
This implementation is worst-case optimized, i.e., it is optimistic and first | ||
computes (see below) the whole sum before comparing to the radius. | ||
In applications where the point is typically far away from the ball, a fail-fast | ||
implementation with interleaved comparisons could be more efficient. | ||
### Algorithm | ||
Let ``B`` be an ``n``-dimensional ball in the p-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 ``\\left( ∑_{i=1}^n |c_i - x_i|^p \\right)^{1/p} ≤ r``. | ||
### Examples | ||
```jldoctest | ||
julia> B = Ballp(1.5, [1.0, 1.0], 1.) | ||
Ballp{Float64, Vector{Float64}}(1.5, [1.0, 1.0], 1.0) | ||
julia> [0.5, -0.5] ∈ B | ||
false | ||
julia> [0.5, 1.5] ∈ B | ||
true | ||
``` | ||
""" | ||
function ∈(x::AbstractVector, B::AbstractBallp) | ||
@assert length(x) == dim(B) "a vector of length $(length(x)) is " * | ||
"incompatible with a set of dimension $(dim(B))" | ||
N = promote_type(eltype(x), eltype(B)) | ||
p = ball_norm(B) | ||
sum = zero(N) | ||
@inbounds for i in eachindex(x) | ||
sum += abs(center(B, i) - x[i])^p | ||
end | ||
return _leq(sum^(one(N) / p), radius_ball(B)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.