Python partial derivatives easy

55.9k Views Asked by At

I'm interested in computing partial derivatives in Python. I've seen functions which compute derivatives for single variable functions, but not others.

It would be great to find something that did the following

    f(x,y,z) = 4xy + xsin(z)+ x^3 + z^8y
    part_deriv(function = f, variable = x)
    output = 4y + sin(z) +3x^2

Has anyone seen anything like this?

2

There are 2 best solutions below

4
On BEST ANSWER

use sympy

>>> from sympy import symbols, diff
>>> x, y, z = symbols('x y z', real=True)
>>> f = 4*x*y + x*sin(z) + x**3 + z**8*y
>>> diff(f, x)
4*y + sin(z) + 3*x**2
0
On

Use sympy


From their Docs:

>>> diff(sin(x)*exp(x), x)
 x           x
ℯ ⋅sin(x) + ℯ ⋅cos(x)

and for your example:

>>> diff(4*x*y + x*sin(z)+ x**3 + z**8*y,x)
3x**2+4*y+sin(z)