ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 03 Jun 2019 18:29:45 +0200Trouble with spherical coordinates in sagemanifoldshttps://ask.sagemath.org/question/46776/trouble-with-spherical-coordinates-in-sagemanifolds/ I'm probably doing something obviously wrong, but I'd appreciate help with the following.
I load the following file
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*e^(2*I*_SAGE_VAR_xxxx1))/(e^(4*I*_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.
Mon, 03 Jun 2019 14:06:23 +0200https://ask.sagemath.org/question/46776/trouble-with-spherical-coordinates-in-sagemanifolds/Answer by eric_g for <p>I'm probably doing something obviously wrong, but I'd appreciate help with the following. </p>
<p>I load the following file</p>
<pre><code>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()
</code></pre>
<p>Sage then gives the error message "ValueError: no solution found; use set_inverse() to set the inverse manually". </p>
<p>If I replace the final two lines with </p>
<pre><code> 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()
</code></pre>
<p>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<em>_SAGE_VAR_xxxx1))/(e^(4</em>I*_SA ... [dozens of lines of similar] ... tan(_SAGE_VAR_xxxx1)^2+1)],union())]' in Sage. </p>
<p>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. </p>
https://ask.sagemath.org/question/46776/trouble-with-spherical-coordinates-in-sagemanifolds/?answer=46778#post-id-46778As 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](https://nbviewer.jupyter.org/github/sagemanifolds/SageManifolds/blob/master/Worksheets/v1.3/SM_hyperbolic_plane.ipynb).Mon, 03 Jun 2019 18:29:45 +0200https://ask.sagemath.org/question/46776/trouble-with-spherical-coordinates-in-sagemanifolds/?answer=46778#post-id-46778