Vector calculus with SageMath

This worksheet illustrates the operators regarding scalar and vector fields on pseudo-Riemannian manifolds introduced in Trac ticket #24622.

Since SageMath 8.3, it is rather obsolete regarding calculus in Euclidean spaces. See these vector calculus examples instead.

In [1]:
version()
Out[1]:
'SageMath version 8.2.beta4, Release Date: 2018-01-27'
In [2]:
%display latex
In [3]:
from sage.manifolds.operators import *   # to get the operators grad, div, curl, etc.

The Euclidean space as a 3-dimensional Riemannian manifold

In [4]:
M = Manifold(3, 'M', structure='Riemannian', start_index=1)
X.<x,y,z> = M.chart()
In [5]:
g = M.metric()
g[1,1], g[2,2], g[3,3] = 1, 1, 1
g.display()
Out[5]:

Gradient of a scalar field

In [6]:
F = M.scalar_field(function('f')(x,y,z), name='F')
F.display()
Out[6]:
In [7]:
grad(F).display()
Out[7]:
In [8]:
curl(grad(F)).display()
Out[8]:
In [9]:
norm(grad(F)).display()
Out[9]:

Laplacien of a scalar field

In [10]:
laplacian(F).display()
Out[10]:
In [11]:
laplacian(F) == div(grad(F))
Out[11]:

Vector field

In [12]:
v = M.vector_field(name='v')
v[1] = function('v_x')(x,y,z)
v[2] = function('v_y')(x,y,z)
v[3] = function('v_z')(x,y,z)
v.display()
Out[12]:

In order not to clutter the outputs, we omit the coordinate arguments in the display of chart functions:

In [13]:
M.options.omit_function_arguments=True
In [14]:
v.display()
Out[14]:

Norm of a vector field:

In [15]:
s = norm(v)
print(s)
Scalar field |v| on the 3-dimensional Riemannian manifold M
In [16]:
s.display()
Out[16]:

Scalar product of two vector fields:

In [17]:
v.dot(grad(F)).display()
Out[17]:

Cross product of two vector fields:

In [18]:
v.cross(grad(F)).display()
Out[18]:

Divergence :

In [19]:
s = div(v)
s.display()
Out[19]:

Curl:

In [20]:
w = curl(v)
print(w)
Vector field curl(v) on the 3-dimensional Riemannian manifold M
In [21]:
w.display()
Out[21]:

To use the notation rot instead of curl, simply do

In [22]:
rot = curl

An alternative is

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

We have then

In [24]:
rot(v).display()
Out[24]:
In [25]:
rot(v) == curl(v)
Out[25]:

The divergence of a curl is always zero:

In [26]:
div(curl(v)).display()
Out[26]:

Laplacian of a vector field:

In [27]:
laplacian(v).display()
Out[27]:
In [28]:
curl(curl(v)).display()
Out[28]:
In [29]:
grad(div(v)).display()
Out[29]:

Check of a famous identity:

In [30]:
curl(curl(v)) == grad(div(v)) - laplacian(v)
Out[30]:

Two other identities regarding any scalar field F and any vector field v:

In [31]:
div(F*v) == F*div(v) + v.dot(grad(F))
Out[31]:
In [32]:
curl(F*v) == grad(F).cross(v) + F*curl(v)
Out[32]:

The left-hand side is

In [33]:
curl(F*v).display()
Out[33]: