# How to get the derivatives of the determinant and inverse of 2nd-order tensor wrt itself in SymPy?

I have a second-order tensor for which I need to compute the derivatives of its determinant and inverse w.r.t. itself. The equations are as follows:

$$\frac{\partial \, det(\mathbf{F})}{\partial F_{ij}} = det(\mathbf{F}) \, F^{-1}_{ji}$$

$$\frac{\partial \, F_{kl}^{-1}}{\partial F_{ij}} = - F^{-1}_{ki} \, F^{-1}_{jl}$$

These are the equations (49) and (60) in the matrix cookbook.

I have been working with SymPy for about a week. I can get the derivative of F wrt itself but can't figure out how to get the derivatives of its determinant and the inverse. For reference, I am posting the code and the output along with the error message.

Python code

from sympy import *

i = tensor.Idx('i',3)
j = tensor.Idx('j',3)
k = tensor.Idx('k',3)
l = tensor.Idx('l',3)

F = MatrixSymbol('F', 3, 3)

print("Derivative of F wrt F")
print("---------------------")
print(diff(F[k,l], F[i,j]))
print("\n")

J = det(F)
print("Derivative of det(F) wrt F")
print("--------------------------")
print(diff(J, F[i,j]))
print("\n")

print("Derivative of inv(F) wrt F")
print("--------------------------")
Finv = Inverse(F)
print(Finv[i,j])
print(diff(Finv[k,l], F[i,j]))
print("\n")


Output

Derivative of F wrt F
---------------------------
KroneckerDelta(i, k)*KroneckerDelta(j, l)

Derivative of det(F) wrt F
-------------------------------
Derivative(Determinant(F), F[i, j])

Derivative of inv(F) wrt F
-------------------------------
Traceback (most recent call last):
File "matdiffinverse.py", line 25, in <module>
print(Finv[i,j])
File "/usr/lib/python3/dist-   packages/sympy/matrices/expressions/matexpr.py", line 248, in __getitem__
return self._entry(i, j)
File "/usr/lib/python3/dist-   packages/sympy/matrices/expressions/matpow.py", line 46, in _entry
raise NotImplementedError(("(%d, %d) entry" % (int(i), int(j)))
File "/usr/lib/python3/dist-packages/sympy/core/expr.py", line 207, in __int__
raise TypeError("can't convert symbols to int")
TypeError: can't convert symbols to int

• You would get more attention in StackOverflow. – nicoguaro Jul 1 '19 at 14:31