Take Python Function and Generate All Derivatives

守給你的承諾、 提交于 2019-12-04 17:12:10

The trick is to use inspect.getargspec to get the names of all the arguments to the function. After that, it's a simple list comprehension:

import inspect
from sympy import *

def get_derivatives(func):
    arg_symbols = symbols(inspect.getargspec(func).args)
    sym_func = func(*arg_symbols)

    return [lambdify(arg_symbols, sym_func.diff(a)) for a in arg_symbols]

For example:

def f(x, y):
    return sin(x)*cos(y)

all_derivatives = get_derivatives(f)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!