# 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 (version 1.3, as included in SageMath 8.3).

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:

In :
version()

Out:
'SageMath version 8.3, Release Date: 2018-08-03'

First we set up the notebook to display math formulas using LaTeX formatting:

In :
%display latex


and we import the standard operators grad, div, curl, etc. involved in vector calculus:

In :
from sage.manifolds.operators import *


## The 3-dimensional Euclidean space¶

We start by declaring the 3-dimensional Euclidean space $\mathbb{E}^3$, with $(\rho,\phi,z)$ as spherical coordinates:

In :
E.<rh,ph,z> = EuclideanSpace(coordinates='cylindrical')
print(E)
E

Euclidean space E^3

Out:

$\mathbb{E}^3$ is endowed with the chart of cylindrical coordinates:

In :
E.atlas()

Out:

as well as with the associated orthonormal vector frame $(e_\rho, e_\phi, e_z)$:

In :
E.frames()

Out:

In the above output, $\left(\frac{\partial}{\partial\rho}, \frac{\partial}{\partial\phi}, \frac{\partial}{\partial z}\right)$ is the coordinate frame associated with $(\rho,\phi,z)$; it is not an orthonormal frame and will not be used below.

## Vector fields¶

We define a vector field on $\mathbb{E}^3$ from its components in the orthonormal vector frame $(e_\rho,e_\phi,e_z)$:

In :
v = E.vector_field(rh*(1+sin(2*ph)), 2*rh*cos(ph)^2, z,
name='v')
v.display()

Out:

We can access to the components of $v$ via the square bracket operator:

In :
v

Out:
In :
v[:]

Out:

A vector field can evaluated at any point of $\mathbb{E}^3$:

In :
p = E((1, pi, 0), name='p')
print(p)

Point p on the Euclidean space E^3

In :
p.coordinates()

Out:
In :
vp = v.at(p)
print(vp)

Vector v at Point p on the Euclidean space E^3

In :
vp.display()

Out:

We may define a vector field with generic components:

In :
u = E.vector_field(function('u_rho')(rh,ph,z),
function('u_phi')(rh,ph,z),
function('u_z')(rh,ph,z),
name='u')
u.display()

Out:
In :
u[:]

Out:
In :
up = u.at(p)
up.display()

Out:

## Algebraic operations on vector fields¶

### Dot product¶

The dot (or scalar) product of the vector fields $u$ and $v$ is obtained by the method dot_product, which admits dot as a shortcut alias:

In :
s = u.dot(v)
s

Out:
In :
print(s)

Scalar field u.v on the Euclidean space E^3


$s= u\cdot v$ is a scalar field, i.e. a map $\mathbb{E}^3 \rightarrow \mathbb{R}$:

In :
s.display()

Out:

It maps point of $\mathbb{E}^3$ to real numbers:

In :
s(p)

Out:

Its coordinate expression is

In :
s.expr()

Out:

### Norm¶

The norm of a vector field is

In :
s = norm(u)
s

Out:
In :
s.display()

Out:
In :
s.expr()

Out:

The norm is related to the dot product by $\|u\|^2 = u\cdot u$, as we can check:

In :
norm(u)^2 == u.dot(u)

Out:

For $v$, we have:

In :
norm(v).expr()

Out:

### Cross product¶

The cross product of $u$ by $v$ is obtained by the method cross_product, which admits cross as a shortcut alias:

In :
s = u.cross(v)
print(s)

Vector field u x v on the Euclidean space E^3

In :
s.display()

Out:

### 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 $u$ and $v$; instead, we set them in a second stage, via the square bracket operator, any unset component being assumed to be zero:

In :
w = E.vector_field(name='w')
w = rh
w = z
w.display()

Out:

The scalar triple product of the vector fields $u$, $v$ and $w$ is obtained as follows:

In :
triple_product = E.scalar_triple_product()
s = triple_product(u, v, w)
print(s)

Scalar field epsilon(u,v,w) on the Euclidean space E^3

In :
s.expr()

Out:

Let us check that the scalar triple product of $u$, $v$ and $w$ is $u\cdot(v\times w)$:

In :
s == u.dot(v.cross(w))

Out:

## Differential operators¶

### 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 $(\rho,\phi,z)$:

