Skip to content

Commit

Permalink
#2116 - Add type parameter for Ballp (#2131)
Browse files Browse the repository at this point in the history
* add type param for Ballp

* fix doctest
  • Loading branch information
mforets authored May 12, 2020
1 parent f5f7650 commit 4f9205d
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/Sets/Ballp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Base: rand,
export Ballp

"""
Ballp{N<:AbstractFloat} <: AbstractCentrallySymmetric{N}
Ballp{N<:AbstractFloat, VN<:AbstractVector{N}} <: AbstractCentrallySymmetric{N}
Type that represents a ball in the p-norm, for ``1 ≤ p ≤ ∞``.
Expand Down Expand Up @@ -36,7 +36,8 @@ A five-dimensional ball in the ``p=3/2`` norm centered at the origin of radius
```jldoctest ballp_constructor
julia> B = Ballp(3/2, zeros(5), 0.5)
Ballp{Float64}(1.5, [0.0, 0.0, 0.0, 0.0, 0.0], 0.5)
Ballp{Float64,Array{Float64,1}}(1.5, [0.0, 0.0, 0.0, 0.0, 0.0], 0.5)
julia> dim(B)
5
```
Expand All @@ -53,13 +54,13 @@ julia> σ([1., 2, 3, 4, 5], B)
0.33790011086518895
```
"""
struct Ballp{N<:AbstractFloat} <: AbstractCentrallySymmetric{N}
struct Ballp{N<:AbstractFloat, VN<:AbstractVector{N}} <: AbstractCentrallySymmetric{N}
p::N
center::Vector{N}
center::VN
radius::N

# default constructor with domain constraint for radius and p
function Ballp(p::N, center::Vector{N}, radius::N) where {N<:Real}
function Ballp(p::N, center::VN, radius::N) where {N<:Real, VN<:AbstractVector{N}}
@assert radius >= zero(N) "radius must not be negative"
@assert p >= 1 "p must not be less than 1"
if p == Inf
Expand All @@ -69,7 +70,7 @@ struct Ballp{N<:AbstractFloat} <: AbstractCentrallySymmetric{N}
elseif p == 1
return Ball1(center, radius)
else
return new{N}(p, center, radius)
return new{N, VN}(p, center, radius)
end
end
end
Expand Down Expand Up @@ -180,9 +181,11 @@ Then ``x ∈ B`` iff ``\\left( ∑_{i=1}^n |c_i - x_i|^p \\right)^{1/p} ≤ r``.
```jldoctest
julia> B = Ballp(1.5, [1., 1.], 1.)
Ballp{Float64}(1.5, [1.0, 1.0], 1.0)
Ballp{Float64,Array{Float64,1}}(1.5, [1.0, 1.0], 1.0)
julia> [.5, -.5] ∈ B
false
julia> [.5, 1.5] ∈ B
true
```
Expand Down

0 comments on commit 4f9205d

Please sign in to comment.