# Manifold graphics in SageMath¶

This notebook illustrates some plotting capabilities of manifold objects in SageMath.

More examples can be found on the SageManifolds page.

In [1]:
%display latex


## The 2-sphere example¶

Let us consider the sphere $\mathbb{S}^2$. In SageMath, we declare it as a 2-dimensional differentiable manifold:

In [2]:
S2 = Manifold(2, 'S^2', latex_name=r'\mathbb{S}^2', start_index=1)
print(S2)

2-dimensional differentiable manifold S^2


We then introduce the standard spherical coordinates $(\theta,\phi)$ as a chart on $\mathbb{S}^2$, denoting the chart as spher and the coordinates th and ph. We shall be lazy here and consider that the spherical chart covers the whole manifold $\mathbb{S}^2$; this amounts to authorize coordinate singularities:

In [3]:
spher.<th,ph> = S2.chart(r'th:[0,pi]:\theta ph:[0,2*pi]:periodic:\phi')
spher

Out[3]:
In [4]:
spher.coord_range()

Out[4]:

For 3D plots, we shall require the Euclidian space $\mathbb{E}^3$:

In [5]:
E3.<X,Y,Z> = EuclideanSpace()
E3

Out[5]:

Let us call cartesian the chart of Cartesian coordinates $(X,Y,Z)$:

In [6]:
cartesian = E3.cartesian_coordinates()
cartesian

Out[6]:

The canonical embedding of $\mathbb{S}^2$ into $\mathbb{E}^3$:

In [7]:
Phi = S2.diff_map(E3, (sin(th)*cos(ph),
sin(th)*sin(ph),
cos(th)),
name='Phi', latex_name=r'\Phi')
Phi.display()

Out[7]:

## Plot of a chart¶

Each chart is endowed with a plot() method. For instance, we may use it to plot the chart of spherical coordinates plotted in terms of itself:

In [8]:
spher.plot(chart=spher)

Out[8]:

More intersting is to the plot the chart of spherical coordinates in terms of $\mathbb{E}^3$'s Cartesian coordinates, via the embedding $\Phi:\; \mathbb{S}^2\to\mathbb{E}^3$:

In [9]:
spher.plot(chart=cartesian, mapping=Phi)

Out[9]:

For a better image, let us increase the number of coordinate values:

In [10]:
spher.plot(chart=cartesian, mapping=Phi, number_values=11)

Out[10]:

One can customize the color of coordinate lines:

In [11]:
spher.plot(chart=cartesian, mapping=Phi, number_values=11,
color={th: 'red', ph: 'green'})

Out[11]:

One can fix the value of some coordinates, thereby obtaining a partial plot:

In [12]:
spher.plot(chart=cartesian, mapping=Phi, number_values=11,
color={th: 'red', ph: 'green'},
fixed_coords={th: pi/4})

Out[12]:

or limit the range of some coordinates:

In [13]:
spher.plot(chart=cartesian, mapping=Phi, number_values=11,
color={th: 'red', ph: 'green'},
ranges={ph:(0, pi)})

Out[13]:

One can also restrict the coordinates of the ambiant chart; here is a example of plot of the chart spher in terms of the coordinates $(X,Y)$ of $\mathbb{E}^3$:

In [14]:
spher.plot(chart=cartesian, mapping=Phi, number_values=11,
color={th: 'red', ph: 'green'},
ambient_coords=(X, Y))

Out[14]:

Same thing with only the coordinates $(X,Z)$:

In [15]:
spher.plot(chart=cartesian, mapping=Phi, number_values=11,
color={th: 'red', ph: 'green'},
ambient_coords=(X, Z))

Out[15]:

Let us introduce the chart of stereographic coordinates from the North pole:

In [16]:
stereoN.<x,y> = S2.chart()
stereoN

Out[16]:
In [17]:
spher_to_stereoN = spher.transition_map(stereoN,
(sin(th)*cos(ph)/(1-cos(th)),
sin(th)*sin(ph)/(1-cos(th))))
spher_to_stereoN.display()

Out[17]:
In [18]:
spher_to_stereoN.set_inverse(2*atan(1/sqrt(x^2+y^2)), atan2(-y,-x)+pi)

Check of the inverse coordinate transformation:
th == 2*arctan(sqrt(-cos(th) + 1)/sqrt(cos(th) + 1))  **failed**
ph == pi + arctan2(sin(ph)*sin(th)/(cos(th) - 1), cos(ph)*sin(th)/(cos(th) - 1))  **failed**
x == x  *passed*
y == y  *passed*
NB: a failed report can reflect a mere lack of simplification.


Plot of spherical coordinate in terms of the stereographic ones:

In [19]:
spher.plot(chart=stereoN, number_values=15, ranges={th: (pi/8,pi)},
color={th: 'red', ph: 'green'})

Out[19]:

Plot of stereographic coordinates in terms of spherical ones:

In [20]:
stereoN.plot(chart=spher, number_values=15, plot_points=200,
color={x: 'blue', y: 'orange'})

Out[20]:

The expression of the embedding $\Phi$ in terms of the stereographic coordinates is computed by Sage:

In [21]:
Phi.display()

Out[21]:

This means that we can use $\Phi$ for a 3D view of the stereographic chart:

In [22]:
stereoN.plot(chart=cartesian, mapping=Phi, number_values=25,
color={x: 'blue', y: 'orange'})

Out[22]:

The hole at the North pole is due to the default limitation to $[-8,8]$ for the plot range of coordinates that span the whole real line. We can change it to $[-20, 20]$ via the argument max_range:

In [23]:
stereoN.plot(chart=cartesian, mapping=Phi, number_values=25,
color={x: 'blue', y: 'orange'},
max_range=20, plot_points=200)

Out[23]: