# Revision history [back]

coefficient() does work with partial derivative as argument. Actually the issue in your case is that you have use 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 use 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 var in [r, theta, phi]:
....:     for order in [1, 2]:
....:         der = diff(chi(r,theta,phi), var, 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)


coefficient() does work with partial derivative as argument. Actually the issue in your case is that you have use 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 use 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 var in [r, theta, phi]:
....:     for order in [1, 2]:
....:         der = diff(chi(r,theta,phi), var, 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)


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 use 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 var in [r, theta, phi]:
....:     for order in [1, 2]:
....:         der = diff(chi(r,theta,phi), var, 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)


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 use 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 var in [r, theta, phi]:
....:     for order in [1, 2]:
....:         der = diff(chi(r,theta,phi), var, 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)


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 var coord in [r, theta, phi]:
....:     for order in [1, 2]:
....:         der = diff(chi(r,theta,phi), var, 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)