Vector calculus in the Euclidean plane

This worksheet illustrates some functionalities regarding Euclidean spaces in SageMath, as introduced in Trac ticket #24623.

In [1]:
version()
Out[1]:
'SageMath version 8.2.rc4, Release Date: 2018-04-20'
In [2]:
%display latex

1. Defining the Euclidean plane

We define the Euclidean plane $\mathbb{E}^2$ as a 2-dimensional Euclidean space, with Cartesian coordinates $(x,y)$:

In [3]:
E.<x,y> = EuclideanSpace()
print(E)
E
Euclidean plane E^2
Out[3]:

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:

In [4]:
type(y)
Out[4]:

Instead of using the variables x and y, one may also access to the coordinates by their indices in the chart of Cartesian coordinates:

In [5]:
cartesian = E.cartesian_coordinates()
cartesian
Out[5]:
In [6]:
cartesian[1]
Out[6]:
In [7]:
cartesian[2]
Out[7]:
In [8]:
y is cartesian[2]
Out[8]:

Each of the Cartesian coordinates spans the entire real line:

In [9]:
cartesian.coord_range()
Out[9]:

2. Vector fields

The Euclidean plane $\mathbb{E}^2$ is canonically endowed with the vector frame associated with Cartesian coordinates:

In [10]:
E.default_frame()
Out[10]:

Vector fields on $\mathbb{E}^2$ are then defined from their components in that frame:

In [11]:
v = E.vector_field(-y, x, name='v')
v.display()
Out[11]:

The access to individual components is performed by the square bracket operator:

In [12]:
v[1]
Out[12]:
In [13]:
v[:]
Out[13]:

A plot of the vector field $v$ (this is with default parameters, see the list of options for customizing the plot):

In [14]:
v.plot()
Out[14]:

One may also define a vector field by setting the components in a second stage:

In [15]:
w = E.vector_field(name='w')
w[1] = function('w_x')(x,y)
w[2] = function('w_y')(x,y)
w.display()
Out[15]:

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:

In [16]:
s = 2*v + x*w
s.display()
Out[16]:

Scalar product and norm

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$:

In [17]:
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:

In [18]:
s == v.dot(w)
Out[18]:
In [19]:
s.display()
Out[19]:

The symbolic expression representing the scalar field $v\cdot w$ is obtained by means of the method expr():

In [20]:
s.expr()
Out[20]:

The Euclidean norm of the vector field $v$ is a scalar field on $\mathbb{E}^2$:

In [21]:
s = norm(v)
s.display()
Out[21]:

Again, the corresponding symbolic expression is obtained via expr():

In [22]:
s.expr()
Out[22]:
In [23]:
norm(w).expr()
Out[23]:

We have of course $\|v\|^2 = v\cdot v$ :

In [24]:
norm(v)^2 == v.dot(v)
Out[24]:

Values at a given point

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:

In [25]:
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():

In [26]:
p.coord()
Out[26]:

or by letting the chart cartesian act on the point:

In [27]:
cartesian(p)
Out[27]:

The value of the scalar field s = norm(v) at $p$ is

In [28]:
s(p)
Out[28]:

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

In [29]:
vp = v.at(p)
print(vp)
Vector v at Point p on the Euclidean plane E^2
In [30]:
vp.display()
Out[30]:
In [31]:
wp = w.at(p)
wp.display()
Out[31]:
In [32]:
s = v.at(p) + pi*w.at(p)
s.display()
Out[32]:

3. Differential operators

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:

In [33]:
from sage.manifolds.operators import *

Divergence

The divergence of a vector field is returned by the function div; the output is a scalar field on $\mathbb{E}^2$:

In [34]:
print(div(v))
Scalar field div(v) on the Euclidean plane E^2
In [35]:
div(v).display()
Out[35]:

In the present case, $\mathrm{div}\, v$ vanishes identically:

In [36]:
div(v) == 0
Out[36]:

On the contrary, the divergence of $w$ is

In [37]:
div(w).display()
Out[37]:
In [38]:
div(w).expr()
Out[38]:

Gradient

The gradient of a scalar field, e.g. s = norm(v), is returned by the function grad; the output is a vector field:

In [39]:
s = norm(v)
print(grad(s))
Vector field grad(|v|) on the Euclidean plane E^2
In [40]:
grad(s).display()
Out[40]:
In [41]:
grad(s)[2]
Out[41]:

