Matrix differentiation with square of trace leads to unpredictable zeros
Author: marc-gitCreated Sep 17, 2026Updated Sep 17, 2026
In the Sympy live shell:
A = MatrixSymbol('A', 3,3)
(trace(Identity(3)-A)**2).diff(A)delivers 0, with no error message.
Yet (trace(Identity(3)-A)**2).expand() delivers the right expansion.
In any case (trace(Identity(3)-A)**2).expand().diff(A) delivers 0.
The leading term (trace(A)**2) will, when explicitly typed, be correctly differentiated.
Source: sympy/sympy