Vector calculus with SageMath
Part 4: Changing coordinates
This notebook illustrates some vector calculus capabilities of SageMath within the 3-dimensional Euclidean space. The corresponding tools have been developed within the SageManifolds project.
Click here to download the notebook file (ipynb format). To run it, you must start SageMath with the Jupyter interface, via the command sage -n jupyter
NB: a version of SageMath at least equal to 8.3 is required to run this notebook:
First we set up the notebook to display math formulas using LaTeX formatting:
Cartesian coordinates
We start by declaring the 3-dimensional Euclidean space , with as Cartesian coordinates:
is endowed with the chart of Cartesian coordinates:
Let us denote by cartesian
the chart of Cartesian coordinates:
The access to the individual coordinates is performed via the square bracket operator:
Thanks to use of <x,y,z>
when declaring E
, the Python variables x
, y
and z
have been created (i.e. there is no need to declare them by something like y = var('y')
); they represent the coordinates as symbolic expressions:
Each of the Cartesian coordinates spans the entire real line:
Being the only coordinate chart created so far, cartesian
is the default chart on E
:
is endowed with the orthonormal vector frame associated with Cartesian coordinates:
Let us denote it by cartesian_frame
:
Each element of this frame is a unit vector field; for instance, we have :
as well as :
Spherical coordinates
Spherical coordinates are introduced by
We have
is now endowed with two coordinate charts:
The change-of-coordinate formulas have been automatically implemented during the above call E.spherical_coordinates()
:
is also now endowed with three vector frames:
The second one is the coordinate frame of spherical coordinates, while the third one is the standard orthonormal frame associated with spherical coordinates. For Cartesian coordinates, the coordinate frame and the orthonormal frame coincide: it is . For spherical coordinates, the orthonormal frame is denoted and is returned by the method spherical_frame()
:
We may check that it is an orthonormal frame:
The orthonormal spherical frame expressed in terms of the Cartesian one:
The reverse transformation:
The orthonormal frame expressed in terms on the coordinate frame (the latter being returned by the method frame()
acting on the chart spherical
):
Cylindrical coordinates
Cylindrical coordinates are introduced by
We have
is now endowed with three coordinate charts:
The transformations linking the cylindrical coordinates to the Cartesian ones are
is also now endowed with five vector frames:
The orthonormal frame associated with cylindrical coordinates is :
We may check that it is an orthonormal frame:
The orthonormal cylindrical frame expressed in terms of the Cartesian one:
The reverse transformation:
The orthonormal cylindrical frame expressed in terms of the spherical one:
The reverse transformation:
The orthonormal frame expressed in terms on the coordinate frame (the latter being returned by the method frame()
acting on the chart cylindrical
):
Coordinates of a point
We introduce a point via the generic SageMath syntax for creating an element from its parent (here ), i.e. the call operator ()
, with the coordinates of the point as the first argument:
Actually, since the Cartesian coordinates are the default ones, the above writting is equivalent to
The coordinates of in a given coordinate chart are obtained by letting the corresponding chart act on :
A point defined from its spherical coordinates:
Expressions of a scalar field in various coordinate systems
Let us define a scalar field on from its expression in Cartesian coordinates:
Note that since the Cartesian coordinates are the default ones, we did not specify them in the above definition. Thanks to the known coordinate transformations, the expression of in terms of other coordinates is automatically computed:
We can limit the output to a single coordinate system:
The coordinate expression in a given coordinate system is obtained via the method expr()
The value of at points and :
Of course, we may define a scalar field from its coordinate expression in a chart that is not the default one:
Instead of using the keyword argument chart
, one can pass a dictionary as the first argument, with the chart as key:
Expression of a vector field in various frames
Let us introduce a vector field on by its components in the Cartesian frame. Since the latter is the default vector frame on , it suffices to write:
Equivalently, a vector field can be defined directly from its expansion on the Cartesian frame:
Let us provide v
with some name, as above:
The components of are returned by the square bracket operator:
The expression of in terms of the orthonormal spherical frame is obtained by
We note that the components are still expressed in the default chart (Cartesian coordinates). To have them expressed in the spherical chart, it suffices to pass the latter as a second argument to display()
:
Again, the components of are obtained by means of the square bracket operator, by specify the vector frame as first argument, and the coordinate chart as the last one:
Similarly, the expression of in terms of the cylindrical frame is
The value of the vector field at point :
The value of the vector field at point :
Changing the default coordinates and default vector frame
At any time, one may change the default coordinates by the method set_default_chart
:
Then
Note that the default vector frame is still the Cartesian one; to change to the orthonormal spherical frame, we use
Then