# Vector calculus with SageMath¶

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

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

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

In :
%display latex


## Cartesian coordinates¶

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

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

Euclidean space E^3

Out:

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

In :
E.atlas()

Out:

Let us denote by cartesian the chart of Cartesian coordinates:

In :
cartesian = E.cartesian_coordinates()
cartesian

Out:

The access to the individual coordinates is performed via the square bracket operator:

In :
cartesian

Out:
In :
cartesian[:]

Out:

Thanks to use of <x,y,z> when declaring E, the Python variables x, y and z have been created (i.e. there is no need to declare them by something like y = var('y')); they represent the coordinates $(x,y,z)$ as symbolic expressions:

In :
y is cartesian

Out:
In :
type(y)

Out:

Each of the Cartesian coordinates spans the entire real line:

In :
cartesian.coord_range()

Out:

Being the only coordinate chart created so far, cartesian is the default chart on E:

In :
cartesian is E.default_chart()

Out:

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

In :
E.frames()

Out:

Let us denote it by cartesian_frame:

In :
cartesian_frame = E.cartesian_frame()
cartesian_frame

Out:
In :
cartesian_frame is E.default_frame()

Out:

Each element of this frame is a unit vector field; for instance, we have $e_x\cdot e_x = 1$:

In :
e_x = cartesian_frame
e_x

Out:
In :
e_x.dot(e_x).expr()

Out:

as well as $e_x\cdot e_y = 0$:

In :
e_y = cartesian_frame
e_x.dot(e_y).expr()

Out:

## Spherical coordinates¶

Spherical coordinates are introduced by

In :
spherical.<r,th,ph> = E.spherical_coordinates()
spherical

Out:
In :
spherical[:]

Out:
In :
spherical.coord_range()

Out:

$\mathbb{E}^3$ is now endowed with two coordinate charts:

In :
E.atlas()

Out:

The change-of-coordinate formulas have been automatically implemented during the above call E.spherical_coordinates():

In :
E.coord_change(spherical, cartesian).display()

Out:
In :
E.coord_change(cartesian, spherical).display()

Out:

$\mathbb{E}^3$ is also now endowed with three vector frames:

In :
E.frames()

Out:

The second one is the coordinate frame of spherical coordinates, while the third one is the standard orthonormal frame associated with spherical coordinates. For Cartesian coordinates, the coordinate frame and the orthonormal frame coincide: it is $(e_x,e_y,e_z)$. For spherical coordinates, the orthonormal frame is denoted $(e_r,e_\theta,e_\phi)$ and is returned by the method spherical_frame():

In :
spherical_frame = E.spherical_frame()
spherical_frame

Out:

We may check that it is an orthonormal frame:

In :
es = spherical_frame
[[es[i].dot(es[j]).expr() for j in E.irange()] for i in E.irange()]

Out:

The orthonormal spherical frame expressed in terms of the Cartesian one:

In :
for vec in spherical_frame:
show(vec.display(cartesian_frame, spherical))


The reverse transformation:

In :
for vec in cartesian_frame:
show(vec.display(spherical_frame, spherical))


The orthonormal frame $(e_r,e_\theta,e_\phi)$ expressed in terms on the coordinate frame $\left(\frac{\partial}{\partial r}, \frac{\partial}{\partial\theta}, \frac{\partial}{\partial \phi}\right)$ (the latter being returned by the method frame() acting on the chart spherical):

In :
for vec in spherical_frame:
show(vec.display(spherical.frame(), spherical))


## Cylindrical coordinates¶

Cylindrical coordinates are introduced by

In :
cylindrical.<rh,ph,z> = E.cylindrical_coordinates()
cylindrical

Out:
In :
cylindrical[:]

Out:
In :
rh is cylindrical

Out:
In :
cylindrical.coord_range()

Out:

$\mathbb{E}^3$ is now endowed with two coordinate charts:

In :
E.atlas()

Out:

The transformations linking the cylindrical coordinates to the Cartesian ones are

In :
E.coord_change(cylindrical, cartesian).display()

Out:
In :
E.coord_change(cartesian, cylindrical).display()

Out:

$\mathbb{E}^3$ is also now endowed with five vector frames:

In :
E.frames()

Out:

The orthonormal frame associated with cylindrical coordinates is $(e_\rho, e_\phi, e_z)$:

In :
cylindrical_frame = E.cylindrical_frame()
cylindrical_frame

Out:

We may check that it is an orthonormal frame:

In :
ec = cylindrical_frame
[[ec[i].dot(ec[j]).expr() for j in E.irange()] for i in E.irange()]

Out:

The orthonormal cylindrical frame expressed in terms of the Cartesian one:

In :
for vec in cylindrical_frame:
show(vec.display(cartesian_frame, cylindrical))


The reverse transformation:

In :
for vec in cartesian_frame:
show(vec.display(cylindrical_frame, cylindrical))


The orthonormal cylindrical frame expressed in terms of the spherical one:

