import sympy as sy
import numpy as np
import scipy.interpolate as interp
# the expression:
t = sy.Symbol('t')
f = sy.Function('f')(t)
expr = (f + f**2 / 2).diff()
# implementation:
f_spline = interp.splrep(np.linspace(0, 1, 100), np.sin(np.linspace(0, 1, 100)))
def df_impl(x):
print '[trace] f_der(' + str(x) + ')'
return interp.splev(x, f_spline, der=1)
def f_impl(x):
print '[trace] f(' + str(x) + ')'
return interp.splev(x, f_spline)
expr_impl = sy.lambdify(t, expr, {'Derivative(f(t), t)': df_impl, 'f': f_impl})
print expr_impl(0.1)
File "<string>", line 1, in <lambda>
NameError: global name 'Derivative' is not definedIt's clear because lambdify can't substitute the the derivative string. There is a workaround, I can do 2 subs:
expr = expr.subs({'Derivative(f(t), t)': 'g(t)'})
expr_impl = sy.lambdify(t, expr, {'g': df_impl, 'f': f_impl})
print expr_impl(0.1)