Python Sympy printing differentiated user defined composite function; how to toggle substitution

旧巷老猫 提交于 2019-12-10 11:16:01

问题


I want to get rid of an extra substitution which sympy makes when differentiating a user defined composite function. The code is

t = Symbol('t')
u = Function('u')
f = Function('f')
U = Symbol('U')

pprint(diff(f(u(t),t),t))

The output is:

d                d        ⎛ d           ⎞│       
──(f(u(t), t)) + ──(u(t))⋅⎜───(f(ξ₁, t))⎟│       
dt               dt       ⎝dξ₁          ⎠│ξ₁=u(t)

I guess it does this because you can't differentiate w.r.t u(t), so this is ok. What I want to do next is to substitute u(t) with an other variable say U and then get rid of the extra substitution \xi_1

⎞│
⎟│
⎠│ξ₁=U

To clarify, I want this output:

d             d     ⎛d          ⎞
──(f(U, t)) + ──(U)⋅⎜──(f(U, t))⎟
dt            dt    ⎝dU         ⎠

The reason is; when I Taylor expand a composite function like this, the extra substitutions make the output unreadable. Does anyone know how to do this? Any other solution is of course welcomed.


回答1:


Substituting is done with subs. If something is not evaluated you can force it with the doit method.

>>> diff(f(u(t),t),t).subs(u(t),U)
Derivative(U,t)∗Subs(Derivative(f(xi1,t),xi1),(xi1,),(U,))+Derivative(f(U,t),t)

>>> _.doit()
Derivative(f(U,t),t)

Check the tutorial! It has all these ideas presented nicely.



来源:https://stackoverflow.com/questions/19050103/python-sympy-printing-differentiated-user-defined-composite-function-how-to-tog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!