# How to compute a gradient, a divergence or a curl?¶

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:

In :
version()

Out:
'SageMath version 9.0, Release Date: 2020-01-01'

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

In :
%display latex


## First stage: introduce the Euclidean 3-space¶

Before evaluating some vector-field operators, one needs to define the arena in which vector fields live, namely the 3-dimensional Euclidean space $\mathbb{E}^3$. In SageMath, we declare it, along with the standard Cartesian coordinates $(x,y,z)$, as follows:

In :
E.<x,y,z> = EuclideanSpace()
print(E)
E

Euclidean space E^3

Out:

Thanks to the notation <x,y,z> in the above declaration, the coordinates $(x,y,z)$ are immediately available as three symbolic variables x, y and z (there is no need to type x, y, z = var('x y z')):

In :
x is E.cartesian_coordinates()

Out:
In :
y is E.cartesian_coordinates()

Out:
In :
z is E.cartesian_coordinates()

Out:
In :
type(z)

Out:

Besides, $\mathbb{E}^3$ is endowed with the orthonormal vector frame $(e_x, e_y, e_z)$ associated with Cartesian coordinates:

In :
E.frames()

Out:

At this stage, this is the default vector frame on $\mathbb{E}^3$ (being the only vector frame introduced so far):

In :
E.default_frame()

Out:

## Defining a vector field¶

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

In :
v = E.vector_field(-y, x, sin(x*y*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((3,-2,1), 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:

Vector fields can be plotted (see the list of options for customizing the plot)

In :
v.plot(max_range=1.5, scale=0.5)

Out:

We may define a vector field with generic components $(u_x, u_y, u_z)$:

In :
u = E.vector_field(function('u_x')(x,y,z),
function('u_y')(x,y,z),
function('u_z')(x,y,z),
name='u')
u.display()

Out:
In :
u[:]

Out:

Its value at the point $p$ is then

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

Out:

## How to compute various vector products?¶

### Dot product¶

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

In :
u.dot(v) == u*v + u*v + u*v

Out:

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

In :
s = u.dot(v)
s

Out:
In :
print(s)

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

In :
s.display()

Out:

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

In :
s(p)

Out:

Its coordinate expression is

In :
s.expr()

Out:

### Norm¶

The norm $\|u\|$ of the vector field $u$ is defined in terms of the dot product by $\|u\| = \sqrt{u\cdot u}$:

In :
norm(u) == sqrt(u.dot(u))

Out:

It is a scalar field on $\mathbb{E}^3$:

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

Scalar field |u| on the Euclidean space E^3

In :
s.display()

Out:
In :
s.expr()

Out:

For $v$, we have

In :
norm(v).expr()

Out:

### Cross product¶

The cross product $u\times 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:

We can check the standard formulas expressing the cross product in terms of the components:

In :
all([s == u*v - u*v,
s == u*v - u*v,
s == u*v - u*v])

Out:

### Scalar triple product¶

Let us introduce a third vector field, $w$ say. 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 = x*z
w = y*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:

## How to evaluate the standard differential operators?¶

While the standard operators $\mathrm{grad}$, $\mathrm{div}$, $\mathrm{curl}$, 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)):

In :
from sage.manifolds.operators import *


### Gradient of a scalar field¶

We first introduce a scalar field, via its expression in terms of Cartesian coordinates; in this example, we consider some unspecified function of $(x,y,z)$:

In :
F = E.scalar_field(function('f')(x,y,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 the vector field $u$:

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

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 the vector field $u$:

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$ is $$\mathrm{curl}(Fu) = \mathrm{grad}\, F\times u + F\, \mathrm{curl}\, u,$$ as we can check:

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

Out:

### Laplacian¶

The Laplacian $\Delta F$ of a scalar field $F$ is another scalar field:

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

Out:

The following identity holds: $$\Delta F = \mathrm{div}\left(\mathrm{grad}\, F\right)$$ as we can check:

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

Out:

The Laplacian $\Delta u$ of a vector field $u$ is another vector field:

In :
Du = laplacian(u)
print(Du)

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


whose components are

In :
Du.display()

Out:

In the Cartesian frame, the components of $\Delta u$ are nothing but the (scalar) Laplacians of the components of $u$, as we can check:

In :
e = E.cartesian_frame()
Du == sum(laplacian(u[[i]])*e[i] for i in E.irange())

Out:

In the above formula, u[[i]] return the $i$-th component of $u$ as a scalar field, while u[i] would have returned the coordinate expression of this scalar field; besides, e is the Cartesian frame:

In :
e[:]

Out:

For the vector fields $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:

A famous identity is

$$\mathrm{curl}\left(\mathrm{curl}\, u\right) = \mathrm{grad}\left(\mathrm{div}\, u\right) - \Delta u.$$

Let us check it:

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

Out:

## How to customize various symbols?¶

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

By default, the vectors of the orthonormal frame associated with Cartesian coordinates are denoted $(e_x,e_y,e_z)$:

In :
frame = E.cartesian_frame()
frame

Out:

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

In :
frame.set_name('a', indices=('x', 'y', 'z'))
frame

Out:
In :
v.display()

Out:
In :
frame.set_name(('hx', 'hy', 'hz'),
latex_symbol=(r'\mathbf{\hat{x}}', r'\mathbf{\hat{y}}',
r'\mathbf{\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.<x,y,z> = EuclideanSpace()


which resulted in the coordinate symbols $(x,y,z)$ and in the corresponding Python variables x, y and z (SageMath symbolic expressions). To use other symbols, for instance $(X,Y,Z)$, it suffices to create E as

In :
E.<X,Y,Z> = EuclideanSpace()


We have then:

In :
E.atlas()

Out:
In :
E.cartesian_frame()

Out:
In :
v = E.vector_field(-Y, X, sin(X*Y*Z), name='v')
v.display()

Out:

By default the LaTeX symbols of the coordinate coincide with the letters given within the angle brackets. But this can be adjusted through the optional argument symbols of the function EuclideanSpace, which 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.<xi,et,ze> = EuclideanSpace(symbols=r"xi:\xi et:\eta ze:\zeta")
E.atlas()

Out:
In :
v = E.vector_field(-et, xi, sin(xi*et*ze), name='v')
v.display()

Out: