-
Notifications
You must be signed in to change notification settings - Fork 32
/
AbstractBallp.jl
196 lines (146 loc) · 4.8 KB
/
AbstractBallp.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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