From 069cda9788032197dfab6ce01e18268d7cd92f53 Mon Sep 17 00:00:00 2001 From: David Hong Date: Thu, 29 Feb 2024 23:49:49 -0500 Subject: [PATCH] Avoid making two copies of `K` 1. use `reduce` to compute all but the last `kron` 2. use `kron!` for the last one --- src/gcp-opt.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/gcp-opt.jl b/src/gcp-opt.jl index 29c62cb..1995745 100644 --- a/src/gcp-opt.jl +++ b/src/gcp-opt.jl @@ -252,7 +252,8 @@ function khatrirao(A::Vararg{T,N}) where {T<:AbstractMatrix,N} r = (only ∘ unique)(size.(A, 2)) K = similar(A[1], prod(size.(A, 1)), r) for j in 1:r - K[:, j] = reduce(kron, [view(A[i], :, j) for i in 1:N]) + temp = reduce(kron, [view(A[i], :, j) for i in 1:N-1]) + kron!(view(K, :, j), temp, view(A[N], :, j)) end return K end