1

I am trying to find the first and second-order partial derivatives of a function of four variables $S$ using pythons symbolic math package sympy.

The issue is that sympy does not automatically see that certain pairs of partial derivatives, when evaluated at the same arguments, are in fact equal, and so cancellations do not occur. For example: $$\frac{\partial S(x,y,u,v)}{\partial x}=\frac{\partial S(u,v,x,y)}{\partial u} \text{ when } x=u \text{ and } y=u.$$ You can see that in each case the functions are differentiated w.r.t their first arguments (x, u respectively) and that afterwards each partial derivative is evaluated at (x,y,x,y).

However, the code below returns False:

sympy.diff(S(x, y, u, v), x).subs({u: x, v: y}) == sympy.diff(S(u, v, x, y), u).subs({u: x, v: y})

Any help is appreciated!

1 Answers1

0

I think you should substitute before differentiating:

sympy.diff(S(x, y, u, v).subs({u: x, v: y}), x) == sympy.diff(S(u, v, x, y).subs({u: x, v: y}), u)

Z1proW
  • 101
  • 2
    Substitution and differentiation are not commutable in general. Take for example $f(x,y)=x+y$. Then $$\left.\frac{\partial f(x,y)}{\partial x}\right|_{y=x} = 1 \neq 2 = \frac{\partial f(x,x)}{\partial x}$$ – G-Shillcock Jul 05 '23 at 11:06