Skip to content

Commit

Permalink
implement wrapping implicit distributions in an explicit Uniform one
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Dec 5, 2017
1 parent 433c879 commit df2f4e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base/random/generation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

## from types: rand(::Type, [dims...])

Sampler(rng::AbstractRNG, d::Union{UniformWrap,UniformType}, n::Repetition) =
Sampler(rng, d[], n)

### random floats

Sampler(rng::AbstractRNG, ::Type{T}, n::Repetition) where {T<:AbstractFloat} =
Expand Down
14 changes: 14 additions & 0 deletions base/random/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ _deduce_type(::Type{T}, ::Val{false}, ::Type{X}) where {T,X} = T{X}

abstract type Uniform{T} <: Distribution{T} end

struct UniformType{T} <: Uniform{T} end

Uniform(::Type{T}) where {T} = UniformType{T}()

Base.getindex(::UniformType{T}) where {T} = T

struct UniformWrap{T,E} <: Uniform{E}
val::T
end

Uniform(x::T) where {T} = UniformWrap{T,eltype(T)}(x)

Base.getindex(x::UniformWrap) = x.val

#### Normal & Exponential

abstract type Normal{T} <: Distribution{T} end
Expand Down

0 comments on commit df2f4e4

Please sign in to comment.