1 | initial version |

You can avoid the `TypeError`

by using the following python function

```
def formal_diff(f, x):
tempX = SR.temp_var()
return f.subs({x: tempX}).diff(tempX).subs({tempX: x})
```

Now, `formal_diff(f,g)`

gives you `D[0](f)(g(r))`

.

**Bonus 1**: You can avoid the sometimes awkward `D[]`

notation by importing the `ExpressionNice`

.

```
from sage.manifolds.utilities import ExpressionNice as EN
EN(formal_diff(f,g))
```

gives `d(f)/d(g(r))`

.

**Bonus 2**: Hiding at the docstring of the SR.temp_var() you can find a general functional derivative

```
def functional_derivative(expr,f,x):
with SR.temp_var() as a:
return expr.subs({f(x):a}).diff(a).subs({a:f(x)})
```

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.