Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Tutorial vector calculus 4: changing coordinates

Project: SageManifolds
Views: 405
Kernel: SageMath 8.8.beta2

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.

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:

version()
'SageMath version 8.8.beta2, Release Date: 2019-04-14'

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

%display latex

Cartesian coordinates

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

E.<x,y,z> = EuclideanSpace() print(E) E
Euclidean space E^3

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

E.atlas()

Let us denote by cartesian the chart of Cartesian coordinates:

cartesian = E.cartesian_coordinates() cartesian

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

cartesian[1]
cartesian[:]

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)(x,y,z) as symbolic expressions:

y is cartesian[2]
type(y)

Each of the Cartesian coordinates spans the entire real line:

cartesian.coord_range()

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

cartesian is E.default_chart()

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

E.frames()

Let us denote it by cartesian_frame:

cartesian_frame = E.cartesian_frame() cartesian_frame
cartesian_frame is E.default_frame()

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

e_x = cartesian_frame[1] e_x
e_x.dot(e_x).expr()

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

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

Spherical coordinates

Spherical coordinates are introduced by

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

We have

spherical[:]
spherical.coord_range()

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

E.atlas()

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

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

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

E.frames()

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 (ex,ey,ez)(e_x,e_y,e_z). For spherical coordinates, the orthonormal frame is denoted (er,eθ,eϕ)(e_r,e_\theta,e_\phi) and is returned by the method spherical_frame():

spherical_frame = E.spherical_frame() spherical_frame

We may check that it is an orthonormal frame:

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

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

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

The reverse transformation:

for vec in cartesian_frame: print(vec.display(spherical_frame, spherical))
e_x = cos(ph)*sin(th) e_r + cos(ph)*cos(th) e_th - sin(ph) e_ph e_y = sin(ph)*sin(th) e_r + cos(th)*sin(ph) e_th + cos(ph) e_ph e_z = cos(th) e_r - sin(th) e_th

The orthonormal frame (er,eθ,eϕ)(e_r,e_\theta,e_\phi) expressed in terms on the coordinate frame (r,θ,ϕ)\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):

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

Cylindrical coordinates

Cylindrical coordinates are introduced by

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

We have

cylindrical[:]
rh is cylindrical[1]
cylindrical.coord_range()

E3\mathbb{E}^3 is now endowed with three coordinate charts:

E.atlas()

The transformations linking the cylindrical coordinates to the Cartesian ones are

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

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

E.frames()

The orthonormal frame associated with cylindrical coordinates is (eρ,eϕ,ez)(e_\rho, e_\phi, e_z):

cylindrical_frame = E.cylindrical_frame() cylindrical_frame

We may check that it is an orthonormal frame:

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

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

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

The reverse transformation:

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

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

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

The reverse transformation:

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

The orthonormal frame (eρ,eϕ,ez)(e_\rho,e_\phi,e_z) expressed in terms on the coordinate frame (ρ,ϕ,z)\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):

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

Coordinates of a point

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

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

p = E((-1, 1,0), name='p') print(p)
Point p on the Euclidean space E^3

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

cartesian(p)
spherical(p)
cylindrical(p)

A point defined from its spherical coordinates:

q = E((4,pi/3,pi), chart=spherical, name='q') print(q)
Point q on the Euclidean space E^3
spherical(q)
cartesian(q)
cylindrical(q)

Expressions of a scalar field in various coordinate systems

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

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 ff in terms of other coordinates is automatically computed:

f.display()

We can limit the output to a single coordinate system:

f.display(cartesian)
f.display(cylindrical)

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

f.expr() # expression in the default chart (Cartesian coordinates)
f.expr(spherical)
f.expr(cylindrical)

The value of ff at points pp and qq:

f(p)
f(q)

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

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:

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

Expression of a vector field in various frames

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

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

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

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

Let us provide v with some name, as above:

v.set_name('v') v.display()

The components of vv are returned by the square bracket operator:

v[1]
v[:]

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

v.display(spherical_frame)

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

v.display(spherical_frame, spherical)

Again, the components of vv 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:

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

Similarly, the expression of vv in terms of the cylindrical frame is

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

The value of the vector field vv at point pp:

vp = v.at(p) print(vp)
Vector v at Point p on the Euclidean space E^3
vp.display()
vp.display(spherical_frame.at(p))
vp.display(cylindrical_frame.at(p))

The value of the vector field vv at point qq:

vq = v.at(q) print(vq)
Vector v at Point q on the Euclidean space E^3
vq.display()
vq.display(spherical_frame.at(q))
vq.display(cylindrical_frame.at(q))

Changing the default coordinates and default vector frame

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

E.set_default_chart(spherical)

Then

f.expr()
v.display()

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

E.set_default_frame(spherical_frame)

Then

v.display()
v.display(cartesian_frame, cartesian)