In :
for vec in cylindrical_frame:
show(vec.display(spherical_frame, spherical))


The reverse transformation:

In :
for vec in spherical_frame:
show(vec.display(cylindrical_frame, spherical))


The orthonormal frame $(e_\rho,e_\phi,e_z)$ expressed in terms on the coordinate frame $\left(\frac{\partial}{\partial\rho}, \frac{\partial}{\partial\phi}, \frac{\partial}{\partial z}\right)$ (the latter being returned by the method frame() acting on the chart cylindrical):

In :
for vec in cylindrical_frame:
show(vec.display(cylindrical.frame(), cylindrical))


## Coordinates of a point¶

We introduce a point $p\in \mathbb{E}^3$ via the generic SageMath syntax for creating an element from its parent (here $\mathbb{E}^3$), i.e. the call operator (), with the coordinates of the point as the first argument:

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

Point p on the Euclidean space E^3


Actually, since the Cartesian coordinates are the default ones, the above writting is equivalent to

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

Point p on the Euclidean space E^3


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:

An example of point defined from its spherical coordinates:

In :
q = E((4,pi/3,pi), chart=spherical, name='q')
print(q)

Point q on the Euclidean space E^3

In :
spherical(q)

Out:
In :
cartesian(q)

Out:
In :
cylindrical(q)

Out:

## Expressions of a scalar field in various coordinate systems¶

Let us define a scalar field on $\mathbb{E}^3$ from its expression in Cartesian coordinates:

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


Note that since the Cartesian coordinates are the default ones, we did not specify them in the above definition. Thanks to the known coordinate transformations, the expression of $f$ in terms of other coordinates is automatically computed:

In :
f.display()

Out:

We can limit the output to a single coordinate system:

In :
f.display(cartesian)

Out:
In :
f.display(cylindrical)

Out:

The coordinate expression in a given coordinate system is obtained via the method expr()

In :
f.expr()  # expression in the default chart (Cartesian coordinates)

Out:
In :
f.expr(spherical)

Out:
In :
f.expr(cylindrical)

Out:

The value of $f$ at points $p$ and $q$:

In :
f(p)

Out:
In :
f(q)

Out:

Of course, we may define a scalar field from its coordinate expression in a chart that is not the default one:

In :
g = E.scalar_field(r^2, chart=spherical, name='g')


Instead of using the keyword argument chart, one can pass a dictionary as the first argument, with the chart as key:

In :
g = E.scalar_field({spherical: r^2}, name='g')

In :
g.display()

Out:

## Expression of a vector field in various frames¶

Let us introduce a vector field on $\mathbb{E}^3$ by its components in the Cartesian frame. Since the latter is the default vector frame on $\mathbb{E}^3$, it suffices to write:

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

Out:

Equivalently, a vector field can be defined directly from its expansion on the Cartesian frame:

In :
ex, ey, ez = cartesian_frame[:]
v = -y*ex + x*ey + z^2*ez
v.display()

Out:

Let us provide v with some name, as above:

In :
v.set_name('v')
v.display()

Out:

The components of $v$ are returned by the square bracket operator:

In :
v

Out:
In :
v[:]

Out:

The expression of $v$ in terms of the orthonormal spherical frame is obtained by

In :
v.display(spherical_frame)

Out:

We note that the components are still expressed in the default chart (Cartesian coordinates). To have them expressed in the spherical chart, it suffices to pass the latter as a second argument to display():

In :
v.display(spherical_frame, spherical)

Out:

Again, the components of $v$ are obtained by means of the square bracket operator, by specify the vector frame as first argument, and the coordinate chart as the last one:

In :
v[spherical_frame,1]

Out:
In :
v[spherical_frame,1,spherical]

Out:
In :
v[spherical_frame,:,spherical]

Out:

Similarly, the expression of $v$ in terms of the cylindrical frame is

In :
v.display(cylindrical_frame, cylindrical)

Out:
In :
v[cylindrical_frame,:,cylindrical]

Out:

The value of the vector field $v$ at point $p$:

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

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

In :
vp.display()

Out:
In :
vp.display(spherical_frame.at(p))

Out:
In :
vp.display(cylindrical_frame.at(p))

Out:

The value of the vector field $v$ at point $q$:

In :
vq = v.at(q)
print(vq)

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

In :
vq.display()

Out:
In :
vq.display(spherical_frame.at(q))

Out:
In :
vq.display(cylindrical_frame.at(q))

Out:

## Changing the default coordinates and default vector frame¶

At any time, one may change the default coordinates by the method set_default_chart:

In :
E.set_default_chart(spherical)


Then

In :
f.expr()

Out:
In :
v.display()

Out:

Note that the default vector frame is still the Cartesian one; to change to the orthonormal spherical frame, we use

In :
E.set_default_frame(spherical_frame)


Then

In :
v.display()

Out:
In :
v.display(cartesian_frame, cartesian)

Out: