Euclidean-norm ball (Ball2)

LazySets.Ball2Module.Ball2Type
Ball2{N<:AbstractFloat, VN<:AbstractVector{N}} <: AbstractBallp{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

\[\mathcal{B}_2^n(c, r) = \{ x ∈ ℝ^n : ‖ x - c ‖_2 ≤ r \},\]

where $c ∈ ℝ^n$ is its center and $r ∈ ℝ_+$ its radius. Here $‖ ⋅ ‖_2$ denotes the Euclidean norm (also known as 2-norm), defined as $‖ x ‖_2 = \left( ∑\limits_{i=1}^n |x_i|^2 \right)^{1/2}$ for any $x ∈ ℝ^n$.

Examples

Create a five-dimensional ball B in the 2-norm centered at the origin with radius 0.5:

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]$:

julia> σ([1.0, 2, 3, 4, 5], B)
5-element Vector{Float64}:
 0.06741998624632421
 0.13483997249264842
 0.20225995873897262
 0.26967994498529685
 0.3370999312316211
source

Operations

LazySets.API.centerMethod
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.

source
LazySets.chebyshev_center_radiusMethod
chebyshev_center_radius(B::Ball2; [kwargs]...)

Compute the 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.

source
Base.randMethod
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.

source
LazySets.API.reflectMethod
reflect(B::Ball2)

Concrete reflection of a ball in the 2-norm B, resulting in the reflected set -B.

Input

  • B – ball in the 2-norm

Output

The Ball2 representing -B.

Algorithm

If $B$ has center $c$ and radius $r$, then $-B$ has center $-c$ and radius $r$.

source
LazySets.API.volumeMethod
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.

source
Base.:∈Method
∈(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

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
source
LazySets.API.sampleMethod
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.

source
LazySets.API.ρMethod
ρ(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.

Algorithm

Let $c$ and $r$ be the center and radius of the ball $B$ in the 2-norm, respectively. Then:

\[ρ(d, B) = ⟨d, c⟩ + r ‖d‖_2.\]

source
LazySets.API.σMethod
σ(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

\[σ(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.

source
LazySets.API.translate!Method
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) for the out-of-place version.

source
Base.isdisjointFunction
isdisjoint(B1::Ball2, B2::Ball2, [witness]::Bool=false)

Check whether two balls in the 2-norm do not intersect, and otherwise optionally compute a witness.

Input

  • B1 – ball in the 2-norm
  • B2 – ball in the 2-norm
  • witness – (optional, default: false) compute a witness if activated

Output

  • If witness option is deactivated: true iff $B1 ∩ B2 = ∅$
  • If witness option is activated:
    • (true, []) iff $B1 ∩ B2 = ∅$
    • (false, v) iff $B1 ∩ B2 ≠ ∅$ and $v ∈ B1 ∩ B2$

Algorithm

$B1 ∩ B2 = ∅$ iff $‖ c_2 - c_1 ‖_2 > r_1 + r_2$.

A witness is computed depending on the smaller/bigger ball (to break ties, choose B1 for the smaller ball) as follows.

  • If the smaller ball's center is contained in the bigger ball, we return it.
  • Otherwise start in the smaller ball's center and move toward the other center until hitting the smaller ball's border. In other words, the witness is the point in the smaller ball that is closest to the center of the bigger ball.
source
Base.:⊆Function
⊆(B1::Ball2, B2::Ball2, [witness]::Bool=false)

Check whether a ball in the 2-norm is contained in another ball in the 2-norm, and if not, optionally compute a witness.

Input

  • B1 – inner ball in the 2-norm
  • B2 – outer ball in the 2-norm
  • witness – (optional, default: false) compute a witness if activated

Output

  • If witness option is deactivated: true iff $B1 ⊆ B2$
  • If witness option is activated:
    • (true, []) iff $B1 ⊆ B2$
    • (false, v) iff $B1 ⊈ B2$ and $v ∈ B1 ∖ B2$

Algorithm

$B1 ⊆ B2$ iff $‖ c_1 - c_2 ‖_2 + r_1 ≤ r_2$

source

Undocumented implementations:

Inherited from LazySet:

Inherited from AbstractCentrallySymmetric:

Inherited from AbstractBallp:

LazySets.Ball2Module._sample_unit_nball_muller!Function
_sample_unit_nball_muller!(D::Vector{Vector{N}}, n::Int, p::Int;
                           [rng]::AbstractRNG=GLOBAL_RNG,
                           [seed]::Union{Int, Nothing}=nothing) where {N}

Draw samples from a uniform distribution on an $n$-dimensional unit ball using Muller's method.

Input

  • D – output, vector of points
  • n – dimension of the ball
  • p – number of random samples
  • rng – (optional, default: GLOBAL_RNG) random number generator
  • seed – (optional, default: nothing) seed for reseeding

Output

The modified vector D.

Algorithm

This function implements Muller's method of normalized Gaussians [1] to uniformly sample from the interior of the unit ball.

Given $n$ Gaussian random variables $Z₁, Z₂, …, Z_n$ and a uniformly distributed random variable $r$ with support in $[0, 1]$, the distribution of the vectors

\[\dfrac{r^{1/n}}{α} \left(z₁, z₂, …, z_n\right)^T,\]

where $α := \sqrt{z₁² + z₂² + … + z_n²}$, is uniform over the $n$-dimensional unit ball.

[1] Muller, Mervin E. A note on a method for generating points uniformly on n-dimensional spheres. Communications of the ACM 2.4 (1959): 19-20.

source