Python partial derivatives easy

cnrk picture cnrk · Jun 11, 2015 · Viewed 30.7k times · Source

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?

Answer

wtayyeb picture wtayyeb · Jun 11, 2015

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