Skip to content

Commit

Permalink
Reduce allocations in matrixdirichlet rand!
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterwln committed Sep 27, 2024
1 parent 5740b52 commit 335551b
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/distributions/matrix_dirichlet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,9 @@ function BayesBase.rand(rng::AbstractRNG, dist::MatrixDirichlet{T}, nsamples::In
end

function BayesBase.rand!(rng::AbstractRNG, dist::MatrixDirichlet, container::AbstractMatrix{T}) where {T <: Real}
samples = vmap(d -> rand(rng, Dirichlet(convert(Vector, d))), eachcol(dist.a))
@views for row in 1:size(container, 2)
b = container[:, row]
b[:] .= samples[row]
@views for (i, col) in enumerate(eachcol(dist.a))
rand!(rng, Dirichlet(col), container[:, i])
end

return container
end

Expand Down

0 comments on commit 335551b

Please sign in to comment.