In :
F = E.scalar_field(function('f')(rh,ph,z), name='F')
F.display()

Out:

The value of $F$ at a point:

In :
F(p)

Out:

The gradient of $F$:

In :
print(grad(F))

Vector field grad(F) on the Euclidean space E^3

In :
grad(F).display()

Out:
In :
norm(grad(F)).display()

Out:

### Divergence¶

The divergence of a vector field:

In :
s = div(u)
s.display()

Out:
In :
s.expr().expand()

Out:

For $v$ and $w$, we have

In :
div(v).expr()

Out:
In :
div(w).expr()

Out:

An identity valid for any scalar field $F$ and any vector field $u$:

In :
div(F*u) == F*div(u) + u.dot(grad(F))

Out:

### Curl¶

The curl of a vector field:

In :
s = curl(u)
print(s)

Vector field curl(u) on the Euclidean space E^3

In :
s.display()

Out:

To use the notation rot instead of curl, simply do

In :
rot = curl


An alternative is

In :
from sage.manifolds.operators import curl as rot


We have then

In :
rot(u).display()

Out:
In :
rot(u) == curl(u)

Out:

For $v$ and $w$, we have:

In :
curl(v).display()

Out:
In :
curl(w).display()

Out:

The curl of a gradient is always zero:

In :
curl(grad(F)).display()

Out:

The divergence of a curl is always zero:

In :
div(curl(u)).display()

Out:

An identity valid for any scalar field $F$ and any vector field $u$:

In :
curl(F*u) == grad(F).cross(u) + F*curl(u)

Out:

### Laplacian¶

The Laplacian of a scalar field:

In :
s = laplacian(F)
s.display()

Out:
In :
s.expr().expand()

Out:

For a scalar field, the Laplacian is nothing but the divergence of the gradient:

In :
laplacian(F) == div(grad(F))

Out:

The Laplacian of a vector field:

In :
Du = laplacian(u)
Du.display()

Out:

Since this expression is quite lengthy, we may ask for a display component by component:

In :
Du.display_comp()

Out:

We may expand each component:

In :
for i in E.irange():
Du[i].expand()
Du.display_comp()

Out:
In :
Du

Out:
In :
Du

Out:
In :
Du

Out:

As a test, we may check that these formulas coincide with those of Wikipedia's article Del in cylindrical and spherical coordinates.

For $v$ and $w$, we have

In :
laplacian(v).display()

Out:
In :
laplacian(w).display()

Out:

We have:

In :
curl(curl(u)).display()

Out:
In :
grad(div(u)).display()

Out:

and we may check a famous identity:

In :
curl(curl(u)) == grad(div(u)) - laplacian(u)

Out:

## Customizations¶

### Customizing the symbols of the orthonormal frame vectors¶

By default, the vectors of the orthonormal frame associated with cylindrical coordinates are denoted $(e_\rho,e_\phi,z)$:

In :
frame = E.cylindrical_frame()
frame

Out:

But this can be changed, thanks to the method set_name:

In :
frame.set_name('a', indices=('rh', 'ph', 'z'),
latex_indices=(r'\rho', r'\phi', 'z'))
frame

Out:
In :
v.display()

Out:
In :
frame.set_name(('hrh', 'hph', 'hz'),
latex_symbol=(r'\hat{\rho}', r'\hat{\phi}', r'\hat{z}'))
frame

Out:
In :
v.display()

Out:

### Customizing the coordinate symbols¶

The coordinates symbols are defined within the angle brackets <...> at the construction of the Euclidean space. Above we did

In :
E.<rh,ph,z> = EuclideanSpace(coordinates='cylindrical')


which resulted in the coordinate symbols $(\rho,\phi,z)$ and in the corresponding Python variables rh, ph and z (SageMath symbolic expressions). Using other symbols, for instance $(R,\Phi,Z)$, 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 (:):

In :
E.<R,Ph,Z> = EuclideanSpace(coordinates='cylindrical', symbols=r'R Ph:\Phi Z')


We have then

In :
E.atlas()

Out:
In :
E.frames()

Out:
In :
E.cylindrical_frame()

Out:
In :
v = E.vector_field(R*(1+sin(2*Ph)), 2*R*cos(Ph)^2, Z,
name='v')
v.display()

Out: