# Vector calculus with SageMath¶

## Part 1: Using Cartesian 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 [1]:
version()

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

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

In [2]:
%display latex


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

In [3]:
from sage.manifolds.operators import *


## The 3-dimensional Euclidean space¶

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

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

Euclidean space E^3

Out[4]:

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

In [5]:
E.atlas()

Out[5]:

as well as with the associated orthonormal vector frame:

In [6]:
E.frames()

Out[6]:

## Vector fields¶

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

In [7]:
v = E.vector_field(-y, x, sin(x*y*z), name='v')
v.display()

Out[7]:

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

In [8]:
v[1]

Out[8]:
In [9]:
v[:]

Out[9]:

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

In [10]:
p = E((3,-2,1), name='p')
print(p)

Point p on the Euclidean space E^3

In [11]:
p.coordinates()

Out[11]:
In [12]:
vp = v.at(p)
print(vp)

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

In [13]:
vp.display()

Out[13]:

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

In [14]:
v.plot(max_range=1.5, scale=0.5, viewer='threejs', online=True)

Out[14]:

We may define a vector field with generic components:

In [15]:
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[15]:
In [16]:
u[:]

Out[16]:
In [17]:
up = u.at(p)
up.display()

Out[17]:

## 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 [18]:
s = u.dot(v)
s

Out[18]:
In [19]:
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 [20]:
s.display()

Out[20]:

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

In [21]:
s(p)

Out[21]:

Its coordinate expression is

In [22]:
s.expr()

Out[22]:

### Norm¶

The norm of a vector field is

In [23]:
s = norm(u)
s

Out[23]:
In [24]:
s.display()

Out[24]:
In [25]:
s.expr()

Out[25]:

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

In [26]:
norm(u)^2 == u.dot(u)

Out[26]:

For $v$, we have:

In [27]:
norm(v).expr()

Out[27]:

### Cross product¶

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

In [28]:
s = u.cross(v)
print(s)

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

In [29]:
s.display()

Out[29]:

### 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 [30]:
w = E.vector_field(name='w')
w[1] = x*z
w[2] = y*z
w.display()

Out[30]:

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

In [31]:
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 [32]:
s.expr()

Out[32]:

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

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

Out[33]:

## 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 $(x,y,z)$:

In [34]:
F = E.scalar_field(function('f')(x,y,z), name='F')
F.display()

Out[34]:

The value of $F$ at a point:

In [35]:
F(p)

Out[35]:

The gradient of $F$:

In [36]:
print(grad(F))

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

In [37]:
grad(F).display()

Out[37]:
In [38]:
norm(grad(F)).display()

Out[38]:

### Divergence¶

The divergence of a vector field:

In [39]:
s = div(u)
s.display()

Out[39]:

For $v$ and $w$, we have

In [40]:
div(v).expr()

Out[40]:
In [41]:
div(w).expr()

Out[41]:

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

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

Out[42]:

### Curl¶

The curl of a vector field:

In [43]:
s = curl(u)
print(s)

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

In [44]:
s.display()

Out[44]:

To use the notation rot instead of curl, simply do

In [45]:
rot = curl


An alternative is

In [46]:
from sage.manifolds.operators import curl as rot


We have then

In [47]:
rot(u).display()

Out[47]:
In [48]:
rot(u) == curl(u)

Out[48]:

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

In [49]:
curl(v).display()

Out[49]:
In [50]:
curl(w).display()

Out[50]:

The curl of a gradient is always zero:

In [51]:
curl(grad(F)).display()

Out[51]:

The divergence of a curl is always zero:

In [52]:
div(curl(u)).display()

Out[52]:

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

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

Out[53]:

### Laplacian¶

The Laplacian of a scalar field:

In [54]:
s = laplacian(F)
s.display()

Out[54]:

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

In [55]:
laplacian(F) == div(grad(F))

Out[55]:

The Laplacian of a vector field:

In [56]:
laplacian(u).display()

Out[56]:

In the Cartesian frame, the components of the Laplacian of a vector field are nothing but the Laplacians of the components of the vector field, as we can check:

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

Out[57]:

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 [58]:
e[:]

Out[58]:

For $v$ and $w$, we have

In [59]:
laplacian(v).display()

Out[59]:
In [60]:
laplacian(w).display()

Out[60]:

We have:

In [61]:
curl(curl(u)).display()

Out[61]:
In [62]:
grad(div(u)).display()

Out[62]:

and we may check a famous identity:

In [63]:
curl(curl(u)) == grad(div(u)) - laplacian(u)

Out[63]:

## Customizations¶

### 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 [64]:
frame = E.cartesian_frame()
frame

Out[64]:

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

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

Out[65]:
In [66]:
v.display()

Out[66]:
In [67]:
frame.set_name(('hx', 'hy', 'hz'),
latex_symbol=(r'\mathbf{\hat{x}}', r'\mathbf{\hat{y}}',
r'\mathbf{\hat{z}}'))
frame

Out[67]:
In [68]:
v.display()

Out[68]:

### Customizing the coordinate symbols¶

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

In [69]:
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 [70]:
E.<X,Y,Z> = EuclideanSpace()


We have then:

In [71]:
E.atlas()

Out[71]:
In [72]:
E.cartesian_frame()

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

Out[73]:

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 [74]:
E.<xi,et,ze> = EuclideanSpace(symbols=r"xi:\xi et:\eta ze:\zeta")
E.atlas()

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

Out[75]: