version()
'SageMath version 8.2.rc4, Release Date: 2018-04-20'
%display latex
We start by declaring the 3-dimensional Euclidean space $\mathbb{E}^3$, with $(x,y,z)$ as Cartesian coordinates:
E.<x,y,z> = EuclideanSpace()
print(E)
E
Euclidean space E^3
$\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)$ 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()
$\mathbb{E}^3$ is endowed with the orthonormal vector frame $(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 $e_x\cdot e_x = 1$:
e_x = cartesian_frame[1]
e_x
e_x.dot(e_x).expr()
as well as $e_x\cdot e_y = 0$:
e_y = cartesian_frame[2]
e_x.dot(e_y).expr()
Spherical coordinates are introduced by
spherical.<r,th,ph> = E.spherical_coordinates()
spherical
spherical[:]
spherical.coord_range()
$\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()
$\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 $(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()
:
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:
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
):
for vec in spherical_frame:
show(vec.display(spherical.frame(), spherical))
Cylindrical coordinates are introduced by
cylindrical.<rh,ph,z> = E.cylindrical_coordinates()
cylindrical
cylindrical[:]
rh is cylindrical[1]
cylindrical.coord_range()
$\mathbb{E}^3$ is now endowed with two 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()
$\mathbb{E}^3$ is also now endowed with five vector frames:
E.frames()
The orthonormal frame associated with cylindrical coordinates is $(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_\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
):
for vec in cylindrical_frame:
show(vec.display(cylindrical.frame(), cylindrical))
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:
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 $p$ in a given coordinate chart are obtained by letting the corresponding chart act on $p$:
cartesian(p)
spherical(p)
cylindrical(p)
An example of 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)
Let us define a scalar field on $\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 $f$ 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 $f$ at points $p$ and $q$:
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()
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:
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 $v$ are returned by the square bracket operator:
v[1]
v[:]
The expression of $v$ 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 $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:
v[spherical_frame,1]
v[spherical_frame,1,spherical]
v[spherical_frame,:,spherical]
Similarly, the expression of $v$ in terms of the cylindrical frame is
v.display(cylindrical_frame, cylindrical)
v[cylindrical_frame,:,cylindrical]
The value of the vector field $v$ at point $p$:
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 $v$ at point $q$:
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))
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)