/
Ball2.jl
393 lines (283 loc) · 9.05 KB
/
Ball2.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import Base: rand,
∈
export Ball2,
sample,
volume
"""
Ball2{N<:AbstractFloat, VN<:AbstractVector{N}} <: AbstractCentrallySymmetric{N}
Type that represents a ball in the 2-norm.
### Fields
- `center` -- center of the ball as a real vector
- `radius` -- radius of the ball as a real scalar (``≥ 0``)
### Notes
Mathematically, a ball in the 2-norm is defined as the set
```math
\\mathcal{B}_2^n(c, r) = \\{ x ∈ \\mathbb{R}^n : ‖ x - c ‖_2 ≤ r \\},
```
where ``c ∈ \\mathbb{R}^n`` is its center and ``r ∈ \\mathbb{R}_+`` its radius.
Here ``‖ ⋅ ‖_2`` denotes the Euclidean norm (also known as 2-norm), defined as
``‖ x ‖_2 = \\left( \\sum\\limits_{i=1}^n |x_i|^2 \\right)^{1/2}`` for any
``x ∈ \\mathbb{R}^n``.
### Examples
Create a five-dimensional ball `B` in the 2-norm centered at the origin with
radius 0.5:
```jldoctest ball2_label
julia> B = Ball2(zeros(5), 0.5)
Ball2{Float64, Vector{Float64}}([0.0, 0.0, 0.0, 0.0, 0.0], 0.5)
julia> dim(B)
5
```
Evaluate `B`'s support vector in the direction ``[1,2,3,4,5]``:
```jldoctest ball2_label
julia> σ([1.0, 2, 3, 4, 5], B)
5-element Vector{Float64}:
0.06741998624632421
0.13483997249264842
0.20225995873897262
0.26967994498529685
0.3370999312316211
```
"""
struct Ball2{N<:AbstractFloat, VN<:AbstractVector{N}} <: AbstractCentrallySymmetric{N}
center::VN
radius::N
# default constructor with domain constraint for radius
function Ball2(center::VN, radius::N) where {N<:AbstractFloat, VN<:AbstractVector{N}}
@assert radius >= zero(N) "the radius must not be negative"
return new{N, VN}(center, radius)
end
end
isoperationtype(::Type{<:Ball2}) = false
"""
center(B::Ball2)
Return the center of a ball in the 2-norm.
### Input
- `B` -- ball in the 2-norm
### Output
The center of the ball in the 2-norm.
"""
function center(B::Ball2)
return B.center
end
"""
ρ(d::AbstractVector, B::Ball2)
Return the support function of a 2-norm ball in the given direction.
### Input
- `d` -- direction
- `B` -- ball in the 2-norm
### Output
The support function in the given direction.
### Notes
Let ``c`` and ``r`` be the center and radius of the ball ``B`` in the 2-norm,
respectively. Then:
```math
ρ(d, B) = ⟨d, c⟩ + r ‖d‖_2.
```
"""
function ρ(d::AbstractVector, B::Ball2)
c = B.center
r = B.radius
dnorm = norm(d, 2)
return dot(d, c) + dnorm * r
end
"""
σ(d::AbstractVector, B::Ball2)
Return the support vector of a 2-norm ball in the given direction.
### Input
- `d` -- direction
- `B` -- ball in the 2-norm
### Output
The support vector in the given direction.
If the direction has norm zero, the center is returned.
### Notes
Let ``c`` and ``r`` be the center and radius of a ball ``B`` in the 2-norm,
respectively.
For nonzero direction ``d`` we have
```math
σ(d, B) = c + r \\frac{d}{‖d‖_2}.
```
This function requires computing the 2-norm of the input direction, which is
performed in the given precision of the numeric datatype of both the direction
and the set.
Exact inputs are not supported.
"""
function σ(d::AbstractVector, B::Ball2)
dnorm = norm(d, 2)
if isapproxzero(dnorm)
return B.center
else
return @. B.center + d * (B.radius / dnorm)
end
end
"""
∈(x::AbstractVector, B::Ball2)
Check whether a given point is contained in a ball in the 2-norm.
### Input
- `x` -- point/vector
- `B` -- ball in the 2-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 2-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|^2 \\right)^{1/2} ≤ r``.
### Examples
```jldoctest
julia> B = Ball2([1., 1.], sqrt(0.5))
Ball2{Float64, Vector{Float64}}([1.0, 1.0], 0.7071067811865476)
julia> [.5, 1.6] ∈ B
false
julia> [.5, 1.5] ∈ B
true
```
"""
function ∈(x::AbstractVector, B::Ball2)
@assert length(x) == dim(B)
N = promote_type(eltype(x), eltype(B))
sum = zero(N)
@inbounds for i in eachindex(x)
sum += (B.center[i] - x[i])^2
end
return _leq(sqrt(sum), B.radius)
end
"""
rand(::Type{Ball2}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing)
Create a random ball in the 2-norm.
### Input
- `Ball2` -- type for dispatch
- `N` -- (optional, default: `Float64`) numeric type
- `dim` -- (optional, default: 2) dimension
- `rng` -- (optional, default: `GLOBAL_RNG`) random number generator
- `seed` -- (optional, default: `nothing`) seed for reseeding
### Output
A random ball in the 2-norm.
### Algorithm
All numbers are normally distributed with mean 0 and standard deviation 1.
Additionally, the radius is nonnegative.
"""
function rand(::Type{Ball2};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing)
rng = reseed(rng, seed)
center = randn(rng, N, dim)
radius = abs(randn(rng, N))
return Ball2(center, radius)
end
"""
translate(B::Ball2, v::AbstractVector)
Translate (i.e., shift) a ball in the 2-norm by the given vector.
### Input
- `B` -- ball in the 2-norm
- `v` -- translation vector
### Output
A translated ball in the 2-norm.
### Notes
See also [`translate!(::Ball2, ::AbstractVector)`](@ref) for the in-place
version.
"""
function translate(B::Ball2, v::AbstractVector)
return translate!(copy(B), v)
end
"""
translate!(B::Ball2, v::AbstractVector)
Translate (i.e., shift) a ball in the 2-norm by the given vector, in-place.
### Input
- `B` -- ball in the 2-norm
- `v` -- translation vector
### Output
The ball `B` translated by `v`.
### Algorithm
We add the vector to the center of the ball.
### Notes
See also [`translate(::Ball2, ::AbstractVector)`](@ref) for the out-of-place version.
"""
function translate!(B::Ball2, v::AbstractVector)
@assert length(v) == dim(B) "cannot translate a $(dim(B))-dimensional " *
"set by a $(length(v))-dimensional vector"
c = B.center
c .+= v
return B
end
"""
sample(B::Ball2{N}, [nsamples]::Int;
[rng]::AbstractRNG=GLOBAL_RNG,
[seed]::Union{Int, Nothing}=nothing) where {N}
Return samples from a uniform distribution on the given ball in the 2-norm.
### Input
- `B` -- ball in the 2-norm
- `nsamples` -- number of random samples
- `rng` -- (optional, default: `GLOBAL_RNG`) random number generator
- `seed` -- (optional, default: `nothing`) seed for reseeding
### Output
A linear array of `nsamples` elements drawn from a uniform distribution in `B`.
### Algorithm
Random sampling with uniform distribution in `B` is computed using Muller's method
of normalized Gaussians. This function requires the package `Distributions`.
See `_sample_unit_nball_muller!` for implementation details.
"""
function sample(B::Ball2{N}, nsamples::Int;
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing) where {N}
require(@__MODULE__, :Distributions; fun_name="sample")
n = dim(B)
D = Vector{Vector{N}}(undef, nsamples) # preallocate output
_sample_unit_nball_muller!(D, n, nsamples, rng=rng, seed=seed)
# customize for the given ball
r, c = B.radius, B.center
@inbounds for i in 1:nsamples
axpby!(one(N), c, r, D[i])
end
return D
end
# --- Ball2 functions ---
"""
chebyshev_center_radius(B::Ball2; [kwargs]...)
Compute the [Chebyshev center](https://en.wikipedia.org/wiki/Chebyshev_center)
and the corresponding radius of a ball in the 2-norm.
### Input
- `B` -- ball in the 2-norm
- `kwargs` -- further keyword arguments (ignored)
### Output
The pair `(c, r)` where `c` is the Chebyshev center of `B` and `r` is the radius
of the largest ball with center `c` enclosed by `B`.
### Notes
The Chebyshev center of a ball in the 2-norm is just the center of the ball.
"""
function chebyshev_center_radius(B::Ball2; kwargs...)
return B.center, B.radius
end
"""
volume(B::Ball2)
Return the volume of a ball in the 2-norm.
### Input
- `B` -- ball in the 2-norm
### Output
The volume of ``B``.
### Algorithm
This function implements the well-known formula for the volume of an n-dimensional
ball using factorials. For details see the wikipedia article
[Volume of an n-ball](https://en.wikipedia.org/wiki/Volume_of_an_n-ball).
"""
function volume(B::Ball2)
n = dim(B)
k = div(n, 2)
R = B.radius
if iseven(n)
vol = Base.pi^k * R^n / factorial(k)
else
vol = 2 * factorial(k) * (4*Base.pi)^k * R^n / factorial(n)
end
return vol
end
function project(B::Ball2, block::AbstractVector{Int}; kwargs...)
return Ball2(B.center[block], B.radius)
end