Vector calculus with SageMath
Part 3: Using cylindrical 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:
The 3-dimensional Euclidean space
We start by declaring the 3-dimensional Euclidean space , with as cylindrical coordinates:
is endowed with the chart of cylindrical coordinates:
as well as with the associated orthonormal vector frame :
In the above output, is the coordinate frame associated with ; it is not an orthonormal frame and will not be used below.
Vector fields
We define a vector field on from its components in the orthonormal vector frame :
We can access to the components of via the square bracket operator:
A vector field can evaluated at any point of :
We may define a vector field with generic components:
Algebraic operations on vector fields
Dot product
The dot (or scalar) product of the vector fields and is obtained by the method dot_product
, which admits dot
as a shortcut alias:
is a scalar field, i.e. a map :
It maps points of to real numbers:
Its coordinate expression is
Norm
The norm of a vector field is
The norm is related to the dot product by , as we can check:
For , we have
Cross product
The cross product of by is obtained by the method cross_product
, which admits cross
as a shortcut alias:
Scalar triple product
Let us introduce a third vector field. As a example, we do not pass the components as arguments of vector_field
, as we did for and ; instead, we set them in a second stage, via the square bracket operator, any unset component being assumed to be zero:
The scalar triple product of the vector fields , and is obtained as follows:
Let us check that the scalar triple product of , and is :
Differential operators
While the standard operators , , , etc. involved in vector calculus are accessible via the dot notation (e.g. v.div()
), let us import functions grad
, div
, curl
, etc. that allow for using standard mathematical notations (e.g. div(v)
):
Gradient of a scalar field
We first introduce a scalar field, via its expression in terms of Cartesian coordinates; in this example, we consider a unspecified function of :
The value of at a point:
The gradient of :
Divergence
The divergence of a vector field:
For and , we have
An identity valid for any scalar field and any vector field :
Curl
The curl of a vector field:
To use the notation rot
instead of curl
, simply do
An alternative is
We have then
For and , we have
The curl of a gradient is always zero:
The divergence of a curl is always zero:
An identity valid for any scalar field and any vector field :
Laplacian
The Laplacian of a scalar field:
For a scalar field, the Laplacian is nothing but the divergence of the gradient:
The Laplacian of a vector field:
Since this expression is quite lengthy, we may ask for a display component by component:
We may expand each component:
As a test, we may check that these formulas coincide with those of Wikipedia's article Del in cylindrical and spherical coordinates.
For and , we have
We have
and we may check a famous identity:
Customizations
Customizing the symbols of the orthonormal frame vectors
By default, the vectors of the orthonormal frame associated with cylindrical coordinates are denoted :
But this can be changed, thanks to the method set_name
:
Customizing the coordinate symbols
The coordinates symbols are defined within the angle brackets <...>
at the construction of the Euclidean space. Above we did
which resulted in the coordinate symbols and in the corresponding Python variables rh
, ph
and z
(SageMath symbolic expressions). Using other symbols, for instance , is possible through the optional argument symbols
of the function EuclideanSpace
. It has to be a string, usually prefixed by r
(for raw string, in order to allow for the backslash character of LaTeX expressions). This string contains the coordinate fields separated by a blank space; each field contains the coordinate’s text symbol and possibly the coordinate’s LaTeX symbol (when the latter is different from the text symbol), both symbols being separated by a colon (:
):
We have then