For a generic scalar field

In [42]:
F = E.scalar_field(function('f')(x,y), name='F')

we have

In [43]:
grad(F).display()
Out[43]:
In [44]:
grad(F)[:]
Out[44]:

Of course, we may combine grad and div:

In [45]:
grad(div(w)).display()
Out[45]:

Laplace operator

The Laplace operator is obtained by the function laplacian; it can act on a scalar field:

In [46]:
laplacian(F).display()
Out[46]:

as well as on a vector field:

In [47]:
laplacian(w).display()
Out[47]:

For a scalar field, we have the identity:

In [48]:
laplacian(F) == div(grad(F))
Out[48]:

4. Polar coordinates

Polar coordinates $(r,\phi)$ are introduced on $\mathbb{E}^2$ by

In [49]:
polar.<r,ph> = E.polar_coordinates()
polar
Out[49]:
In [50]:
polar.coord_range()
Out[50]:

They are related to Cartesian coordinates by the following transformations:

In [51]:
E.coord_change(polar, cartesian).display()
Out[51]:
In [52]:
E.coord_change(cartesian, polar).display()
Out[52]:

The orthonormal vector frame associated to polar coordinates is

In [53]:
polar_frame = E.polar_frame()
polar_frame
Out[53]:
In [54]:
er = polar_frame[1]
er.display()  # display in the default frame (Cartesian frame) 
              # with the default coordinates (Cartesian)
Out[54]:
In [55]:
er.display(cartesian.frame(), polar) # display in the Cartesian frame
                                     # with components expressed in polar coordinates
Out[55]:
In [56]:
eph = polar_frame[2]
eph.display()
Out[56]:
In [57]:
eph.display(cartesian.frame(), polar)
Out[57]:

We may check that $(e_r, e_\phi)$ is an orthonormal frame:

In [58]:
all([er.dot(er) == 1,
     er.dot(eph) == 0,
     eph.dot(eph) == 1])
Out[58]:

Scalar fields can be expressed in terms of polar coordinates:

In [59]:
F.display()
Out[59]:
In [60]:
F.display(polar)
Out[60]:

and we may ask for the components of vector fields in terms of the polar frame:

In [61]:
v.display()  # default frame and default coordinates (both Cartesian ones)
Out[61]:
In [62]:
v.display(polar_frame)  # polar frame and default coordinates
Out[62]:
In [63]:
v.display(polar_frame, polar)  # polar frame and polar coordinates
Out[63]:
In [64]:
w.display()
Out[64]:
In [65]:
w.display(polar_frame, polar)
Out[65]:

Gradient in polar coordinates

Let us define a generic scalar field in terms of polar coordinates:

In [66]:
H = E.scalar_field({polar: function('h')(r,ph)}, name='H')
H.display(polar)
Out[66]:

The gradient of $H$ is then

In [67]:
grad(H).display(polar_frame, polar)
Out[67]:

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:

In [68]:
grad(H).display(cartesian.frame(), polar)
Out[68]:
In [69]:
grad(H)[polar_frame,2,polar]
Out[69]:

Divergence in polar coordinates

Let us define a generic vector field in terms of polar coordinates:

In [70]:
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)
Out[70]:
In [71]:
div(u).display(polar)
Out[71]:
In [72]:
div(u).expr(polar)
Out[72]:
In [73]:
div(u).expr(polar).expand()
Out[73]:

Using polar coordinates by default:

In order to avoid specifying the arguments polar_frame and polar in display(), expr() and [], we may change the default values by

In [74]:
E.set_default_chart(polar)
E.set_default_frame(polar_frame)

Then we have

In [75]:
u.display()
Out[75]:
In [76]:
u[1]
Out[76]:
In [77]:
v.display()
Out[77]:
In [78]:
v[2]
Out[78]:
In [79]:
w.display()
Out[79]:
In [80]:
div(u).expr()
Out[80]:

5. Advanced topics: the Euclidean plane as a Riemannian manifold

$\mathbb{E}^2$ is actually a Riemannian manifold, i.e. a smooth real manifold endowed with a positive definite metric tensor:

In [81]:
E.category()
Out[81]:
In [82]:
print(E.category())
Category of smooth manifolds over Real Field with 53 bits of precision
In [83]:
E.base_field() is RR
Out[83]:

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:

