3
$\begingroup$

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.

$\endgroup$

1 Answer 1

5
$\begingroup$
B = Ut.H.U;
result = Transpose[
   ArrayReshape[
    KroneckerProduct[IdentityMatrix[n, SparseArray], B.B], {n, n, n, n}],
   {2, 1, 4, 3}
   ];
$\endgroup$
4
  • 1
    $\begingroup$ thankyou very much. Can you please explain little about {2,1,4,3} ? Rest I understand the math. $\endgroup$
    – Aman
    Jan 29, 2022 at 19:26
  • 1
    $\begingroup$ 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. ;) $\endgroup$ Jan 29, 2022 at 19:40
  • 1
    $\begingroup$ Thank you very much, I think I got it. $\endgroup$
    – Aman
    Jan 29, 2022 at 19:45
  • $\begingroup$ Glad to hear that! You're welcome. $\endgroup$ Jan 29, 2022 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.