简体   繁体   English

为什么我的implements_function()导致NameError:全局名称'Derivative'没有定义?

[英]Why my implemented_function() results in NameError: global name 'Derivative' is not defined?

Why does this code 为什么这个代码

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))

give me the output below? 给我下面的输出? (Shouldn't it have sufficient information to fully evaluate the derivatives?) (它不应该有足够的信息来全面评估衍生品吗?)

How do I fix (or work around) this error? 如何修复(或解决)此错误? Assume I am given user_f and user_x as inputs. 假设我被赋予user_fuser_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

Providing the modules argument of sympy.lambdify() eliminates the problem: 提供sympy.lambdify()modules参数可以解决问题:

>>> 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,))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM