# Partial derivative and chain rule

I have the following variable and function:

var('r')
g = function('g')(r)


Now, I define the function f, which depends on g:

f = function('f')(g)


If I want to compute the derivative diff(f,r), I get:

D(f)(g(r))*diff(g(r), r)


which is the usual chain rule. However, if I want the derivative with respect to g:

diff(f,g)


I get an error:

TypeError: argument symb must be a symbol

Is there a way I can calculate the partial derivative of a function? I would expect a symbolic expression, like

$\displaystyle \frac{\partial f}{\partial g}$

I have seen that in REDUCE there is the package DFPART which accounts for derivatives with respect to generic functions, but I have not found an analogous module in SageMath.

edit retag close merge delete

Sort by » oldest newest most voted

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(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)})

more

Let's dissect this case :

sage: f=function("f")
sage: g=function("g")


Sage knows the chain rule, but expresses it in a somewhat unusual form :

sage: diff(g(f(x)),x)
diff(f(x), x)*D(g)(f(x))


Let's try to understand it :

sage: diff(g(f(x)),x).operands()
D(g)(f(x))
sage: diff(g(f(x)),x).operands().operator()
D(g)


This is not a "usual" function, but it has arguments :

sage: diff(g(f(x)),x).operands().arguments()
(x,)


and is applied to a list of arguments :

sage: diff(g(f(x)),x).operands().operands()
[f(x)]


When applied to x, it returns the valye od the derivative of f at the point x :

sage: diff(g(f(x)),x).operands().operator()(x)
diff(g(x), x)


but it applies to any value, including an unrelated variable :

sage: var("t")
t
sage: diff(g(f(x)),x).operands().operator()(t)
diff(g(t), t)


This object is not a symbolic expression :

sage: diff(g(f(x)),x).operands().operator() in SR
False


so let's investigate :

sage: type(diff(g(f(x)),x).operands().operator())
<class 'sage.symbolic.operators.FDerivativeOperator'>


You should read help(type(diff(g(f(x)),x).operands().operator())) and the documentation of operators ... Note that FDerivativeOperator isn't directly accessible :

import_statements(type(diff(g(f(x)),x).operands().operator()))
from sage.symbolic.operators import FDerivativeOperator


And that the ? interface to its help fails :

sage: FDerivativeOperator?


[in the *Messages* buffer of emacs : ] Object FDerivativeOperator not found.

HTH,

more

Thanks! Sometimes I struggle to find information about certain topics of SageMath (as the one in this thread), so the tool help() will be useful.