I've been doing derivatives in sympy, and I didn't know how that would syntactically be written. I tried looking it up, but none of the solutions made sense. For example, if I'm trying to differentiate x**5 + y**2 + z**4 = 8xyz
by computation, how would I do that? Would z be a symbol, or a function like in regular derivatives? Thank you.
Asked
Active
Viewed 3,845 times
5
-
1Are you doing derivatives or do you try to integrate? You question is not clear about that. Then you should also specify which derivative you want, with respect to which varibale or how you want to integrate the expression, what your integration interval is. – Cleb Feb 18 '16 at 11:52
-
Oh, sorry for that typo there. I'm trying to differentiate with respect to z. – MANA624 Feb 18 '16 at 15:33
2 Answers
9
For two variables you can use idiff
.
In your case, the simplest way is to set x
and y
to be functions of z
, like
x = Function('x')(z)
y = Function('y')(z)
Then normal diff(expr, z)
will take the derivative correctly.

asmeurer
- 86,894
- 26
- 169
- 240
-
So after reading some documentation I got it to work using idiff, but not quite sure what you're saying about using regular diff and setting x and y as functions. I keep getting weird results. Shouldn't z be a function of x or y, depending on which one you're taking with respect to? – MANA624 Feb 18 '16 at 18:26
-
Sure. You said in the question that you are taking the derivative with respect to `z`. – asmeurer Feb 18 '16 at 20:40
1
You commented you used idiff, here is the corresponding code for those who want to know:
from sympy import symbols, idiff, simplify
x, y, z = symbols('x y z')
ex = x**5 + y**2 + z**4 - 8*x*y*z
ex_d = simplify(idiff(ex,(x,y),z))
display(ex_d)
In idiff(ex,(x,y),z)
, (x,y)
are the dependent variables, and z
the variable the derivative is being taken with respect to.

mins
- 6,478
- 12
- 56
- 75