# 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 :
version()

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


## The Euclidean 3-space¶

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

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

Euclidean space E^3

Out:

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

In :
E.category()

Out:
In :
print(E.category())

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

In :
E.base_field() is RR

Out:

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 :
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 :
E.atlas()

Out:

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

In :
E.frames()

Out:

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 :
spherical_frame = E.spherical_frame()
spherical_frame

Out:
In :
cylindrical_frame = E.cylindrical_frame()
cylindrical_frame

Out:

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

In :
spherical.frame()

Out:
In :
cylindrical.frame()

Out:

## 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 :
cartesian = E.cartesian_coordinates()
cartesian

Out:

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

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

Point p on the Euclidean space E^3

In :
p.parent() is E

Out:

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

In :
cartesian(p)

Out:
In :
spherical(p)

Out:
In :
cylindrical(p)

Out:

## Riemannian metric¶

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

In :
g = E.metric()
print(g)

Riemannian metric g on the Euclidean space E^3

In :
g.display()

Out:
In :
g[:]

Out:

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 :
g.display(spherical_frame)

Out:
In :
g[spherical_frame,:]

Out:

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 :
spherical_frame.coframe()

Out:

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 :
g.display(spherical.frame())

Out:

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 :
g.display(spherical.frame(), spherical)

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

Out:

Similarly, for cylindrical coordinates, we have:

In :
g.display(cylindrical_frame)

Out:
In :
g.display(cylindrical.frame(), cylindrical)

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

Out:

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

In :
print(g.riemann())

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

In :
g.riemann().display()

Out:

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

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

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

Out:
In :
u.dot(v) == g(u,v)

Out:
In :
norm(u) == sqrt(g(u,u))

Out:

### 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 :
epsilon = E.scalar_triple_product()
print(epsilon)

3-form epsilon on the Euclidean space E^3

In :
epsilon is E.volume_form()

Out:
In :
epsilon.display()

Out:
In :
epsilon.display(spherical.frame(), spherical)

Out:
In :
epsilon.display(cylindrical.frame(), cylindrical)

Out:

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

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

Out:
In :
epsilon(*spherical_frame)

Out:
In :
epsilon(*spherical_frame).display()

Out:
In :
epsilon(*cylindrical_frame).display()

Out:

## Vector fields as derivatives¶

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

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

Out:

Vector fields acts as derivative on scalar fields:

In :
print(v(f))

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

In :
v(f).display()

Out:
In :
v(f) == v.dot(f.gradient())

Out:
In :
df = f.differential()
print(df)

1-form df on the Euclidean space E^3

In :
df.display()

Out:
In :
v(f) == df(v)

Out:

## 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 :
CE = E.scalar_field_algebra()
CE

Out:
In :
CE.category()

Out:
In :
f in CE

Out:

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

In :
f.parent() is CE

Out:

## 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 :
XE = v.parent()
print(XE)
XE

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

Out:
In :
print(XE.category())

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

In :
print(XE.base_ring())
XE.base_ring()

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

Out:
In :
XE.base_ring() is CE

Out:
In :
rank(XE)

Out:

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

In :
XE.bases()

Out:

## Tangent spaces¶

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

In :
print(p)

Point p on the Euclidean space E^3

In :
vp = v.at(p)
vp.display()

Out:
In :
Tp = vp.parent()
print(Tp)
Tp

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

Out:
In :
print(Tp.category())

Category of finite dimensional vector spaces over Symbolic Ring

In :
dim(Tp)

Out:
In :
isinstance(Tp, FiniteRankFreeModule)

Out:

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

In :
Tp.bases()

Out:

For instance, we have:

In :
spherical_frame.at(p)

Out:
In :
spherical_frame.at(p) in Tp.bases()

Out:

## Levi-Civita connection¶

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

In :
nabla = g.connection()
print(nabla)
nabla

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

Out:

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 :
g.christoffel_symbols_display(cartesian)

Out:

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

In :
g.christoffel_symbols_display(spherical)

Out:

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 :
g.christoffel_symbols_display(cylindrical)

Out:

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

In :
nabla.display(cylindrical.frame(), cylindrical)

Out:

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

In :
nabla.display(spherical_frame, spherical)

Out:
In :
nabla.display(cylindrical_frame, cylindrical)

Out:

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

In :
from sage.manifolds.operators import *

In :
grad(f) == nabla(f).up(g)

Out:
In :
nabla(f) == grad(f).down(g)

Out:
In :
div(u) == nabla(u).trace()

Out:
In :
div(v) == nabla(v).trace()

Out:
In :
laplacian(f) == nabla(nabla(f).up(g)).trace()

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

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

Out: