sympy icon indicating copy to clipboard operation
sympy copied to clipboard

Inconsistent behaviour when differentiating MatrixSymbols with respect to each other

Open r0uv3n opened this issue 1 year ago • 6 comments

Assume we have

import sympy

n = sympy.symbols("n",positive=True,integer=True)
X=sympy.MatrixSymbol("X",n,1)
Y=sympy.MatrixSymbol("Y",n,1)

Then sympy.diff(X+Y,Y) return I (which is consistent with e.g. the behaviour for normal symbols, where sympy.diff(x+y,y) returns 1), but sympy.diff(X,Y) returns Derivative(X,Y) (for normal symbols, sympy.diff(x,y) returns 0).

It seems sympy assumes a dependency between X and Y if we the expression does not explicitly depend on Y, but not otherwise.

Is this a bug or am I misunderstanding how this should work?

Right now I really just want to get the coefficients of an expression of the form a*X+b*Y (which will always be linear in X and Y), for which I'd like to use .diff(Y) (since .expand().coeff(Y) also doesn't seem to work). Is there some other way to achieve this without running into the issue above?

r0uv3n avatar Feb 28 '24 22:02 r0uv3n