Revision history [back]

You should use substitute_function, not subs, having first defined g0*cos(th) + g2 as a function of (th, r):

sage: G(r, th) = g0 + cos(th)*g2
sage: f = f.substitute_function(function('g'), G)
sage: f
cos(th)*g2(r) + cos(th)*diff(g2(r), r) + g0(r) + diff(g0(r), r)


Note that the first argument of substitute_function has to be function('g') because in the declaration g = function('g')(r, th), you have overwritten the Python variable g by the expression function('g')(r,th). It would have been better to keep the Python name g for function('g'), i.e. to write

sage: g = function('g')
sage: f = g(r, th) + diff(g(r, th), r)


Then you can use simply g as the first argument of substitute_function::

sage: f.substitute_function(g, G)
cos(th)*g2(r) + cos(th)*diff(g2(r), r) + g0(r) + diff(g0(r), r)