JuliaStats / Distributions.jl

A Julia package for probability distributions and associated functions.
Other
1.1k stars 414 forks source link

StackOverflowError with rand! #1822

Closed 3f6a closed 8 months ago

3f6a commented 8 months ago
julia> X = zeros(2, 10)
2×10 Matrix{Float64}:
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0

julia> rand!(X, MultivariateNormal([1.0, 2.0], [1.0 0.0; 0.0 1.0]))
ERROR: StackOverflowError:
Stacktrace:
 [1] rand!(rng::TaskLocalRNG, A::Matrix{Float64}, X::FullNormal) (repeats 2 times)
   @ Random ~/.julia/juliaup/julia-1.10.0+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/Random/src/Random.jl:268
devmotion commented 8 months ago

The arguments have to be provided in different order:

julia> using Distributions, Random

julia> X = zeros(2, 10)
2×10 Matrix{Float64}:
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0

julia> rand!(MultivariateNormal([1.0, 2.0], [1.0 0.0; 0.0 1.0]), X)
2×10 Matrix{Float64}:
 0.337452  0.144253  2.32285  -1.27711  …  -0.530822  0.0721648  -0.624228
 1.39392   2.19907   3.2094    3.96336     -1.20843   1.82541     0.828073

julia> X
2×10 Matrix{Float64}:
 0.337452  0.144253  2.32285  -1.27711  …  -0.530822  0.0721648  -0.624228
 1.39392   2.19907   3.2094    3.96336     -1.20843   1.82541     0.828073
devmotion commented 8 months ago

Ref https://github.com/JuliaStats/Distributions.jl/issues/1316

3f6a commented 8 months ago

Thanks for the response.

I think it's a bit confusing, but please feel free to close this.