Skip to content

Commit

Permalink
Use loop instead of sum for getindex
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmul1114 committed Mar 8, 2024
1 parent 631ce99 commit 7d17561
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/cpd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ end

function getindex(M::CPD{T,N}, I::Vararg{Int,N}) where {T,N}
@boundscheck Base.checkbounds_indices(Bool, axes(M), I) || Base.throw_boundserror(M, I)
return sum(
M.λ[j] * prod(M.U[k][I[k], j] for k in Base.OneTo(ndims(M))) for
j in Base.OneTo(ncomponents(M));
init = zero(eltype(T)),
)
val = zero(eltype(T))
for j in Base.OneTo(ncomponents(M))
val += M.λ[j] * prod(M.U[k][I[k], j] for k in Base.OneTo(ndims(M)))
end
return val
end
getindex(M::CPD{T,N}, I::CartesianIndex{N}) where {T,N} = getindex(M, Tuple(I)...)

Expand Down

0 comments on commit 7d17561

Please sign in to comment.