Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/sage/SageManifolds/SM_tutorial.sagews
Views: 1034
SageManifolds tutorial
This worksheet provides a short introduction to SageManifolds (version 0.8).
It is released under the GNU General Public License version 3.
(c) Eric Gourgoulhon, Michal Bejger (2015)
The corresponding worksheet file can be downloaded from here.
Defining a manifold
The following assumes that the SageManifolds package is installed on your computer (see the download and installation instructions if not).
As an example let us define a differentiable manifold of dimension 3 over :
- The first argument, 3, is the manifold dimension. In SageManifolds, it can be any positive integer.
- The second argument, '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 is a mere Python variable name that refers to the manifold object in the computer memory, while 'M' identifies the manifold.
- The third argument, r'\mathcal{M}', is a string defining the LaTeX symbol to represent 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).
- The fourth argument, 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 . The default value is start_index=0.
If we ask for M, the LaTeX symbol is returned (provided the worksheet's Typeset box is selected):
If we use the command print instead, we get a short description of the object:
Via the command type, we get the type of the Python object corresponding to M (here the Python class Manifold_with_category):
The indices on the manifold are generated by the method irange(), to be used in loops:
If the parameter start_index had not been specified, the default range of the indices would have been instead:
Defining a chart on the manifold
Let us assume that the manifold can be covered by a single chart (other cases are discussed below); the chart is declared as follows:
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 . For other cases, an argument must be passed to chart() to specify the coordinate symbols and range, as well as the LaTeX symbols of the coordinates if the latter are different from the coordinate names (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 coordinate names:
The coordinates can be accessed individually, by means of their indices, following the convention defined by start_index=1 in the manifold's definition:
The full set of coordinates is obtained by means of the operator [:]:
Thanks to the operator <x,y,z> in the chart declaration, each coordinate can be accessed directly via its name:
Coordinates are Sage symbolic expressions:
Functions of the chart coordinates
Real-valued functions of the chart coordinates (mathematically speaking, functions defined on the chart codomain) are formed via the method function() acting on the chart:
They belong to SageManifolds class FunctionChart:
and differ from Sage standard symbolic functions by automatic simplifications in all operations. For instance, adding the two symbolic functions
results in
while the sum of the corresponding FunctionChart functions is automatically simplified:
To get the same output with symbolic functions, one has to call the method simplify_trig():
Another difference regards the display; if we ask for the symbolic function f0, we get:
while if we ask for the chart function f1, we get only the coordinate expression:
To get an output similar to that of f0, one should call the method display():
Note that the method expr() returns the corresponding Sage symbolic expression:
Introducing a second chart on the manifold
Let us first consider an open subset of , for instance the complement of the region defined by (note that (y!=0, x<0) stands for OR ; the condition AND would have been written [y!=0, x<0] instead):
Let us call X_U the restriction of the chart X to the open subset :
We introduce another chart on , with spherical-type coordinates :
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 . For a given coordinate, the various fields are separated by the character ':' and a space character separates the coordinates. Note that for , there is only two fields, since the LaTeX symbol has not to be specified. The LaTeX symbols are used for the outputs:
The declared coordinate ranges are now known to Sage, as we may check by means of the command assumptions():
They are used in simplifications:
After having been declared, the chart Y can be fully specified by its relation to the chart X_U, via a transition map:
The inverse of the transition map can be specified by means of the method set_inverse():
The check is passed, although some simplifications related to the arctan2 function are not performed.
At this stage, the manifold's atlas (the "user atlas", not the maximal atlas!) contains three charts:
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()):
Each open subset has its own atlas:
We can draw the chart in terms of the chart : the plot shows lines of constant coordinates from the chart in a "Cartesian frame" based on the coordinates:
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:
Conversly, the chart can be plotted in terms of the chart (this is not possible for the whole chart since its domain is larger than that of chart ):
Points on the manifold
A point on is defined by its coordinates in a given chart:
Since is the manifold's default chart, its name can be omitted:
Of course, belongs to :
It is also in :
Indeed the coordinates of have :
Note in passing that since is the default chart on , its name can be omitted in the arguments of coord():
The coordinates of can also be obtained by letting the chart acting of the point (from the very definition of a chart!):
Let be a point with and :
This time, the point does not belong to :
Accordingly, we cannot ask for the coordinates of in the chart :
but we can for point :
Points can be compared:
In Sage's terminology, points are elements, whose parents are the manifold on which they have been defined:
Scalar fields
A scalar field is a differentiable mapping , where is an open subset of .
The scalar field is defined by its expressions in terms of charts covering its domain (in general more than one chart is necessary to cover all the domain):
The coordinate expressions of the scalar field are passed as a Python dictionary, with the charts as keys, hence the writing {X_U: x+y^2+z^3}.
Since in the present case, there is only one chart in the dictionary, an alternative writing is
Since X_U is the domain's default chart, it can be omitted in the above declaration:
As a mapping , a scalar field acts on points, not on coordinates:
The expression of the scalar field in terms of the coordinates :
If the method display() is used without any argument, it displays the coordinate expression of the scalar field in all the charts defined on the domain (except for subcharts, i.e. the restrictions of some chart to a subdomain):
Note that the expression of the scalar field in terms of the coordinates has not been provided by the user: it has been automatically computed via the change-of-coordinate formula declared above in the transition map.
In each chart, the scalar field is represented by a function of the chart coordinates (an object of the type FunctionChart described above), which is accessible via the method function_chart():
The "raw" symbolic expression is returned by the method expr():
A scalar field can also be defined by some unspecified function of the coordinates:
The parent of is the set of all smooth scalar fields on , which is a commutative algebra over :
The base ring of the algebra is the field , which is represented by Sage's Symbolic Ring (SR):
Arithmetic operations on scalar fields are defined through the algebra structure:
Tangent spaces
The tangent vector space to the manifold at point is obtained as follows:
is a 2-dimensional vector space over (represented here by Sage Symbolic Ring (SR)) :
is automatically endowed with vector bases deduced from the vector frames defined around the point:
For the tangent space at the point , on the contrary, there is only one pre-defined basis, since is not in the domain of the frame associated with coordinates :
A random element:
Note that, despite what the above simplified writing may suggest (the mention of the point or is omitted in the basis vectors), and are different vectors, for they belong to different vector spaces:
In particular, it is not possible to add and :
Vector Fields
Each chart defines a vector frame on the chart domain: the so-called coordinate basis:
The list of frames defined on a given open subset is returned by the method frames():
Unless otherwise specified (via the command set_default_frame()), the default frame is that associated with the default chart:
Individual elements of a frame can be accessed by means of their indices:
We may define a new vector field as follows:
A vector field can be defined by its components with respect to a given vector frame. When the latter is not specified, the open set's default frame is of course assumed:
Vector fields on are Sage element objects, whose parent is the set of vector fields defined on :
The set is a module over the commutative algebra of scalar fields on :
A vector field acts on scalar fields:
Unset components are assumed to be zero:
A vector field on can be expanded in the vector frame associated with the chart :
By default, the components are expressed in terms of the default coordinates . To express them in terms of the coordinates , one should add the corresponding chart as the second argument of the method display():
The components of a tensor field w.r.t. the default frame can also be obtained as a list, via the command [:]:
An alternative is to use the method display_comp():
To obtain the components w.r.t. to another frame, one may go through the method comp() and specify the frame:
However a shortcut is to provide the frame as the first argument of the square brackets:
Components are shown expressed in terms of the default's coordinates; to get them in terms of the coordinates instead, add the chart name as the last argument in the square brackets:
or specify the chart in display_comp():
To get some components of a vector as a scalar field, instead of a coordinate expression, use double square brackets:
A vector field can be defined with components being unspecified functions of the coordinates:
Values of vector fields at a given point
The value of a vector field at some point of the manifold is obtained via the method at():
Indeed, recall that, w.r.t. chart X_U=, the coordinates of the point and the components of the vector field are
Note that to simplify the writing, the symbol used to denote the value of the vector field at point is the same as that of the vector field itself (namely ); this can be changed by the method set_name():
Of course, belongs to the tangent space at :
1-forms
A 1-form on is a field of linear forms. For instance, it can be the differential of a scalar field:
In the above writing, the 1-form is expanded over the basis associated with the chart . This basis can be accessed via the member coframe:
The list of all coframes defined on a given manifold open subset is returned by the method coframes():
As for a vector field, the value of the differential form at some point on the manifold is obtained by the method at():
Recall that
The linear form belongs to the dual of the tangent vector space at :
As such, it is acting on vectors at , yielding a real number:
The differential 1-form of the unspecified scalar field :
A 1-form can also be defined from scratch:
It can be specified by providing its components in a given coframe:
Of course, one may set the components in a frame different from the default one:
The components in the coframe are updated automatically:
Let us revert to the values set previously:
This time, the components in the coframe are those that are updated:
A 1-form acts on vector fields, resulting in a scalar field:
In the case of a differential 1-form, the following identity holds:
1-forms are Sage element objects, whose parent is the set of all tensor fields of type (0,1) defined on :
is actually the dual of the free module :
Differential forms and exterior calculus
The exterior product of two 1-forms is taken via the method wedge() and results in a 2-form:
A matrix view of the components:
Displaying only the non-vanishing components, skipping the redundant ones (i.e. those that can be deduced by antisymmetry):
The 2-form can be expanded on the coframe:
As a 2-form, can be applied to a pair of vectors and is antisymmetric:
The exterior derivative of a differential form is taken by invoking the function xder():
The exterior derivative is nilpotent:
Lie derivative
The Lie derivative of any tensor field with respect to a vector field is computed by the method lie_der(), with the vector field as argument:
Let us check Cartan identity on the 1-form :
and on the 2-form :
The Lie derivative of a vector field along another one is the commutator of the two vectors fields:
Tensor fields of arbitrary rank
Up to now, we have encountered tensor fields
- of type (0,0) (i.e. scalar fields),
- of type (1,0) (i.e. vector fields),
- of type (0,1) (i.e. 1-forms),
- of type (0,2) and antisymmetric (i.e. 2-forms).
More generally, tensor fields of any type can be introduced in SageManifolds. For instance a tensor field of type (1,2) on the open subset is declared as follows:
As for vectors or 1-forms, the tensor's components with respect to the domain's default frame are set by means of square brackets:
Unset components are zero:
Display of the nonzero components:
Double square brackets return the component (still w.r.t. the default frame) as a scalar field, while single square brackets return the expression of this scalar field in terms of the domain's default coordinates:
A tensor field of type (1,2) maps a 3-tuple (1-form, vector field, vector field) to a scalar field:
As for vectors and differential forms, the tensor components can be taken in any frame defined on the manifold:
Tensor calculus
The tensor product is denoted by *:
The tensor product preserves the (anti)symmetries: since is a 2-form, it is antisymmetric with respect to its two arguments (positions 0 and 1); as a result, b is antisymmetric with respect to its last two arguments (positions 1 and 2):
Standard tensor arithmetics is implemented:
Tensor contractions are dealt with by the methods trace() and contract(): for instance, let us contract the tensor w.r.t. its first two arguments (positions 0 and 1), i.e. let us form the tensor of components :
An alternative to the writing trace(0,1) is to use the index notation to denote the contraction: the indices are given in a string inside the [] operator, with '^' in front of the contravariant indices and '_' in front of the covariant ones:
The contraction is performed on the repeated index (here k); the letter denoting the remaining index (here i) is arbitrary:
It can even be replaced by a dot:
LaTeX notations are allowed:
The contraction of the tensor fields and is taken as follows (2 refers to the last index position of and 0 to the only index position of v):
Since 2 corresponds to the last index position of and 0 to the first index position of , a shortcut for the above is
Instead of contract(), the index notation, combined with the * operator, can be used to denote the contraction:
The non-repeated indices can be replaced by dots:
Metric structures
A Riemannian metric on the manifold is declared as follows:
It is a symmetric tensor field of type (0,2):
The metric is initialized by its components with respect to some vector frame. For instance, using the default frame of :
The components w.r.t. another vector frame are obtained as for any tensor field:
Of course, the metric acts on vector pairs:
The Levi-Civita connection associated to the metric :
The Christoffel symbols with respect to the manifold's default coordinates:
The Christoffel symbols with respect to the coordinates :
A nice view is obtained via the method display() (by default, only the nonzero connection coefficients are shown):
The connection acting as a covariant derivative:
Being a Levi-Civita connection, is torsion.free:
In the present case, it is also flat:
Let us consider a non-flat metric, by changing to :
For convenience, we change the default chart on the domain to Y=:
In this way, we do not have to specify Y when asking for coordinate expressions in terms of :
We recognize the metric of the hyperbolic space . Its expression in terms of the chart is
A matrix view of the components may be more appropriate:
We extend these components, a priori defined only on , to the whole manifold , by demanding the same coordinate expressions in the frame associated to the chart X=:
The Levi-Civita connection is automatically recomputed, after the change in :
In particular, the Christoffel symbols are different:
The Riemann tensor is now
Note that it can be accessed directely via the metric, without any explicit mention of the connection:
The Ricci tensor is
The Weyl tensor is:
The Weyl tensor vanishes identically because the dimension of is 3.
Finally, the Ricci scalar is
We recover the fact that is a Riemannian manifold of constant negative curvature.
Tensor transformations induced by a metric
The most important tensor transformation induced by the metric is the so-called musical isomorphism, or index raising and index lowering:
Raising the last index of with :
Raising all the covariant indices of (i.e. those at the positions 1 and 2):
Hodge duality
The volume 3-form (Levi-Civita tensor) associated with the metric is
We check the classical formula , or, more precisely, (for is defined on only):
The Hodge dual of a 1-form is a 2-form:
The Hodge dual of a 2-form is a 1-form:
Finally, the Hodge dual of a 3-form is a 0-form:
In dimension 3 and for a Riemannian metric, the Hodge star is idempotent:
Getting help
To get the list of functions (methods) that can be called on a object, type the name of the object, followed by a dot and the TAB key, e.g.
sa.<tab>
To get information on an object or a method, use the question mark:
Using a double question mark leads directly to the Python source code (Sage and SageManifolds are open source, aren't they ?)
Going further
Have a look at the examples on SageManifolds page, especially the 2-dimensional sphere example for usage on a non-parallelizable manifold (each scalar field has to be defined in at least two coordinate charts, the module is no longer free and each tensor field has to be defined in at least two vector frames).