Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Redefine symbolic function even in derivatives

As a simple example, I have the following variables and functions:

r = var('r')
th = var('th', latex_name = '\\theta')

g = function('g')(r, th)

f = g + diff(g,r)

From now on I want to decompose g into g0and g2 as:

g0 = function('g0', latex_name = 'g_0')(r)
g2 = function('g2', latex_name = 'g_2')(r)

g = g0 + cos(th)*g2

If I print f I still get:

$ f = g(r, \theta) + frac{dg(r,\theta)}{dr} $

So instead, I apply a substitution on g:

f = f.subs(g==g0+cos(th)*g2)

This changes g, but not diff(g,r):

$f = g_0(r) + g_2(r)*\cos(\theta) + frac{dg(r,\theta)}{dr}$

In order to change also diff(g,r) I have to substitute explicitly the derivative:

f = f.subs(diff(g,r) == diff(g0+cos(th)*g2, r))

My question is the following:

Is there a way I can redefine a function without having to redefine also every single derivative? This way, I would avoid having to write all these substitutions:

diff(g,r)     ==   diff(g0+g2*cos(th), r),
diff(g,r,r)   ==   diff(g0+g2*cos(th), r, r),
diff(g,th)    ==   diff(g0+g2*cos(th), th),
diff(g,th,th) ==   diff(g0+g2*cos(th), th, th),
diff(g,th,r)  ==   diff(g0+g2*cos(th), th, r),
diff(g,r,th)  ==   diff(g0+g2*cos(th), r,  th)

Redefine symbolic function even in derivatives

As a simple example, I have the following variables and functions:

r = var('r')
th = var('th', latex_name = '\\theta')

g = function('g')(r, th)

f = g + diff(g,r)

From now on I want to decompose g into g0and g2 as:

g0 = function('g0', latex_name = 'g_0')(r)
g2 = function('g2', latex_name = 'g_2')(r)

g = g0 + cos(th)*g2

If I print f I still get:

$ f = g(r, \theta) + frac{dg(r,\theta)}{dr} dg(r,\theta)/dr $

So instead, I apply a substitution on g:

f = f.subs(g==g0+cos(th)*g2)

This changes g, but not diff(g,r):

$f = g_0(r) + g_2(r)*\cos(\theta) + frac{dg(r,\theta)}{dr}$dg(r,\theta)/dr$

In order to change also diff(g,r) I have to substitute explicitly the derivative:

f = f.subs(diff(g,r) == diff(g0+cos(th)*g2, r))

My question is the following:

Is there a way I can redefine a function without having to redefine also every single derivative? This way, I would avoid having to write all these substitutions:

diff(g,r)     ==   diff(g0+g2*cos(th), r),
diff(g,r,r)   ==   diff(g0+g2*cos(th), r, r),
diff(g,th)    ==   diff(g0+g2*cos(th), th),
diff(g,th,th) ==   diff(g0+g2*cos(th), th, th),
diff(g,th,r)  ==   diff(g0+g2*cos(th), th, r),
diff(g,r,th)  ==   diff(g0+g2*cos(th), r,  th)