Maksim Surov
Maksim Surov

Reputation: 613

sympy: substitute function into expression

I have a 2D curve 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

after that I want to evaluate parameters of the curve for a specific realization:

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)

I found that SymPy does not evaluate the derivatives of sine and cosine functions automatically, and after calling print f2(0.1) it shows error message:

NameError: global name 'Derivative' is not defined

What is wrong in my example?

Upvotes: 0

Views: 1218

Answers (1)

Sudhanshu Mishra
Sudhanshu Mishra

Reputation: 2092

You can call doit() on the Matrix to evaluate it.

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

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

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

Also note that you are putting assumptions wrong way. Real assumption on t should be put like this:

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

Upvotes: 1

Related Questions