1
$\begingroup$

How can I write this tensor in Mathematica?

$$\mathcal{P}_{ijkl}(N) = \Big( \delta_{ik} - N_iN_k \Big) \Big( \delta_{jl} - N_jN_l \Big) - \dfrac{1}{2} \Big( \delta_{ij} - N_iN_j \Big)\Big( \delta_{kl} - N_kN_l \Big)$$

I have tried with TensorProduct, but I realized that it is not so simple because of the position of the indices.

EDIT To clarify: $\delta_{ij}$ is the Kroeneker delta and $$N=\{\cos\phi \sin\theta, \sin\phi\sin\theta,\cos\theta\}$$

$\endgroup$
2
  • $\begingroup$ Are you looking for a numerical object, given a numerical n? Or something symbolic? $\endgroup$
    – evanb
    Commented Jun 7, 2021 at 8:42
  • $\begingroup$ @evanb I am looking for a symbolic tensor. $\endgroup$
    – mattiav27
    Commented Jun 7, 2021 at 8:54

1 Answer 1

2
$\begingroup$

The simplest way would be to use Table, e.g. something like

tensorTable[n_] := With[{dim = Length[n]},
  Table[
   (KroneckerDelta[i, k] - n[[i]] n[[k]]) (KroneckerDelta[j, l] - 
       n[[j]] n[[l]]) -
    1/2 (KroneckerDelta[i, j] - n[[i]] n[[j]]) (KroneckerDelta[k, l] -
        n[[k]] n[[l]]),
   {i, dim}, {j, dim}, {k, dim}, {l, dim}]]

For the vector given by OP:

exampleTensor = Simplify[tensorTable[{Cos[ϕ] Sin[θ],Sin[ϕ] Sin[θ], Cos[θ]}]];

I suppose you could also write this in more convenient(?) mathematical notation using SparseArray, by noticing the two terms are identical up to a permutation of $j\leftrightarrow k$

commonTerm[n_] := With[{dim = Length[n]},
  Plus @@ Map[SparseArray[#, {dim, dim, dim, dim}] &,
    {
     {i_, j_, k_, l_} :> n[[i]] n[[j]] n[[k]] n[[l]],
     {i_, j_, i_, l_} :> -n[[j]] n[[l]],
     {i_, j_, k_, j_} :> -n[[i]] n[[k]],
     {i_, j_, i_, j_} :> 1
     }
    ]
  ]

This will also store the tensor as a SparseArray which may be beneficial for further analysis.

exampleTensor2 = 
  Simplify[commonTerm[{Cos[ϕ] Sin[θ],Sin[ϕ] Sin[θ], Cos[θ]}] - 
    1/2 Transpose[commonTerm[{Cos[ϕ] Sin[θ], Sin[ϕ] Sin[θ], Cos[θ]}], 
      Cycles[{{2, 3}}]]];

exampleTensor == exampleTensor2
(*True*)
$\endgroup$
2
  • $\begingroup$ is it possible that we can convert these "With" functions to c code ? $\endgroup$
    – ABCDEMMM
    Commented Jun 7, 2021 at 19:18
  • $\begingroup$ Not sure I understand the question, but perhaps this compiled version helps? ``` kr = Compile[{{i, _Integer}, {j, _Integer}}, If[i == j, 1, 0]]; cf = Compile[{{n, _Real, 1}}, Module[{d = Length[n]}, Table[(kr[i, k] - n[[i]] n[[k]]) (kr[j, l] - n[[j]] n[[l]]) - 1/2 (kr[i, j] - n[[i]] n[[j]]) (kr[k, l] - n[[k]] n[[l]]), {i, d}, {j, d}, {k, d}, {l, d}]], CompilationOptions -> {"InlineExternalDefinitions" -> True}] ``` $\endgroup$ Commented Jun 8, 2021 at 14:12

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.