Search code examples
pythonsympysymbolic-math

Why does Sympy's diff function return a 4D array as an answer?



import sympy as sym
from sympy import symbols

x1,x2,x3 = symbols('x1,x2,x3')
X = sym.Matrix([[x1],[x2],[x3]])
print(X)
A = sym.Matrix([[1,0,1], [0,1,1],[1,1,0]])
print(A)
print(X.T*A*X)
D = sym.diff(X.T*A*X , X)
print(D.shape)
print(D)
print(D[0])
print(D[0][0])
print(D[0][0][0])
print(D[0][0][0][0])

The output of the above is as below:

Matrix([[x1], [x2], [x3]])
Matrix([[1, 0, 1], [0, 1, 1], [1, 1, 0]])
Matrix([[x1*(x1 + x3) + x2*(x2 + x3) + x3*(x1 + x2)]])
(3, 1, 1, 1)
[[[[2*x1 + 2*x3]]], [[[2*x2 + 2*x3]]], [[[2*x1 + 2*x2]]]]
[[[2*x1 + 2*x3]]]
[[2*x1 + 2*x3]]
[2*x1 + 2*x3]
2*x1 + 2*x3

So this means D is a 4D array. Can someone please tell why sympy.diff returns a 4D array ? (it should be a 2D array of shape (3,1))

It should be a 2D array of shape (3,1).

So I was expecting an ans:

 [[2*x1 + 2*x3], [2*x2 + 2*x3], [2*x1 + 2*x2]]

Instead I got:

 [[[[2*x1 + 2*x3]]], [[[2*x2 + 2*x3]]], [[[2*x1 + 2*x2]]]]

Solution

  • If you check, (X.T*A*X).shape is (1,1) when this should really be a scalar. This is messing up the diff operations. Extracting that single result fixes the issue, i.e.

    D = sym.diff((X.T*A*X)[0], X)