Skip to content

Commit

Permalink
faster sampling from singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed May 29, 2022
1 parent 945c380 commit a0555c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Utils/samples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ _default_sampler(::LineSegment{N}) where {N} =
_default_sampler(::HalfSpace) = HalfSpaceSampler()
_default_sampler(::Hyperplane) = HyperplaneSampler()
_default_sampler(::Line2D) = HyperplaneSampler()
_default_sampler(::AbstractSingleton) = SingletonSampler()

# =====================
# Uniform distribution
Expand Down Expand Up @@ -530,6 +531,28 @@ function sample!(D::Vector{VN}, hp::Union{Hyperplane, Line2D},
end
end

# ==================
# Singleton sampler
# ==================

"""
SingletonSampler <: AbstractSampler
Type used for sampling from a singleton.
"""
struct SingletonSampler <: AbstractSampler
end

function sample!(D::Vector{VN}, S::AbstractSingleton,
sampler::SingletonSampler;
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing) where {N, VN<:AbstractVector{N}}
x = element(S)
@inbounds for i in 1:length(D)
D[i] = copy(x)
end
end

# =============================
# Code requiring Distributions
# =============================
Expand Down
3 changes: 3 additions & 0 deletions test/Sets/Singleton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ for N in [Float64, Rational{Int}, Float32]
answer, w = isuniversal(s, true)
@test !isuniversal(s) && !answer && w s

# sampling
@test sample(s, 2) == [element(s), element(s)]

# membership
S = Singleton(N[1, 1])
@test N[0.9, 1.1] S
Expand Down

0 comments on commit a0555c3

Please sign in to comment.