Ask Your Question

Revision history [back]

click to hide/show revision 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)})