# Trouble with spherical coordinates in sagemanifolds I'm probably doing something obviously wrong, but I'd appreciate help with the following.

theManifold = Manifold(3, 'M', r'\mathcal{M}')
theOpenSet = theManifold.open_subset('theOpenSet')
CartesianChart.<x,y,z> = theOpenSet.chart(r'x y z')
g = theManifold.lorentzian_metric('g')
g[0,0] = 1
g[1,1] = 1
g[2,2] = 1
show(g.display())
sphericalChart.<r,th,ph> = theOpenSet.chart(r'r th:(0,pi):\theta ph:(0,2*pi):\phi')

spherical_to_Cartesian = sphericalChart.transition_map(CartesianChart,[r*sin(th)*sin(ph),r*sin(th)*cos(ph),r*cos(th)])
Cartesian_to_spherical = spherical_to_Cartesian.inverse()


Sage then gives the error message "ValueError: no solution found; use set_inverse() to set the inverse manually".

If I replace the final two lines with

 Cartesian_to_spherical = CartesianChart.transition_map(sphericalChart[sqrt(x^2+y^2+z^2),arctan(z/sqrt(x^2+y^2)),arctan(y/x)])
spherical_to_Cartesian = Cartesian_to_spherical.inverse()


then Sage gives the following unable to make sense of Maxima expression '[if((-pi/2<parg(_sage_var_xxxx0))and(-pi 2<parg(-_sage_var_xxxx0="" sqrt((4<em="">e^(2I_SAGE_VAR_xxxx1))/(e^(4I*_SA ... [dozens of lines of similar] ... tan(_SAGE_VAR_xxxx1)^2+1)],union())]' in Sage.

I am aware that Sagemanifolds has native commands for using spherical coordinates. I'm attempting to work on a more complicated manifold, and this is is a minimal working example to demonstrate my problems.

edit retag close merge delete

Sort by » oldest newest most voted

As the error message says, you must use set_inverse() to set the inverse by hand, because SageMath is not capable to invert the system automatically in this case. So you should run

spherical_to_Cartesian.set_inverse(sqrt(x^2+y^2+z^2),
atan2(sqrt(x^2+y^2), z),
atan2(y, x))


Then you may write

Cartesian_to_spherical = spherical_to_Cartesian.inverse()
Cartesian_to_spherical.display()


the output of which is

r = sqrt(x^2 + y^2 + z^2)
th = arctan2(sqrt(x^2 + y^2), z)
ph = arctan2(y, x)


Note that you can add the optional parameter verbose=True to set_inverse in order for SageMath to check that the provided inverse is valid (the check consists in performing the coordinate transformation followed by its inverse, in both directions):

spherical_to_Cartesian.set_inverse(sqrt(x^2+y^2+z^2),
atan2(sqrt(x^2+y^2), z),
atan2(y, x), verbose=True)


This results in

Check of the inverse coordinate transformation:
r == abs(r)
th == arctan2(abs(r)*sin(th), r*cos(th))
ph == arctan2(r*cos(ph)*sin(th), r*sin(ph)*sin(th))
x == y
y == x
z == z


As you can see, the check is not perfect, partly because you did not specify the range for the coordinate r, so that abs(r) could not be simplified. If you specify r:(0,+oo) while declaring sphericalChart, then you get:

Check of the inverse coordinate transformation:
r == r
th == arctan2(r*sin(th), r*cos(th))
ph == arctan2(r*cos(ph)*sin(th), r*sin(ph)*sin(th))
x == y
y == x
z == z


This is almost OK, except for some lack of simplification of the arctan2 function.

PS: many examples of use of set_inverse() can be found in the hyperbolic plane example.

more