# How to perform delta and sum functions in KroneckerProduct in Mathemtica?

I am trying to simulate a tensors which includes sum and kroneckerdelta functions, Sum[KroneckerDelta[b, d] gamma1[a, j] gamma2[j, c], {j, 1, n}]. So I have used Table to create this as shown in following example:

n = 2
DHs = {{0.273896 + 0. I, 0.0967486 - 0.0561944 I}, {0.0967486 + 0.0561944 I,  0.0110102 + 0. I}}

U = {{-0.0654387 + 0.0871213 I, 0.596998 - 0.794809 I}, {0.994046 + 0. I, 0.10896 + 0. I}}

Ut = {{-0.0654387 - 0.0871213 I, 0.994046 + 0. I}, {0.596998 + 0.794809 I, 0.10896 + 0. I}}

gamma1[a_, b_] := Indexed[(Ut.DHs.U), {a, b}]

gamma2[c_, d_] := Indexed[(Ut.DHs.U), {c, d}]

Table[Sum[
KroneckerDelta[b, d] gamma1[a, j] gamma2[j, c], {j, 1, n}], {a, 1,
n}, {b, 1, n}, {c, 1, n}, {d, 1, n}] // MatrixForm



I would like to perform same operation using KroneckerProduct, as in actual problem the dimensions of the my matrices are very huge and I have to calculate many tensors and Table function is taking long time. So far, I have tried:

KroneckerProduct[Ut.DHs.U, Ut.DHs.U]


But, I am unable to use KroneckerDelta[b, d] & Sum[j,1,n] on top of KroneckerProduct.

Any help will be highly appreciated.

B = Ut.H.U;
result = Transpose[
ArrayReshape[
KroneckerProduct[IdentityMatrix[n, SparseArray], B.B], {n, n, n, n}],
{2, 1, 4, 3}
];

• thankyou very much. Can you please explain little about {2,1,4,3} ? Rest I understand the math.
– Aman
Jan 29, 2022 at 19:26
• Well, the sparse 4-tensor generated by ArrayReshape has the correct entries but the slots are in a wrong order. Transpose[...,{2, 1, 4, 3}] this. Why {2, 1, 4, 3} specifically? Well, I knew there must be a suitable transposition. So I tries just all of them and picked one that worked. ;) Jan 29, 2022 at 19:40
• Thank you very much, I think I got it.
– Aman
Jan 29, 2022 at 19:45
• Glad to hear that! You're welcome. Jan 29, 2022 at 19:45