# Coefficients of partial derivatives

In working with the Laplacian of a scalar field, I want to collect the complete coefficient expression of each partial derivative. In 3-space, this can be done "by hand", but in 6-space, automation is essential to avoid error. Consider:

sage: version()
'SageMath version 8.5, Release Date: 2018-12-22'
sage: #Patch to avoid maxima bug
sage: maxima_calculus.eval("domain:real;")
'real'
sage: M = Manifold(3,'R^3',field='real',start_index=1)
sage: c_C.<x,y,z> = M.chart();
sage: g = M.riemannian_metric('g');
sage: g[1,1],g[2,2],g[3,3] = 1,1,1;
sage: g.display()
g = dx*dx + dy*dy + dz*dz
sage: c_S.<r,theta,phi> = M.chart(r'r:(0,+oo) theta:(0,pi/2):\theta phi:(0,2*pi):\phi');
sage: ch_C_S = c_C.transition_map(c_S, (sqrt(x^2+y^2+z^2), arccos(z/(sqrt(x^2+y^2+z^2))), arctan2(y,x)))
sage: ch_C_S
Change of coordinates from Chart (R^3, (x, y, z)) to Chart (R^3, (r, theta, phi))
sage: ch_C_S.set_inverse(r*sin(theta)*cos(phi), r*sin(theta)*sin(phi), r*cos(theta))
sage: M.set_default_chart(c_S)    # this saves a little typing later
sage: M.set_default_frame(c_S.frame())
sage: chi = M.scalar_field(function('chi')(r,theta,phi), name='chi', latex_name=r'\chi')
sage: delChi = chi.laplacian(g)
sage: ddChi = delChi.expr()
sage: ddChi.collect_common_factors()
((r^2*diff(chi(r, theta, phi), r, r) + 2*r*diff(chi(r, theta, phi), r) + diff(chi(r, theta, phi), theta, theta))*sin(theta)^2 + cos(theta)*sin(theta)*diff(chi(r, theta, phi), theta) + diff(chi(r, theta, phi), phi, phi))/(r^2*sin(theta)^2)

# We can find the coefficients of scalar variables (sort of):
sage: ddChi.coefficient(r,2); ddChi.coefficient(r,1)
diff(chi(r, theta, phi), r, r)/r^2
2*diff(chi(r, theta, phi), r)/r^2
sage: ddChi.coefficient(sin(theta)^2)    # From which we see that .coefficient() is a shallow operator.
(r^2*diff(chi(r, theta, phi), r, r) + 2*r*diff(chi(r, theta, phi), r) + diff(chi(r, theta, phi), theta, theta))/(r^2*sin(theta)^2)


In the above, we work on the expression for the Laplacian (living in the symbolic ring). But .coefficient() will not work with a partial derivative as argument. Searching through the functions available for delChi (the Laplacian), I see nothing applicable there either. Any suggestions?

edit retag close merge delete

Sort by » oldest newest most voted

coefficient() does work with partial derivative as argument. Actually the issue in your case is that you have used the same symbol chi to denote the scalar field and the symbolic function representing the scalar field in spherical coordinates. Indeed, when you write

chi = M.scalar_field(function('chi')(r,theta,phi), name='chi', latex_name=r'\chi')


function('chi') injects the name chi in the global namespace to denote the symbolic function $chi(r,\theta,\phi)$, but this name is then overwritten by chi = ... to denote instead the scalar field. Hence, when you enter diff(chi(r, theta, phi), r) you get an error. The solution is of course to use different names for the symbolic function and the scalar field, e.g.

chi_s = M.scalar_field(function('chi')(r,theta,phi), name='chi', latex_name=r'\chi')


Here is then the full code for collecting the coefficients of the partial derivatives in the Laplacian (I have used the Euclidean space as a manifold to shorten the code):

sage: M.<r,theta,phi> = EuclideanSpace(coordinates='spherical')
sage: chi_s = M.scalar_field(function('chi')(r,theta,phi), name='chi', latex_name=r'\chi')
sage: delChi = chi_s.laplacian()
sage: ddChi = delChi.expr()
sage: ddChi
((r^2*diff(chi(r, theta, phi), r, r) + 2*r*diff(chi(r, theta, phi), r) + diff(chi(r, theta, phi), theta, theta))*sin(theta)^2 + cos(theta)*sin(theta)*diff(chi(r, theta, phi), theta) + diff(chi(r, theta, phi), phi, phi))/(r^2*sin(theta)^2)
sage: for coord in [r, theta, phi]:
....:     for order in [1, 2]:
....:         der = diff(chi(r,theta,phi), coord, order)
....:         print("{}:  {}".format(der, ddChi.coefficient(der, 1)))
....:
diff(chi(r, theta, phi), r):  2/r
diff(chi(r, theta, phi), r, r):  1
diff(chi(r, theta, phi), theta):  cos(theta)/(r^2*sin(theta))
diff(chi(r, theta, phi), theta, theta):  r^(-2)
diff(chi(r, theta, phi), phi):  0
diff(chi(r, theta, phi), phi, phi):  1/(r^2*sin(theta)^2)

more