user541686
user541686

Reputation: 210725

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.

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

Upvotes: 3

Views: 2731

Answers (1)

Leon
Leon

Reputation: 32504

Providing the modules argument of sympy.lambdify() eliminates the problem:

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

Upvotes: 3

Related Questions