My question is really easy for experienced users. In my tensor equations I have an unknown tensor Q (symmetric and traceless):

$Q=\begin{pmatrix} n1(x,y) & n2(x,y) \\ n2(x,y) & -n1(x,y) \end{pmatrix}$

In mathematica:

Q = {{n1[x, y, t], n2[x, y, t]}, {n2[x, y, t], -n1[x, y, t]}}

I need to calculate and work with the following tensors:

1) $P1_{\alpha\beta}=(\partial_\alpha Q_{\gamma\epsilon})(\partial_\beta Q_{\gamma\epsilon})=\sum_{\gamma,\epsilon} \left[\frac{\partial Q_{\gamma\epsilon}}{\partial x_\alpha} \frac{\partial Q_{\gamma\epsilon}}{\partial x_\beta}\right]$

2) $P2_{\alpha\beta}=(\partial_\gamma Q_{\gamma\epsilon})(\partial_\epsilon Q_{\alpha\beta})=\sum_{\gamma,\epsilon} \left[\frac{\partial Q_{\gamma\epsilon}}{\partial x_\gamma} \frac{\partial Q_{\alpha\beta}}{\partial x_\epsilon}\right]$

3) $P3_{\alpha\beta}=Q_{\gamma\epsilon}(\partial_{\epsilon\gamma} Q_{\alpha\beta})=\sum_{\gamma,\epsilon} \left[Q_{\gamma\epsilon} \frac{\partial^2 Q_{\alpha\beta}}{\partial x_\gamma \partial x_\epsilon}\right]$

Where Einstein summation rule is used, all indexes are either 1 or 2 (x or y)., $\partial_{\epsilon\gamma}$ is second derivative over $x_\gamma$ and $x_\epsilon$.

I figured how to calculate $P2$,since $P2=(\nabla\cdot Q)\cdot(\nabla Q)$, an inner product of divergence by gradient. In mathematica, since indexes are messed, you have to multiply gradient by divergence:

r = {x, y}
Simplify[D[Q, {r}].Div[Q,r]]

I checked analytically that this result is correct.

I have no idea how to calculate $P1, P3$.

The best answer would be the code, which finds the required tensors by definition, i.e. by direct summations of products of certain entries.

  • 1
    $\begingroup$ In 1), it's ambiguous whether $\partial_{\alpha}$ acts on both factors to the right or just one. Also, the dimensions of your result for $P2$ aren't those of a $2\times 2$ matrix, as the index notation indicates they should be. The conclusion: Einstein notation is a disease. $\endgroup$
    – Jens
    May 6, 2016 at 20:05
  • $\begingroup$ @Jens, corrected. Now everyone should Understand this. Einstein notations are very convenient to use) $\endgroup$ May 6, 2016 at 20:14
  • $\begingroup$ @Jens, I am sure you know how to do it using table and sum commands. I don't have much experience with this stuff $\endgroup$ May 6, 2016 at 20:16
  • $\begingroup$ Yes, I'll post it. $\endgroup$
    – Jens
    May 6, 2016 at 20:16

1 Answer 1


Here is how I would literally translate the definitions:

Q = {{n1[x, y, t], n2[x, y, t]}, {n2[x, y, t], -n1[x, y, t]}};
r = {x, y};

p1[α_, β_] := 
 Sum[D[Q[[γ, ϵ]], r[[α]]] D[ Q[[γ, ϵ]], r[[β]]], {γ, 1, 2}, {ϵ, 1, 2}]

p3[α_, β_] := 
 Sum[Q[[γ, ϵ]] D[ Q[[α, β]], r[[ϵ]], r[[γ]]], {γ, 1, 2}, {ϵ, 1, 2}]

p2[α_, β_] := 
 Sum[D[Q[[γ, ϵ]], r[[γ]]] D[ Q[[α, β]], r[[ϵ]]], {γ, 1, 2}, {ϵ, 1, 2}]

Table[p1[i, j], {i, 2}, {j, 2}]

Table[p2[i, j], {i, 2}, {j, 2}]

Table[p3[i, j], {i, 2}, {j, 2}]
  • $\begingroup$ Nice, thanks a lot, that works! $\endgroup$ May 6, 2016 at 20:23
  • 1
    $\begingroup$ @MikhailGenkin OK - and of course I have nothing against Einstein personally. $\endgroup$
    – Jens
    May 6, 2016 at 20:24
  • $\begingroup$ actually p3[$\alpha, \beta$] is not defined correctly, since D[f[x,y],{x,y}] produce strange result. But after I replaced if with D[D[f[x,y],x],y], I got what I wanted, so it is ok $\endgroup$ May 6, 2016 at 20:41
  • $\begingroup$ @MikhailGenkin Ah yes, I had too many brackets there. Fixed the typos. $\endgroup$
    – Jens
    May 6, 2016 at 20:58

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.