Hey,
I have a question, maybe you can answer me.
Following this Distributions.jl
documentation, when I define a multivariate distribution, I implement the two functions :
function Distributions._rand!(rng::Distributions.AbstractRNG, X::MyDistribution, x::AbstractVector{T}) where {T}
x .= #something
return x
end
function Base.rand(rng::Distributions.AbstractRNG,X::MyDistribution)
x = zeros(length(X)) #allocate
x .= #something, in fact exactly the same something as the previous function.
return x
end
I remember that a while ago both were needed, and now it simply became an habit. But:
- First, I cannot recall why both interfaces would be needed, would you by chance point me to the right place ?
- Then, I define a lot of distributions, and I wander if I cannot just use one method for the second function as:
function Base.rand(rng::Distributions.AbstractRNG,X<:MyAbstractDistributionFamilly)
x = zeros(length(X)) #allocate
Distributions._rand!(rng,X,x)
end
and then only define the Distributions._rand!(..)
version for each subtype.
Edit: This might be related: `Base.rand` but `Random.rand!`?