I am doing some work in elasticity and as a result am working with tensors. In particular, I would like to calculate the contraction of a fourth order tensor (the stiffness tensor) with a second order tensor (the velocity gradient) to get another tensor (the time derivative of the stress, for anyone interested).

For my material, the stiffness tensor is easily written down in terms of products of Kronecker Delta Functions such as $\delta_{ij}\delta_{kl}$. I can then easily compute the contraction by manually entering the second order tensor of interest, and using Sum in combination with Part to extract the result.

The problem is that, naturally, Mathematica displays the output in terms of KroneckerDelta[3, i], for example, and there's a lot of them - it's essentially writing the matrix out elementwise using delta functions. Is there a way I can easily view this as a matrix, or map them to a matrix, or alternatively, approach this problem from a different perspective which would avoid this issue altogether? I am trying to apply something along the lines of the following to my expression:

Replace[KroneckerDelta[i, j], KroneckerDelta[i, j] -> TensorProduct[Part[IdentityMatrix[3], i], Part[IdentityMatrix[3], j]]]

If this were valid Mathematica code, it would work in theory, but I get the error The expression i cannot be used as a part specification. along with the same for j.

  • $\begingroup$ The tensors you speak of do not have definite dimensions? $\endgroup$ Jul 19, 2016 at 17:11
  • $\begingroup$ They do, what do you mean by that? $\endgroup$ Jul 19, 2016 at 17:29
  • $\begingroup$ Do you have definite tensors like SparseArray[{k_, k_, k_} -> 1, {3, 3, 3}] or are you just manipulating symbolic tensors? $\endgroup$ Jul 19, 2016 at 17:30
  • $\begingroup$ I'm not sure I know the difference. I have the definite tensors analytically, but I have input the tensors basically as a function defined using KroneckerDelta as, for example, stiff[i_, j_, k_, l_] = L KroneckerDelta[i, j] KroneckerDelta[k, l] + \[Mu] (KroneckerDelta[i, k] KroneckerDelta[j, l] + KroneckerDelta[i, l] KroneckerDelta[j, k]) $\endgroup$ Jul 19, 2016 at 17:49
  • 2
    $\begingroup$ Try SparseArray[{{i_, j_, k_, l_} :> L KroneckerDelta[i, j] KroneckerDelta[k, l] + μ (KroneckerDelta[i, k] KroneckerDelta[j, l] + KroneckerDelta[i, l] KroneckerDelta[j, k])}, {3, 3, 3, 3}]. Consider reading the docs for SparseArray[] and SymmetrizedArray[] too, while you're at it. $\endgroup$ Jul 19, 2016 at 19:19

1 Answer 1


As far as I understood from your answers to comments you like KronekerDelta in the stiffness tensor, but not in the stress tensor. You can do as follows:

stiff[i_, j_, k_, l_] := 
 L KroneckerDelta[i, j] KroneckerDelta[k, 
    l] + \[Mu] (KroneckerDelta[i, k] KroneckerDelta[j, l] + 
     KroneckerDelta[i, l] KroneckerDelta[j, k])
vg = Array[v, {3, 3}];
vg // MatrixForm
stress[i_, j_] := Sum[stiff[i, j, k, l] vg[[k, l]], {k, 3}, {l, 3}]
stressT = Table[stress[i, j], {i, 3}, {j, 3}] // Simplify;
stressT // MatrixForm

leading to

$\begin{pmatrix} v(1,1) & v(1,2) & v(1,3) \\ v(2,1) & v(2,2) & v(2,3) \\ v(3,1) & v(3,2) & v(3,3) \\ \end{pmatrix}$


$\left(\begin{smallmatrix} 2 \mu v(1,1)+L (v(1,1)+v(2,2)+v(3,3)) & \mu (v(1,2)+v(2,1)) & \mu (v(1,3)+v(3,1)) \\ \mu (v(1,2)+v(2,1)) & 2 \mu v(2,2)+L (v(1,1)+v(2,2)+v(3,3)) & \mu (v(2,3)+v(3,2)) \\ \mu (v(1,3)+v(3,1)) & \mu (v(2,3)+v(3,2)) & 2 \mu v(3,3)+L (v(1,1)+v(2,2)+v(3,3)) \\ \end{smallmatrix}\right)$

  • $\begingroup$ @J.M. thanks for editing that long equation $\endgroup$
    – yarchik
    Jul 20, 2016 at 17:31
  • $\begingroup$ No worries; it certainly was starting to look garbled at the right... $\endgroup$ Jul 20, 2016 at 17:43
  • $\begingroup$ @J.M.needshelp. v, {i, j} = v, {j, i}, how can we modify this boundary conditions? $\endgroup$
    Jul 19, 2018 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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