Ask Your Question
0

Partial derivative and chain rule

asked 2021-12-15 12:59:18 +0100

keko gravatar image

updated 2021-12-15 18:05:58 +0100

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[0](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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2021-12-15 21:04:40 +0100

cav_rt gravatar image

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)})
edit flag offensive delete link more
1

answered 2021-12-15 22:17:54 +0100

Emmanuel Charpentier gravatar image

updated 2021-12-15 22:28:43 +0100

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[0](g)(f(x))

Let's try to understand it :

sage: diff(g(f(x)),x).operands()[1]
D[0](g)(f(x))
sage: diff(g(f(x)),x).operands()[1].operator()
D[0](g)

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

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

and is applied to a list of arguments :

sage: diff(g(f(x)),x).operands()[1].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()[1].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()[1].operator()(t)
diff(g(t), t)

This object is not a symbolic expression :

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

so let's investigate :

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

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

import_statements(type(diff(g(f(x)),x).operands()[1].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,

edit flag offensive delete link more

Comments

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.

keko gravatar imagekeko ( 2021-12-16 15:28:48 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-12-15 12:59:18 +0100

Seen: 112 times

Last updated: Dec 15 '21