Billy Matlock
Billy Matlock

Reputation: 350

After substituting Functions in sympy, the evaluation fails to do straightforward derivatives

Last Day I have been bothering with this problem. At first I made a question but after comment of needing a simplified example, I deleted the question and I finally found the "source" of the problem.

I fail to evaluate expressions AFTER substituting Functions by expressions: Following example will show you what I mean:

xx     = sy.Symbol('x',real=True)
yy     = sy.Symbol('y',real=True)
FuncT  = sy.Function('F')
TestExp= sy.cos(sy.diff(FuncT(xx,yy),xx)+xx+yy*xx+yy)
print(TestExp.subs({FuncT(xx,yy):xx})).subs({xx:1,yy:0.1})

which results

enter image description here

How can it replace dx/dx = 1 ?

Upvotes: 1

Views: 35

Answers (1)

smichr
smichr

Reputation: 18939

Just doit:

>>> TestExp.subs({FuncT(xx,yy):xx}).subs({xx:1,yy:0.1}).doit()
-0.588501117255346

How to know to use doit?

When I print (not pprint) the expressions I see

cos(Subs(Derivative(x, x), x, 1) + 1.2)

I don't want Subs there but I don't know much about Subs so I ask for help and read the following:

>>> help(Subs)
...
There's no automatic expansion - use the method .doit() to effect all
possible substitutions of the object and also of objects inside the
expression.
...

Upvotes: 4

Related Questions