sympy:将函数代入表达式

sympy: substitute function into expression

我有一个二维曲线 rho(t)

import sympy as sy

t = sy.Symbol('t', is_real=True) # curve parameter
rho_x = sy.Function('rho_x', is_real=True)(t)
rho_y = sy.Function('rho_y', is_real=True)(t) # components
rho = sy.Matrix([rho_x, rho_y]) # curve vector
tau = rho.diff() / rho.diff().norm() # tangent

之后我想评估特定实现的曲线参数:

real_shape = {rho_x: sy.sin(t), rho_y: sy.cos(t)}
f1 = tau.subs(real_shape)
print f1
f2 = sy.lambdify(t, f1)
print f2(0.1)

我发现 SymPy 不会自动计算正弦和余弦函数的导数,调用 print f2(0.1) 后显示错误消息:

NameError: global name 'Derivative' is not defined

我的示例有什么问题?

您可以在 Matrix 上调用 doit() 来计算它。

In [49]: f1 = f1.doit()

In [50]: f2 = lambdify(t, f1)

In [51]: print f2(0.1)
[[ 0.99500417]
 [-0.09983342]]

另请注意,您的假设是错误的。 t 的真实假设应该这样写:

In [52]: t = Symbol('t', real=True)