Sample StaticVectors with Distributions.jl

Joaquin_Rodriguez
Thu, 12 Dec 2024 23:04:44 +0000
General Usage

I need to generate some random vectors with a custom distribution, and I would like to use the Distributions.jl API. However, my vectors are small StaticVectors and sampling from a distribution returns plain Vectors.

As shown by @Elrod on Creating random Static Vector by StaticVector.jl and Distributions.jl in a loop, you can define a distribution using static vectors, but you still get dynamic vectors when sampling:

julia> using Distributions, StaticArrays
julia> dist2dN = MvNormal(@SVector([0., 0.]),10.0)
MvNormal{Float64, PDMats.ScalMat{Float64}, SVector{2, Float64}}(
dim: 2
μ: [0.0, 0.0]
Σ: [100.0 0.0; 0.0 100.0]
)
julia> rand(dist2dN)
2-element Vector{Float64}:
 -15.693466638813794
 -10.710701041361501

Is there a way to make rand(my_sampler) return an SVector or an MVector?

3 posts - 2 participants