How can I do the following computation more efficiently? We have nested lists and want to multiply element-wise, where the 'element' is the deepest 2 levels treated like normal matrix multiplication. And then we Total
all but the last 2 levels. In reality I need to deal with larger size (like d1=300
but still small d2
) lists many many times. Probably the following is not the optimal way to program it.
d1 = 10; d2 = 2;
mat1 = RandomComplex[1 + I, {d1, d1, d2, d2}];
mat2 = RandomComplex[1 + I, {d1, d1, d2, d2}];
mat3 = ConjugateTranspose[mat1];
data = Table[mat1[[i, j]] . mat2[[i, j]] . mat3[[i, j]], {i, d1}, {j, d1}];
Total[data, 2];
Apply[Dot, Transpose[{mat1, mat2, mat3}, {3, 1, 2}], {2}]
to getdata
but it is easier to see what is being done using your approach. $\endgroup$Table
. Nice. $\endgroup$