# 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 [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


## Cartesian coordinates¶

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

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

Euclidean space E^3

Out[3]:

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

In [4]:
E.atlas()

Out[4]:

Let us denote by cartesian the chart of Cartesian coordinates:

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

Out[5]:

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

In [6]:
cartesian[1]

Out[6]:
In [7]:
cartesian[:]

Out[7]:

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 [8]:
y is cartesian[2]

Out[8]:
In [9]:
type(y)

Out[9]:

Each of the Cartesian coordinates spans the entire real line:

In [10]:
cartesian.coord_range()

Out[10]:

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

In [11]:
cartesian is E.default_chart()

Out[11]:

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

In [12]:
E.frames()

Out[12]:

Let us denote it by cartesian_frame:

In [13]:
cartesian_frame = E.cartesian_frame()
cartesian_frame

Out[13]:
In [14]:
cartesian_frame is E.default_frame()

Out[14]:

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

In [15]:
e_x = cartesian_frame[1]
e_x

Out[15]:
In [16]:
e_x.dot(e_x).expr()

Out[16]:

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

In [17]:
e_y = cartesian_frame[2]
e_x.dot(e_y).expr()

Out[17]:

## Spherical coordinates¶

Spherical coordinates are introduced by

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

Out[18]:
In [19]:
spherical[:]

Out[19]:
In [20]:
spherical.coord_range()

Out[20]:

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

In [21]:
E.atlas()

Out[21]:

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

In [22]:
E.coord_change(spherical, cartesian).display()

Out[22]:
In [23]:
E.coord_change(cartesian, spherical).display()

Out[23]:

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

In [24]:
E.frames()

Out[24]:

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

Out[25]:

We may check that it is an orthonormal frame:

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

Out[26]:

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

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


The reverse transformation:

In [28]:
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 [29]:
for vec in spherical_frame:
show(vec.display(spherical.frame(), spherical))


## Cylindrical coordinates¶

Cylindrical coordinates are introduced by

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

Out[30]:
In [31]:
cylindrical[:]

Out[31]:
In [32]:
rh is cylindrical[1]

Out[32]:
In [33]:
cylindrical.coord_range()

Out[33]:

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

In [34]:
E.atlas()

Out[34]:

The transformations linking the cylindrical coordinates to the Cartesian ones are

In [35]:
E.coord_change(cylindrical, cartesian).display()

Out[35]:
In [36]:
E.coord_change(cartesian, cylindrical).display()

Out[36]:

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

In [37]:
E.frames()

Out[37]:

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

In [38]:
cylindrical_frame = E.cylindrical_frame()
cylindrical_frame

Out[38]:

We may check that it is an orthonormal frame:

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

Out[39]:

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

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


The reverse transformation:

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


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

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


The reverse transformation:

In [43]:
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 [44]:
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 [45]:
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 [46]:
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 [47]:
cartesian(p)

Out[47]:
In [48]:
spherical(p)

Out[48]:
In [49]:
cylindrical(p)

Out[49]:

An example of point defined from its spherical coordinates:

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

Point q on the Euclidean space E^3

In [51]:
spherical(q)

Out[51]:
In [52]:
cartesian(q)

Out[52]:
In [53]:
cylindrical(q)

Out[53]:

## 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 [54]:
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 [55]:
f.display()

Out[55]:

We can limit the output to a single coordinate system:

In [56]:
f.display(cartesian)

Out[56]:
In [57]:
f.display(cylindrical)

Out[57]:

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

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

Out[58]:
In [59]:
f.expr(spherical)

Out[59]:
In [60]:
f.expr(cylindrical)

Out[60]:

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

In [61]:
f(p)

Out[61]:
In [62]:
f(q)

Out[62]:

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

In [63]:
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 [64]:
g = E.scalar_field({spherical: r^2}, name='g')

In [65]:
g.display()

Out[65]:

## 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 [66]:
v = E.vector_field(-y, x, z^2, name='v')
v.display()

Out[66]:

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

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

Out[67]:

Let us provide v with some name, as above:

In [68]:
v.set_name('v')
v.display()

Out[68]:

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

In [69]:
v[1]

Out[69]:
In [70]:
v[:]

Out[70]:

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

In [71]:
v.display(spherical_frame)

Out[71]:

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 [72]:
v.display(spherical_frame, spherical)

Out[72]:

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 [73]:
v[spherical_frame,1]

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

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

Out[75]:

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

In [76]:
v.display(cylindrical_frame, cylindrical)

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

Out[77]:

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

In [78]:
vp = v.at(p)
print(vp)

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

In [79]:
vp.display()

Out[79]:
In [80]:
vp.display(spherical_frame.at(p))

Out[80]:
In [81]:
vp.display(cylindrical_frame.at(p))

Out[81]:

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

In [82]:
vq = v.at(q)
print(vq)

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

In [83]:
vq.display()

Out[83]:
In [84]:
vq.display(spherical_frame.at(q))

Out[84]:
In [85]:
vq.display(cylindrical_frame.at(q))

Out[85]:

## Changing the default coordinates and default vector frame¶

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

In [86]:
E.set_default_chart(spherical)


Then

In [87]:
f.expr()

Out[87]:
In [88]:
v.display()

Out[88]:

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

In [89]:
E.set_default_frame(spherical_frame)


Then

In [90]:
v.display()

Out[90]:
In [91]:
v.display(cartesian_frame, cartesian)

Out[91]: