Skip to content

Commit

Permalink
Loop in reverse direction to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
penelopeysm committed Dec 1, 2024
1 parent f7bc09e commit 229ea4c
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/bijectors/corr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,20 +293,20 @@ which is the above implementation.
function _link_chol_lkj(W::AbstractMatrix)
K = LinearAlgebra.checksquare(W)

y = similar(W) # z is also UpperTriangular.
y = similar(W) # W is upper triangular.
# Some zero filling can be avoided. Though diagnoal is still needed to be filled with zero.

@inbounds for j in 1:K
for i in 1:(j - 1)
remainder_norm = norm(W[i:end, j])
z = W[i, j] / remainder_norm
remainder_sq = W[j, j]^2
for i in (j - 1):-1:1
remainder_sq += W[i, j]^2
z = W[i, j] / sqrt(remainder_sq)
y[i, j] = atanh(z)
end
for i in j:K
y[i, j] = 0
end
end

return y
end

Expand All @@ -316,16 +316,18 @@ function _link_chol_lkj_from_upper(W::AbstractMatrix)

y = similar(W, N)

idx = 1
starting_idx = 1
@inbounds for j in 2:K
y[idx] = atanh(W[1, j])
idx += 1
for i in 2:(j - 1)
remainder_norm = norm(W[i:end, j])
z = W[i, j] / remainder_norm
y[starting_idx] = atanh(W[1, j])
starting_idx += 1
remainder_sq = W[j, j]^2
for i in (j - 1):-1:2
idx = starting_idx + i - 2
remainder_sq += W[i, j]^2
z = W[i, j] / sqrt(remainder_sq)
y[idx] = atanh(z)
idx += 1
end
starting_idx += length((j - 1):-1:2)
end

return y
Expand Down

0 comments on commit 229ea4c

Please sign in to comment.