# Creating a rank 4 tensor

I am struggling with creating the rank-4 matrix T in Mathematica. The matric is defined as

$$T_{i,j,k,l}= n_i n_j n_k n_l-(\delta_{i,j} n_k n_l +\delta_{i,k} n_j n_l+\delta_{i,l} n_k n_j+\delta_{j,l} n_i n_k+\delta_{j,k} n_i n_l+\delta_{k,l} n_i n_j)/6$$,

Where $$n$$ is a vector with two components: $$n=(a,b)$$.

I used TensorProduct to create the matrix, but I am not sure if that is correct, and also I do not know how to add the second part with delta functions. Could someone please help me?

Here is what I did:

n = {a, b};
mat = TensorProduct[n, n, n, n]
mat // MatrixForm


You can use the second syntax of Transpose and it should work like the code below, but it is yet to be checked whether each 4-length List as the second argument of Transpose exactly corresponds to your order of indexes or not

n = {a, b};
id4 = TensorProduct[IdentityMatrix[2], n, n];
mat = TensorProduct[n, n, n, n] - (id4 + Transpose[id4, {1, 3, 2, 4}] +
Transpose[id4, {1, 4, 3, 2}] + Transpose[id4, {3, 1, 4, 2}] +
Transpose[id4, {3, 1, 2, 4}] + Transpose[id4, {3, 4, 1, 2}])/6


Update

As @WReach mentioned in the comment, two groups of indices above should indeed be corrected as below:

{3, 1, 4, 2} -> {2, 4, 1, 3}
{3, 1, 2, 4} -> {2, 3, 1, 4}


Though, the spirit of using Transpose is uninfluenced all the way.

• +1. I believe that the index sets 3142 and 3124 should be 2413 (jlik) and 2314 (jkil). At least, that matches what I got using direct construction: kd[i_,j_,k_,l_] := KroneckerDelta[i,j]n[[k]]n[[l]]; Table[n[[i]]n[[j]]n[[k]]n[[l]] - (kd[i,j,k,l]+kd[i,k,j,l]+kd[i,l,k,j]+kd[j,l,i,k]+kd[j,k,i,l]+kd[k,l,i,j])/6 , {i, 2}, {j, 2}, {k, 2}, {l, 2}] Mar 2 at 16:47
• @WReach Thx very much! After a double-check, I think your order of index is correct. BTW, I believe that your Table-Part combo deserves an individual answer rather than just a comment. Mar 3 at 9:35