Skip to content

Commit

Permalink
forward special speedup, drop copy-paste maintain ability
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyuezhuo committed Aug 21, 2020
1 parent 9c9846b commit 9746499
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/bijectors/corr.jl
Expand Up @@ -10,7 +10,7 @@ scratch.
struct CorrBijector <: Bijector{2} end

function (b::CorrBijector)(x::AbstractMatrix{<:Real})
w = cholesky(x).U #+ zero(x) # convert to dense matrix
w = cholesky(x).U
r = _link_chol_lkj(w)
return r + zero(x) # keep LowerTriangular until here can avoid some computation
# This dense format itself is required by a test, though I can't get the point.
Expand Down Expand Up @@ -56,22 +56,17 @@ end
function _inv_link_chol_lkj(y)
K = LinearAlgebra.checksquare(y)

z = tanh.(y)
w = similar(z)
w = similar(y)

@inbounds for j in 1:K
w[1, j] = 1
for i in j+1:K
w[i, j] = 0
end
for i in 2:j
w[i, j] = w[i-1, j] * sqrt(1 - z[i-1, j]^2)
end
end

@inbounds for j in 2:K
for i in 1:j-1
w[i, j] = w[i, j] * z[i, j]
z = tanh(y[i-1, j])
w[i, j] = w[i-1, j] * sqrt(1 - z^2)
w[i-1, j] *= z
end
end

Expand Down

0 comments on commit 9746499

Please sign in to comment.