using LazyArrays, BenchmarkTools function kroneckerproducts(A) I = oneunit(A) # (I ⊗ A) and its transpose, Hermitian transpose and complex conjugate. IA = Kron(I,A) IAT, IAH, IAC = [transpose(IA), IA', transpose(IA')] # (A ⊗ I)... AI = Kron(A,I) ATI, AHI, ACI = [transpose(AI), AI', transpose(AI')] return [IA, AI, IAT, ATI, IAH, AHI, IAC, ACI] end function updatekroneckerproducts!(Karray,Klazy,B) # Should be enough to update (I ⊗ A) -> (I ⊗ B) Klazy.arrays .= B copyto!.(Karray,Klazy) end N = 8 A = rand(ComplexF64,N,N) K = kroneckerproducts(A) Karr = Array.(K) Anew = rand(ComplexF64,N,N) @btime updatekroneckerproducts!($Karr,$K,$Anew) # 3.066 ms (8 allocations: 320 bytes)
These allocations don’t really seem to affect performance, but I am wondering if I can get rid of them when performing the sum.