为什么这段代码
user_f = lambda a, b: a + b
user_x = lambda u: 2 * u
import sympy
from sympy.abc import t
from sympy.utilities.lambdify import implemented_function
x = implemented_function(sympy.Function('x'), user_x)
f = implemented_function(sympy.Function('f'), user_f)
dx = sympy.diff(f(x(t), t), t, 1)
print(dx)
fl = sympy.lambdify((x(t), t), dx)
print(fl(x(t), t))
给我下面的输出? (它不应该有足够的信息来全面评估衍生物吗?)
如何修复(或解决)此错误?假设我被user_f
和user_x
作为输入。
Derivative(x(t), t)*Subs(Derivative(f(_xi_1, t), _xi_1), (_xi_1,), (x(t),)) + Subs(Derivative(f(x(t), _xi_2), _xi_2), (_xi_2,), (t,))
Traceback (most recent call last):
File <path>, line 12, in <module>
print(fl(x(t), t))
File "<string>", line 1, in <lambda>
NameError: global name 'Derivative' is not defined
答案 0 :(得分:2)
提供modules
sympy.lambdify()
参数消除了问题:
>>> fl = sympy.lambdify((x(t), t), dx, modules=sympy)
>>> print(fl(x(t), t))
Derivative(x(t), t)*Subs(Derivative(_xi_1 + t, _xi_1), (_xi_1,), (x(t),)) + Subs(Derivative(_xi_2 + x(t), _xi_2), (_xi_2,), (t,))