version()
'SageMath version 8.2.rc4, Release Date: 2018-04-20'
%display latex
We define the Euclidean plane $\mathbb{E}^2$ as a 2-dimensional Euclidean space, with Cartesian coordinates $(x,y)$:
E.<x,y> = EuclideanSpace()
print(E)
E
Euclidean plane E^2
Thanks to the use of <x,y>
in the above command, the Python variables x
and y
are assigned to the symbolic variables $x$ and $y$ describing the Cartesian coordinates:
type(y)
Instead of using the variables x
and y
, one may also access to the coordinates by their indices in the chart of Cartesian coordinates:
cartesian = E.cartesian_coordinates()
cartesian
cartesian[1]
cartesian[2]
y is cartesian[2]
Each of the Cartesian coordinates spans the entire real line:
cartesian.coord_range()
The Euclidean plane $\mathbb{E}^2$ is canonically endowed with the vector frame associated with Cartesian coordinates:
E.default_frame()
Vector fields on $\mathbb{E}^2$ are then defined from their components in that frame:
v = E.vector_field(-y, x, name='v')
v.display()
The access to individual components is performed by the square bracket operator:
v[1]
v[:]
A plot of the vector field $v$ (this is with default parameters, see the list of options for customizing the plot):
v.plot()
One may also define a vector field by setting the components in a second stage:
w = E.vector_field(name='w')
w[1] = function('w_x')(x,y)
w[2] = function('w_y')(x,y)
w.display()
Note that in the above example the components of $w$ are unspecified functions of $(x,y)$, contrary to the components of $v$.
Standard linear algebra operations are available on vector fields:
s = 2*v + x*w
s.display()
The dot (scalar) product of $v$ by $w$ in performed by the operator dot_product
; it gives rise to a scalar field on $\mathbb{E}^2$:
s = v.dot_product(w)
print(s)
Scalar field v.w on the Euclidean plane E^2
A shortcut alias of dot_product
is dot
:
s == v.dot(w)
s.display()
The symbolic expression representing the scalar field $v\cdot w$ is obtained by means of the method expr()
:
s.expr()
The Euclidean norm of the vector field $v$ is a scalar field on $\mathbb{E}^2$:
s = norm(v)
s.display()
Again, the corresponding symbolic expression is obtained via expr()
:
s.expr()
norm(w).expr()
We have of course $\|v\|^2 = v\cdot v$ :
norm(v)^2 == v.dot(v)
We introduce a point $p\in \mathbb{E}^2$ via the generic SageMath syntax for creating an element from its parent (here $\mathbb{E}^2$), i.e. the call operator ()
, with the Cartesian coordinates of the point as the first argument:
p = E((-2,3), name='p')
print(p)
Point p on the Euclidean plane E^2
The coordinates of $p$ are returned by the method coord()
:
p.coord()
or by letting the chart cartesian
act on the point:
cartesian(p)
The value of the scalar field s = norm(v)
at $p$ is
s(p)
The value of a vector field at $p$ is obtained by the method at
(since the call operator ()
is reserved for the action of vector fields on scalar fields, see Section 5 below):
vp = v.at(p)
print(vp)
Vector v at Point p on the Euclidean plane E^2
vp.display()
wp = w.at(p)
wp.display()
s = v.at(p) + pi*w.at(p)
s.display()
Tu use functional notations, i.e. div(v)
instead of v.div()
for the divergence of the vector field v
, we import the functions div
, grad
, etc. in the global namespace:
from sage.manifolds.operators import *
The divergence of a vector field is returned by the function div
; the output is a scalar field on $\mathbb{E}^2$:
print(div(v))
Scalar field div(v) on the Euclidean plane E^2
div(v).display()
In the present case, $\mathrm{div}\, v$ vanishes identically:
div(v) == 0
On the contrary, the divergence of $w$ is
div(w).display()
div(w).expr()
The gradient of a scalar field, e.g. s = norm(v)
, is returned by the function grad
; the output is a vector field:
s = norm(v)
print(grad(s))
Vector field grad(|v|) on the Euclidean plane E^2
grad(s).display()
grad(s)[2]
For a generic scalar field
F = E.scalar_field(function('f')(x,y), name='F')
we have
grad(F).display()
grad(F)[:]
Of course, we may combine grad
and div
:
grad(div(w)).display()
The Laplace operator is obtained by the function laplacian
; it can act on a scalar field:
laplacian(F).display()
as well as on a vector field:
laplacian(w).display()
For a scalar field, we have the identity:
laplacian(F) == div(grad(F))
Polar coordinates $(r,\phi)$ are introduced on $\mathbb{E}^2$ by
polar.<r,ph> = E.polar_coordinates()
polar
polar.coord_range()
They are related to Cartesian coordinates by the following transformations:
E.coord_change(polar, cartesian).display()
E.coord_change(cartesian, polar).display()
The orthonormal vector frame associated to polar coordinates is
polar_frame = E.polar_frame()
polar_frame
er = polar_frame[1]
er.display() # display in the default frame (Cartesian frame)
# with the default coordinates (Cartesian)
er.display(cartesian.frame(), polar) # display in the Cartesian frame
# with components expressed in polar coordinates
eph = polar_frame[2]
eph.display()
eph.display(cartesian.frame(), polar)
We may check that $(e_r, e_\phi)$ is an orthonormal frame:
all([er.dot(er) == 1,
er.dot(eph) == 0,
eph.dot(eph) == 1])
Scalar fields can be expressed in terms of polar coordinates:
F.display()
F.display(polar)
and we may ask for the components of vector fields in terms of the polar frame:
v.display() # default frame and default coordinates (both Cartesian ones)
v.display(polar_frame) # polar frame and default coordinates
v.display(polar_frame, polar) # polar frame and polar coordinates
w.display()
w.display(polar_frame, polar)
Let us define a generic scalar field in terms of polar coordinates:
H = E.scalar_field({polar: function('h')(r,ph)}, name='H')
H.display(polar)
The gradient of $H$ is then
grad(H).display(polar_frame, polar)
To access to individual components is perfomed by the square bracket operator, where, in addition to the index, one has to specify the vector frame and the coordinates if they are not the default ones:
grad(H).display(cartesian.frame(), polar)
grad(H)[polar_frame,2,polar]
Let us define a generic vector field in terms of polar coordinates:
u = E.vector_field(function('u_r')(r,ph),
function('u_ph', latex_name=r'u_\phi')(r,ph),
frame=polar_frame, chart=polar, name='u')
u.display(polar_frame, polar)
div(u).display(polar)
div(u).expr(polar)
div(u).expr(polar).expand()
In order to avoid specifying the arguments polar_frame
and polar
in display()
, expr()
and []
, we may change the default values by
E.set_default_chart(polar)
E.set_default_frame(polar_frame)
Then we have
u.display()
u[1]
v.display()
v[2]
w.display()
div(u).expr()
$\mathbb{E}^2$ is actually a Riemannian manifold, i.e. a smooth real manifold endowed with a positive definite metric tensor:
E.category()
print(E.category())
Category of smooth manifolds over Real Field with 53 bits of precision
E.base_field() is RR
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.
The user atlas of $\mathbb{E}^2$ has two charts:
E.atlas()
while there are three vector frames defined on $\mathbb{E}^2$:
E.frames()
Indeed, there are two frames associated with polar coordinates: the coordinate frame $(\frac{\partial}{\partial r}, \frac{\partial}{\partial \phi})$ and the orthonormal frame $(e_r, e_\phi)$.
The default metric tensor of $\mathbb{E}^2$ is
g = E.metric()
print(g)
Riemannian metric g on the Euclidean plane E^2
g.display()
In the above display, $e^r$ and $e^\phi$ are the 1-forms defining the coframe dual to the orthonormal polar frame $(e_r,e_\phi)$, which is the default vector frame on $\mathbb{E}^2$:
polar_frame.coframe()
Of course, we may ask for display with respect to frames different from the default one:
g.display(cartesian.frame())
g.display(polar.frame())
g[:]
g[polar.frame(),:]
It is a flat metric: its (Riemann) curvature tensor is zero:
print(g.riemann())
Tensor field Riem(g) of type (1,3) on the Euclidean plane E^2
g.riemann().display()
The metric $g$ is defining the dot product on $\mathbb{E}^2$:
v.dot(w) == g(v,w)
norm(v) == sqrt(g(v,v))
Vector fields acts as derivative on scalar fields:
print(v(F))
Scalar field v(F) on the Euclidean plane E^2
v(F).display()
v(F) == v.dot(grad(F))
dF = F.differential()
print(dF)
1-form dF on the Euclidean plane E^2
v(F) == dF(v)
The set $\mathfrak{X}(\mathbb{E}^2)$ of all vector fields on $\mathbb{E}^2$ is a free module of rank 2 over the commutative algebra of smooth scalar fields on $\mathbb{E}^2$, $C^\infty(\mathbb{E}^2)$:
XE = v.parent()
print(XE)
XE
Free module X(E^2) of vector fields on the Euclidean plane E^2
print(XE.category())
Category of finite dimensional modules over Algebra of differentiable scalar fields on the Euclidean plane E^2
print(XE.base_ring())
XE.base_ring()
Algebra of differentiable scalar fields on the Euclidean plane E^2
CE = F.parent()
CE
CE is XE.base_ring()
print(CE.category())
Category of commutative algebras over Symbolic Ring
rank(XE)
The bases of the free module $\mathfrak{X}(\mathbb{E}^2)$ are nothing but the vector frames defined on $\mathbb{E}^2$:
XE.bases()
Vector fields evaluated at a point are vectors in the tangent space at this point:
vp = v.at(p)
vp.display()
Tp = vp.parent()
print(Tp)
Tp
Tangent space at Point p on the Euclidean plane E^2
print(Tp.category())
Category of finite dimensional vector spaces over Symbolic Ring
dim(Tp)
isinstance(Tp, FiniteRankFreeModule)
Tp.bases()
The Levi-Civita connection associated to the Euclidean metric $g$ is
nabla = g.connection()
print(nabla)
nabla
Levi-Civita connection nabla_g associated with the Riemannian metric g on the Euclidean plane E^2
The corresponding Christoffel symbols with respect to the polar coordinates are:
g.christoffel_symbols_display()
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).
The Christoffel symbols with respect to the Cartesian coordinates are all zero:
g.christoffel_symbols_display(chart=cartesian, only_nonzero=False)
$\nabla_g$ is the connection involved in differential operators:
grad(F) == nabla(F).up(g)
nabla(F) == grad(F).down(g)
div(v) == nabla(v).trace()
div(w) == nabla(w).trace()
laplacian(F) == nabla(nabla(F).up(g)).trace()
laplacian(w) == nabla(nabla(w).up(g)).trace(1,2)