2

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 Answers1

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.

Krastanov
  • 6,479
  • 3
  • 29
  • 42