I need to calculate something of the form:
where are dense matrices, are diagonal, is the identity matrix and
is an arbitrary scalar function.
The following code creates all the variations of Kronecker products that I need (transpose, Hermitian transpose and complex conjugation). It also allows me to update the products when I update .
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[1].arrays[2] .= 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.
Any suggestions?