This notebook provides a short introduction to differentiable manifolds in SageMath. The tools described below have been implemented through the SageManifolds project.
Click here to download the notebook file (ipynb format). To run it, you must start SageMath with the Jupyter notebook, via the command sage -n jupyter
The following assumes that you are using version 9.2 (or higher) of SageMath:
version()
First we set up the notebook to display mathematical objects using LaTeX rendering:
%display latex
As an example let us define a differentiable manifold of dimension 3 over $\mathbb{R}$:
M = Manifold(3, 'M', latex_name=r'\mathcal{M}', start_index=1)
3
, is the manifold dimension. In SageManifolds, it can be any
positive integer.'M'
, is a string defining the manifold's name; it may be
different from the symbol set on the left-hand side of the = sign (here M
): the latter
stands for a mere Python variable, which refers to the manifold object in the computer
memory, while the string 'M'
is the mathematical symbol chosen for the manifold.latex_name=r'\mathcal{M}'
sets the LaTeX
symbol to display the manifold. Note the letter 'r' in front on the first quote:
it indicates that the string is a raw one, so that the backslash character
in \mathcal
is considered as an ordinary character (otherwise, the backslash is
used to escape some special characters). If the argument latex_name
is not
provided by the user, it is set to the string used as the second argument (here 'M'
)start_index=1
defines the range of indices to be used for
tensor components on the manifold: setting it to 1 means that indices will range
in $\{1,2,3\}$. The default value is start_index=0
.Note that the default base field is $\mathbb{R}$. If we would have used the optional
argument field='complex'
, we would have defined a manifold over $\mathbb{C}$. See the
list of all options for more details.
If we ask for M, it is displayed via its LaTeX symbol:
M
If we use the function print()
instead, we get a short description of the object:
print(M)
Via the function type()
, we get the type of the Python object corresponding to M (here the Python class DifferentiableManifold_with_category
:
type(M)
We can also ask for the category of M and see that it is the category of smooth manifolds over $\mathbb{R}$:
category(M)
The indices on the manifold are generated by the method irange()
, to be used in loops:
[i for i in M.irange()]
If the parameter start_index
had not been specified, the default range of the indices would have been $\{0,1,2\}$ instead:
M0 = Manifold(3, 'M', latex_name=r'\mathcal{M}')
[i for i in M0.irange()]
Let us assume that the manifold $\mathcal{M}$ can be covered by a single chart (other cases are discussed below); the chart is declared as follows:
X.<x,y,z> = M.chart()
The writing .<x,y,z>
in the left-hand side means that the Python variables x
, y
and z
are set to the three coordinates of the chart. This allows one to refer subsequently to the coordinates by their names.
In this example, the function chart()
has no arguments, which implies that the coordinate symbols will be x
, y
and z
(i.e. exactly the characters set in the <...>
operator) and that each coordinate range is $(-\infty,+\infty)$. For other cases, an argument must be passed to chart()
to specify the coordinate symbols and range, as well as the LaTeX symbol of a coordinate if the latter is different from the coordinate name (an example will be provided below).
The chart is displayed as a pair formed by the open set covered by it (here the whole manifold) and the coordinates:
print(X)
X
The coordinates can be accessed individually, by means of their indices, following the convention defined by start_index=1 in the manifold's definition:
X[1]
X[2]
X[3]
The full set of coordinates is obtained by means of the operator [:]:
X[:]
Thanks to the operator <x,y,z>
used in the chart declaration, each coordinate can be accessed directly via its name:
z is X[3]
Coordinates are SageMath symbolic expressions:
type(z)
Real-valued functions of the chart coordinates (mathematically speaking, functions defined on the chart codomain) are generated via the method function() acting on the chart:
f = X.function(x+y^2+z^3)
f
f.display()
f(1,2,3)
They belong to SageManifolds class ChartFunction:
type(f)
and differ from SageMath standard symbolic functions by automatic simplifications in all operations. For instance, adding the two symbolic functions
f0(x,y,z) = cos(x)^2; g0(x,y,z) = sin(x)^2
results in
f0 + g0
while the sum of the corresponding functions in the class ChartFunction is automatically simplified:
f1 = X.function(cos(x)^2); g1 = X.function(sin(x)^2)
f1 + g1
To get the same output with symbolic functions, one has to invoke the method simplify_trig():
(f0 + g0).simplify_trig()
Another difference regards the display; if we ask for the symbolic function f0, we get:
f0
while if we ask for the chart function f1, we get only the coordinate expression:
f1
To get an output similar to that of f0, one should call the method display():
f1.display()
Note that the method expr()
returns the underlying symbolic expression:
f1.expr()
type(f1.expr())
Let us first consider an open subset of $\mathcal{M}$, for instance the complement $U$ of the region defined by $\{y=0, x\geq 0\}$ (note that (y!=0, x<0)
stands for $y\not=0$ OR $x<0$; the condition $y\not=0$ AND $x<0$ would have been written [y!=0, x<0]
instead):
U = M.open_subset('U', coord_def={X: (y!=0, x<0)})
Let us call X_U
the restriction of the chart X
to the open subset $U$:
X_U = X.restrict(U)
X_U
We introduce another chart on $U$, with spherical-type coordinates $(r,\theta,\phi)$:
Y.<r,th,ph> = U.chart(r'r:(0,+oo) th:(0,pi):\theta ph:(0,2*pi):\phi')
Y
The function chart() has now some argument; it is a string, which contains specific LaTeX symbols, hence the prefix 'r' to it (for raw string). It also contains the coordinate ranges, since they are different from the default value, which is $(-\infty, +\infty)$. For a given coordinate, the various fields are separated by the character ':' and a space character separates the coordinates. Note that for the coordinate $r$, there are only two fields, since the LaTeX symbol has not to be specified. The LaTeX symbols are used for the outputs:
th, ph
Y[2], Y[3]
The declared coordinate ranges are now known to Sage, as we may check by means of the command assumptions():
assumptions()
They are used in simplifications:
simplify(abs(r))
simplify(abs(x)) # no simplification occurs since x can take any value in R
After having been declared, the chart Y can be fully specified by its relation to the chart X_U, via a transition map:
transit_Y_to_X = Y.transition_map(X_U, [r*sin(th)*cos(ph), r*sin(th)*sin(ph), r*cos(th)])
transit_Y_to_X
transit_Y_to_X.display()
The inverse of the transition map can be specified by means of the method set_inverse():
transit_Y_to_X.set_inverse(sqrt(x^2+y^2+z^2), atan2(sqrt(x^2+y^2),z), atan2(y, x))
A check of the provided inverse is performed by composing it with the original transition map, on the left and on the right respectively. As indicated, the reported failure for th
and ph
is actually due to a lack of simplification of expressions involving arctan2
.
We have then
transit_Y_to_X.inverse().display()
At this stage, the manifold's atlas (the "user atlas", not the maximal atlas!) contains three charts:
M.atlas()
The first chart defined on the manifold is considered as the manifold's default chart (it can be changed by the method set_default_chart()):
M.default_chart()
Each open subset has its own atlas (since an open subset of a manifold is a manifold by itself):
U.atlas()
U.default_chart()
We can draw the chart $Y$ in terms of the chart $X$ via the command Y.plot(X)
, which shows the lines of constant coordinates from the $Y$ chart in a "Cartesian frame" based on the $X$ coordinates:
Y.plot(X)
The command plot() allows for many options, to control the number of coordinate lines to be drawn, their style and color, as well as the coordinate ranges (cf. the list of all options):
Y.plot(X, ranges={r:(1,2), th:(0,pi/2)}, number_values=4,
color={r:'blue', th:'green', ph:'red'}, aspect_ratio=1)
Conversly, the chart $X|_{U}$ can be plotted in terms of the chart $Y$ (this is not possible for the whole chart $X$ since its domain is larger than that of chart $Y$):
graph = X_U.plot(Y)
show(graph, axes_labels=['r','theta','phi'])