In [84]:
E.atlas()
Out[84]:

while there are three vector frames defined on $\mathbb{E}^2$:

In [85]:
E.frames()
Out[85]:

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)$.

Riemannian metric

The default metric tensor of $\mathbb{E}^2$ is

In [86]:
g = E.metric()
print(g)
Riemannian metric g on the Euclidean plane E^2
In [87]:
g.display()
Out[87]:

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$:

In [88]:
polar_frame.coframe()
Out[88]:

Of course, we may ask for display with respect to frames different from the default one:

In [89]:
g.display(cartesian.frame())
Out[89]:
In [90]:
g.display(polar.frame())
Out[90]:
In [91]:
g[:]
Out[91]:
In [92]:
g[polar.frame(),:]
Out[92]:

It is a flat metric: its (Riemann) curvature tensor is zero:

In [93]:
print(g.riemann())
Tensor field Riem(g) of type (1,3) on the Euclidean plane E^2
In [94]:
g.riemann().display()
Out[94]:

The metric $g$ is defining the dot product on $\mathbb{E}^2$:

In [95]:
v.dot(w) == g(v,w)
Out[95]:
In [96]:
norm(v) == sqrt(g(v,v))
Out[96]:

Vector fields as derivatives

Vector fields acts as derivative on scalar fields:

In [97]:
print(v(F))
Scalar field v(F) on the Euclidean plane E^2
In [98]:
v(F).display()
Out[98]:
In [99]:
v(F) == v.dot(grad(F))
Out[99]:
In [100]:
dF = F.differential()
print(dF)
1-form dF on the Euclidean plane E^2
In [101]:
v(F) == dF(v)
Out[101]:

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

In [102]:
XE = v.parent()
print(XE)
XE
Free module X(E^2) of vector fields on the Euclidean plane E^2
Out[102]:
In [103]:
print(XE.category())
Category of finite dimensional modules over Algebra of differentiable scalar fields on the Euclidean plane E^2
In [104]:
print(XE.base_ring())
XE.base_ring()
Algebra of differentiable scalar fields on the Euclidean plane E^2
Out[104]:
In [105]:
CE = F.parent()
CE
Out[105]:
In [106]:
CE is XE.base_ring()
Out[106]:
In [107]:
print(CE.category())
Category of commutative algebras over Symbolic Ring
In [108]:
rank(XE)
Out[108]:

The bases of the free module $\mathfrak{X}(\mathbb{E}^2)$ are nothing but the vector frames defined on $\mathbb{E}^2$:

In [109]:
XE.bases()
Out[109]:

Tangent spaces

Vector fields evaluated at a point are vectors in the tangent space at this point:

In [110]:
vp = v.at(p)
vp.display()
Out[110]:
In [111]:
Tp = vp.parent()
print(Tp)
Tp
Tangent space at Point p on the Euclidean plane E^2
Out[111]:
In [112]:
print(Tp.category())
Category of finite dimensional vector spaces over Symbolic Ring
In [113]:
dim(Tp)
Out[113]:
In [114]:
isinstance(Tp, FiniteRankFreeModule)
Out[114]:
In [115]:
Tp.bases()
Out[115]:

Levi-Civita connection

The Levi-Civita connection associated to the Euclidean metric $g$ is

In [116]:
nabla = g.connection()
print(nabla)
nabla
Levi-Civita connection nabla_g associated with the Riemannian metric g on the Euclidean plane E^2
Out[116]:

The corresponding Christoffel symbols with respect to the polar coordinates are:

In [117]:
g.christoffel_symbols_display()
Out[117]:

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:

In [118]:
g.christoffel_symbols_display(chart=cartesian, only_nonzero=False)
Out[118]:

$\nabla_g$ is the connection involved in differential operators:

In [119]:
grad(F) == nabla(F).up(g)
Out[119]:
In [120]:
nabla(F) == grad(F).down(g)
Out[120]:
In [121]:
div(v) == nabla(v).trace()
Out[121]:
In [122]:
div(w) == nabla(w).trace()
Out[122]:
In [123]:
laplacian(F) == nabla(nabla(F).up(g)).trace()
Out[123]:
In [124]:
laplacian(w) == nabla(nabla(w).up(g)).trace(1,2)
Out[124]:
In [ ]: