# Vector calculus with SageMath¶

## 5. Advanced aspects: Euclidean spaces as Riemannian manifolds¶

This worksheet illustrates some functionalities regarding Euclidean spaces in SageMath, as introduced in Trac ticket #24623.

In [1]:
version()

Out[1]:
'SageMath version 8.2.rc4, Release Date: 2018-04-20'
In [2]:
%display latex


## The Euclidean 3-space¶

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

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

Euclidean space E^3

Out[3]:

$\mathbb{E}^3$ is actually a Riemannian manifold, i.e. a smooth real manifold endowed with a positive definite metric tensor:

In [4]:
E.category()

Out[4]:
In [5]:
print(E.category())

Category of smooth manifolds over Real Field with 53 bits of precision

In [6]:
E.base_field() is RR

Out[6]:

Actually RR is used here as a proxy for the real field (this should be replaced in the future, see the discussion at #24456) and the 53 bits of precision play of course no role for the symbolic computations.

Let us introduce spherical and cylindrical coordinates on $\mathbb{E}^3$:

In [7]:
spherical.<r,th,ph> = E.spherical_coordinates()
cylindrical.<rh,ph,z> = E.cylindrical_coordinates()


The user atlas of $\mathbb{E}^3$ has then three charts:

In [8]:
E.atlas()

Out[8]:

while there are five vector frames defined on $\mathbb{E}^3$:

In [9]:
E.frames()

Out[9]:

Indeed, there are two frames associated with each of the three coordinate systems: the coordinate frame (denoted with partial derivatives above) and an orthonormal frame, but for Cartesian coordinates, both frames coincide.

We get the orthonormal spherical and cylindrical frames by

In [10]:
spherical_frame = E.spherical_frame()
spherical_frame

Out[10]:
In [11]:
cylindrical_frame = E.cylindrical_frame()
cylindrical_frame

Out[11]:

On the other side, the coordinate frames are returned by the method frame() acting on the coordinate charts:

In [12]:
spherical.frame()

Out[12]:
In [13]:
cylindrical.frame()

Out[13]:

## Charts as maps $\mathbb{E}^3 \rightarrow \mathbb{R}^3$¶

The chart of Cartesian coordinates has been constructed at the declaration of E; let us denote it by cartesian:

In [14]:
cartesian = E.cartesian_coordinates()
cartesian

Out[14]:

Let us consider a point $p\in \mathbb{E}^3$, defined by its Cartesian coordinates:

In [15]:
p = E((-1, 1,0), chart=cartesian, name='p')
print(p)

Point p on the Euclidean space E^3

In [16]:
p.parent() is E

Out[16]:

The coordinates of $p$ in a given coordinate chart are obtained by letting the corresponding chart act on $p$:

In [17]:
cartesian(p)

Out[17]:
In [18]:
spherical(p)

Out[18]:
In [19]:
cylindrical(p)

Out[19]:

## Riemannian metric¶

The default metric tensor of $\mathbb{E}^3$ is

In [20]:
g = E.metric()
print(g)

Riemannian metric g on the Euclidean space E^3

In [21]:
g.display()

Out[21]:
In [22]:
g[:]

Out[22]:

The above display in performed in the default frame, which is the Cartesian one. Of course, we may ask for display with respect to other frames:

In [23]:
g.display(spherical_frame)

Out[23]:
In [24]:
g[spherical_frame,:]

Out[24]:

In the above display, $e^r$, $e^\theta$ and $e^\phi$ are the 1-forms defining the coframe dual to the orthonormal spherical frame $(e_r,e_\theta,e_\phi)$:

In [25]:
spherical_frame.coframe()

Out[25]:

The fact that the above metric components are either 0 or 1 reflect the orthonormality of the vector frame $(e_r,e_\theta,e_\phi)$. On the contrary, in the coordinate frame $\left(\frac{\partial}{\partial r}, \frac{\partial}{\partial\theta}, \frac{\partial}{\partial \phi}\right)$, which is not orthonormal, the components differ from 0 or 1:

In [26]:
g.display(spherical.frame())

Out[26]:

Note that the components are expressed in terms of the default chart, namely the Cartesian one. To have them displayed in terms of the spherical chart, we have to provide the latter as the second argument of the method display():

In [27]:
g.display(spherical.frame(), spherical)

Out[27]:
In [28]:
g[spherical.frame(),:, spherical]

Out[28]:

Similarly, for cylindrical coordinates, we have:

In [29]:
g.display(cylindrical_frame)

Out[29]:
In [30]:
g.display(cylindrical.frame(), cylindrical)

Out[30]:
In [31]:
g[cylindrical.frame(),:,cylindrical]

Out[31]:

The metric $g$ is a flat: its (Riemann) curvature tensor is zero:

In [32]:
print(g.riemann())

Tensor field Riem(g) of type (1,3) on the Euclidean space E^3

In [33]:
g.riemann().display()

Out[33]:

The metric $g$ is defining the dot product on $\mathbb{E}^3$:

In [34]:
u = E.vector_field(x*y, y*z, z*x)
u.display()

Out[34]:
In [35]:
v = E.vector_field(-y, x, z^2, name='v')
v.display()

Out[35]:
In [36]:
u.dot(v) == g(u,v)

Out[36]:
In [37]:
norm(u) == sqrt(g(u,u))

Out[37]:

### The Levi-Civita tensor¶

The scalar triple product of $\mathbb{E}^3$ is provided by the Levi-Civita tensor (also called volume form) associated with $g$ (and chosen such that $(e_x,e_y,e_z)$ is right-handed):

In [38]:
epsilon = E.scalar_triple_product()
print(epsilon)

3-form epsilon on the Euclidean space E^3

In [39]:
epsilon is E.volume_form()

Out[39]:
In [40]:
epsilon.display()

Out[40]:
In [41]:
epsilon.display(spherical.frame(), spherical)

Out[41]:
In [42]:
epsilon.display(cylindrical.frame(), cylindrical)

Out[42]:

Checking that all orthonormal frames introduced above are right-handed:

In [43]:
ex, ey, ez = E.cartesian_frame()[:]
epsilon(ex, ey, ez).display()

Out[43]:
In [44]:
epsilon(*spherical_frame)

Out[44]:
In [45]:
epsilon(*spherical_frame).display()

Out[45]:
In [46]:
epsilon(*cylindrical_frame).display()

Out[46]:

## Vector fields as derivatives¶

Let $f$ be a scalar field on $\mathbb{E}^3$:

In [47]:
f = E.scalar_field(x^2+y^2 - z^2, name='f')
f.display()

Out[47]:

Vector fields acts as derivative on scalar fields:

In [48]:
print(v(f))

Scalar field v(f) on the Euclidean space E^3

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

Out[49]:
In [50]:
v(f) == v.dot(f.gradient())

Out[50]:
In [51]:
df = f.differential()
print(df)

1-form df on the Euclidean space E^3

In [52]:
df.display()

Out[52]:
In [53]:
v(f) == df(v)

Out[53]:

## The algebra of scalar fields¶

The set $C^\infty(\mathbb{E}^3)$ of all smooth scalar fields on $\mathbb{E}^3$ forms a commutative algebra over $\mathbb{R}$:

In [54]:
CE = E.scalar_field_algebra()
CE

Out[54]:
In [55]:
CE.category()

Out[55]:
In [56]:
f in CE

Out[56]:

In SageMath terminology $C^\infty(\mathbb{E}^3)$ is the parent of scalar fields:

In [57]:
f.parent() is CE

Out[57]:

## The free module of vector fields¶

The set $\mathfrak{X}(\mathbb{E}^3)$ of all vector fields on $\mathbb{E}^3$ is a free module of rank 3 over the commutative algebra $C^\infty(\mathbb{E}^3)$:

In [58]:
XE = v.parent()
print(XE)
XE

Free module X(E^3) of vector fields on the Euclidean space E^3

Out[58]:
In [59]:
print(XE.category())

Category of finite dimensional modules over Algebra of differentiable scalar fields on the Euclidean space E^3

In [60]:
print(XE.base_ring())
XE.base_ring()

Algebra of differentiable scalar fields on the Euclidean space E^3

Out[60]:
In [61]:
XE.base_ring() is CE

Out[61]:
In [62]:
rank(XE)

Out[62]:

The bases of the free module $\mathfrak{X}(\mathbb{E}^3)$ are nothing but the vector frames defined on $\mathbb{E}^3$:

In [63]:
XE.bases()

Out[63]:

## Tangent spaces¶

Vector fields evaluated at a point are vectors in the tangent space at this point:

In [64]:
print(p)

Point p on the Euclidean space E^3

In [65]:
vp = v.at(p)
vp.display()

Out[65]:
In [66]:
Tp = vp.parent()
print(Tp)
Tp

Tangent space at Point p on the Euclidean space E^3

Out[66]:
In [67]:
print(Tp.category())

Category of finite dimensional vector spaces over Symbolic Ring

In [68]:
dim(Tp)

Out[68]:
In [69]:
isinstance(Tp, FiniteRankFreeModule)

Out[69]:

The bases on $T_p\mathbb{E}^3$ are inherited from the vector frames of $\mathbb{E}^3$:

In [70]:
Tp.bases()

Out[70]:

For instance, we have:

In [71]:
spherical_frame.at(p)

Out[71]:
In [72]:
spherical_frame.at(p) in Tp.bases()

Out[72]:

## Levi-Civita connection¶

The Levi-Civita connection associated to the Euclidean metric $g$ is

In [73]:
nabla = g.connection()
print(nabla)
nabla

Levi-Civita connection nabla_g associated with the Riemannian metric g on the Euclidean space E^3

Out[73]:

The corresponding Christoffel symbols with respect to Cartesian coordinates are identically zero: none of them appear in the output of christoffel_symbols_display, which by default displays only nonzero Christoffel symbols:

In [74]:
g.christoffel_symbols_display(cartesian)

Out[74]:

On the contrary, some of the Christoffel symbols with respect to spherical coordinates differ from zero:

In [75]:
g.christoffel_symbols_display(spherical)

Out[75]:

By default, only nonzero and nonredundant values are displayed (for instance $\Gamma^\phi_{\ \, \phi r}$ is skipped, since it can be deduced from $\Gamma^\phi_{\ \, r \phi}$ by symmetry on the last two indices).

Similarly, the nonzero Christoffel symbols with respect to cylindrical coordinates are

In [76]:
g.christoffel_symbols_display(cylindrical)

Out[76]:

The Christoffel symbols are nothing but the connection coefficient in the corresponding coordinate frame:

In [77]:
nabla.display(cylindrical.frame(), cylindrical)

Out[77]:

The connection coefficients with respect to the orthonormal (non-coordinate) frames are (again only nonzero values are displayed):

In [78]:
nabla.display(spherical_frame, spherical)

Out[78]:
In [79]:
nabla.display(cylindrical_frame, cylindrical)

Out[79]:

$\nabla_g$ is the connection involved in differential operators:

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

In [81]:
grad(f) == nabla(f).up(g)

Out[81]:
In [82]:
nabla(f) == grad(f).down(g)

Out[82]:
In [83]:
div(u) == nabla(u).trace()

Out[83]:
In [84]:
div(v) == nabla(v).trace()

Out[84]:
In [85]:
laplacian(f) == nabla(nabla(f).up(g)).trace()

Out[85]:
In [86]:
laplacian(u) == nabla(nabla(u).up(g)).trace(1,2)

Out[86]:
In [87]:
laplacian(v) == nabla(nabla(v).up(g)).trace(1,2)

Out[87]: