This notebook provides some introduction to tensors on free modules of finite rank in SageMath. This is a pure algebraic subpart of the SageManifolds project, which does not depend on other parts of SageManifolds and which has been integrated in SageMath 6.6.
version()
'SageMath version 10.3.beta6, Release Date: 2024-01-21'
First we set up the notebook to display mathematical objects using LaTeX rendering:
%display latex
Let $R$ be a commutative ring and $M$ a free module of finite rank over $R$, i.e. a module over $R$ that admits a finite basis (finite family of linearly independent generators). Since $R$ is commutative, it has the invariant basis number property: all bases of $M$ have the same cardinality, which is called the rank of $M$. In this tutorial, we consider a free module of rank 3 over the integer ring $\mathbb{Z}$:
M = FiniteRankFreeModule(ZZ, 3, name='M', start_index=1)
The first two arguments are the ring and the rank; the third one is a string to denote the module and the last one defines the range of indices to be used for tensor components on the module: setting it to 1 means that indices will range in $\{1,2,3\}$. The default value is start_index=0
.
The functionprint
returns a short description of the just constructed module:
print(M)
Rank-3 free module M over the Integer Ring
If we ask just for M, the module's LaTeX symbol is returned; by default, this is the same as the argument name
in the constructor (this can be changed by providing the optional argument latex_name
:
M
M1 = FiniteRankFreeModule(ZZ, 3, name='M', latex_name=r'\mathcal{M}', start_index=1)
M1
The indices of basis elements or tensor components on the module are generated by the method irange()
, to be used in loops:
for i in M.irange():
print(i)
1 2 3
If the parameter start_index
had not been specified, the default range of the indices would have been $\{0,1,2\}$ instead:
M0 = FiniteRankFreeModule(ZZ, 3, name='M')
for i in M0.irange():
print(i)
0 1 2
$M$ is the category of finite dimensional modules over $\mathbb{Z}$:
print(M.category())
Category of finite dimensional modules over Integer Ring
Self-inquiry commands are
M.base_ring()
M.rank()
At construction, the free module $M$ has no pre-defined basis:
M.print_bases()
No basis has been defined on the Rank-3 free module M over the Integer Ring
M.bases()
For this reason, the class FiniteRankFreeModule
does not inherit from Sage class CombinatorialFreeModule
isinstance(M, CombinatorialFreeModule)
and $M$ does not belong to the category of modules with a distinguished basis:
M in ModulesWithBasis(ZZ)
It simply belongs to the category of modules over $\mathbb{Z}$:
M in Modules(ZZ)
More precisely, it belongs to the subcategory of finite dimensional modules over $\mathbb{Z}$:
M in Modules(ZZ).FiniteDimensional()
We define a first basis on $M$ as follows:
e = M.basis('e')
e
M.print_bases()
Bases defined on the Rank-3 free module M over the Integer Ring: - (e_1,e_2,e_3) (default basis)
The elements of the basis are accessed via their indices:
e[1]
print(e[1])
Element e_1 of the Rank-3 free module M over the Integer Ring
e[1] in M
e[1].parent()
Let us introduce a second basis on the free module $M$ from a family of 3 linearly independent module elements:
f = M.basis('f', from_family=(-e[1]+2*e[2]-4*e[3],
e[2]+2*e[3],
e[2]+3*e[3]))
print(f)
f
Basis (f_1,f_2,f_3) on the Rank-3 free module M over the Integer Ring
We may ask to view each element of basis $f$ in terms of its expansion onto basis $e$, via the method display()
:
f[1].display(e)
f[2].display(e)
f[3].display(e)
Conversely, the expression of basis $e$ is terms of basis $f$ is
e[1].display(f)
e[2].display(f)
e[3].display(f)
The module automorphism $a$ relating the two bases is obtained as
a = M.change_of_basis(e,f)
a
It belongs to the general linear group of the free module $M$:
a.parent()
and its matrix w.r.t. basis $e$ is
a.matrix(e)
Let us check that the elements of basis $f$ are images of the elements of basis $e$ via $a$:
all([f[i] == a(e[i]) for i in M.irange()])
The reverse change of basis is of course the inverse automorphism:
M.change_of_basis(f,e) == a^(-1)
(a^(-1)).matrix(f)
At this stage, two bases have been defined on $M$:
M.print_bases()
Bases defined on the Rank-3 free module M over the Integer Ring: - (e_1,e_2,e_3) (default basis) - (f_1,f_2,f_3)
The first defined basis, $e$, is considered as the default basis, which means that it can be skipped in any method argument requirying a basis. For instance, let us consider the method display()
:
f[1].display(e)
Since $e$ is the default basis, the above command is fully equivalent to
f[1].display()
Of course, the names of non-default bases have to be specified:
f[1].display(f)
e[1].display(f)
Note that the concept of *default basis* is different from that of *distinguished basis* which is implemented in other free module constructions in Sage (e.g. CombinatorialFreeModule
): the default basis is intended only for shorthand notations in user commands, avoiding to repeat the basis name many times; it is by no means a privileged basis on the module. For user convenience, the default basis can be changed at any moment by means of the method set_default_basis()
:
M.set_default_basis(f)
e[1].display()
Let us revert to $e$ as the default basis:
M.set_default_basis(e)
Elements of the free module $M$ are constructed by providing their components with respect to a given basis to the operator ()
acting on the module:
v = M([3,-4,1], basis=e, name='v')
print(v)
Element v of the Rank-3 free module M over the Integer Ring
Since $e$ is the default basis, its mention can be skipped:
v = M([3,-4,1], name='v')
print(v)
Element v of the Rank-3 free module M over the Integer Ring
v.display()
While $v$ has been defined from the basis $e$, its expression in terms of the basis $f$ can be evaluated, thanks to the known relation between the two bases:
v.display(f)
According to Sage terminology, the parent of $v$ is of course $M$:
v.parent()
We have also
v in M
Let us define a second module element, from the basis $f$ this time:
u = M([-1,3,5], basis=f, name='u')
u.display(f)
Another way to define module elements is of course via linear combinations:
w = 2*e[1] - e[2] - 3*e[3]
print(w)
Element of the Rank-3 free module M over the Integer Ring
w.display()
As the result of a linear combination, $w$ has no name; it can be given one by the method set_name()
and the LaTeX symbol can be specified if different from the name:
w.set_name('w', latex_name=r'\omega')
w.display()
Module operations are implemented, independently of the bases:
s = u + 3*v
print(s)
Element of the Rank-3 free module M over the Integer Ring
s.display()
s.display(f)
s = u - v
print(s)
Element u-v of the Rank-3 free module M over the Integer Ring
s.display()
s.display(f)
The components of a module element with respect to a given basis are given by the method components()
:
v.components(f)
A shortcut is comp()
:
v.comp(f) is v.components(f)
for i in M.irange():
print(v.comp(f)[i])
-3 17 -15
v.comp(f)[:]
The function display_comp()
provides a list of components w.r.t. to a given basis:
v.display_comp(f)
As a shortcut, instead of calling the method comp()
, the basis can be provided as the first argument of the square bracket operator:
v[f,2]
v[f,:]
For the default basis, the basis can be omitted:
v[:]
v[2]
A specific module element is the zero one:
print(M.zero())
Element zero of the Rank-3 free module M over the Integer Ring
M.zero()[:]
M.zero()[f,:]
v + M.zero() == v
Let us introduce some linear form on the free module $M$:
a = M.linear_form('a')
print(a)
Linear form a on the Rank-3 free module M over the Integer Ring
$a$ is specified by its components with respect to the basis dual to $e$:
a[:] = [2,-1,3]
a.display()
The notation $e^i$ stands for the elements of the basis dual to $e$, i.e. the basis of the dual module $M^*$ such that
$$e^i(e_j) = \delta^i_{\ \, j} $$Indeed
ed = e.dual_basis()
ed
print(ed[1])
Linear form e^1 on the Rank-3 free module M over the Integer Ring
ed[1](e[1]), ed[1](e[2]), ed[1](e[3])
ed[2](e[1]), ed[2](e[2]), ed[2](e[3])
ed[3](e[1]), ed[3](e[2]), ed[3](e[3])
The linear form $a$ can also be defined by its components with respect to the basis dual to $f$:
a[f,:] = [2,-1,3]
a.display(f)
For consistency, the previously defined components with respect to the basis dual to $e$ are automatically deleted and new ones are computed from the change-of-basis formula:
a.display()
By definition, linear forms belong to the dual module:
a.parent()
print(a.parent())
Dual of the Rank-3 free module M over the Integer Ring
a.parent() is M.dual()
The dual module is itself a free module of the same rank as $M$:
isinstance(M.dual(), FiniteRankFreeModule)
M.dual().rank()
Linear forms map module elements to ring elements:
a(v)
a(u)
in a linear way:
a(u+2*v) == a(u) + 2*a(v)
Let us introduce a second linear form, $b$, on the free module $M$:
b = M.linear_form('b')
b[:] = [-4,2,5]
and take its exterior product with the linear form $a$:
c = a.wedge(b)
print(c)
c
Alternating form a∧b of degree 2 on the Rank-3 free module M over the Integer Ring
c.display()
c.display(f)
c(u,v)
$c$ is antisymmetric:
c(v,u)
and is multilinear:
c(u+4*w,v) == c(u,v) + 4*c(w,v)
We may check the standard formula for the exterior product of two linear forms:
c(u,v) == a(u)*b(v) - b(u)*a(v)
In terms of tensor product (denoted here by *), it reads
c == a*b - b*a
The parent of the alternating form $c$ is the second external power of the dual module $M^*$, which is denoted by $\Lambda^2(M^*)$:
c.parent()
print(c.parent())
2nd exterior power of the dual of the Rank-3 free module M over the Integer Ring
$c$ is a tensor of type $(0,2)$:
c.tensor_type()
whose components with respect to any basis are antisymmetric:
c[:] # components with respect to the default basis (e)
c[f,:] # components with respect to basis f
c.comp(f)
An alternating form can be constructed from scratch:
c1 = M.alternating_form(2) # 2 stands for the degree
Only the non-zero and non-redundant components are to be defined (the others are deduced by antisymmetry); for the components with respect to the default basis, we write:
c1[1,2] = -108
c1[1,3] = -164
c1[2,3] = -53
Then
c1[:]
c1 == c
Internally, only non-redundant components are stored, in a dictionary whose keys are the indices:
c.comp(e)._comp
c.comp(f)._comp
The other components are deduced by antisymmetry.
The exterior product of a linear form with an alternating form of degree 2 leads to an alternating form of degree 3:
d = M.linear_form('d')
d[:] = [-1,-2,4]
s = d.wedge(c)
print(s)
Alternating form d∧a∧b of degree 3 on the Rank-3 free module M over the Integer Ring
s.display()
s.display(f)
s(e[1], e[2], e[3])
s(f[1], f[2], f[3])
$s$ is antisymmetric:
s(u,v,w), s(u,w,v), s(v,w,u), s(v,u,w), s(w,u,v), s(w,v,u)
$k$ and $l$ being non negative integers, a tensor of type $(k,l)$ on the free module $M$ is a multilinear map
$$ t: \underbrace{M^*\times\cdots\times M^*}_{k\ \; \mbox{times}} \times \underbrace{M\times\cdots\times M}_{l\ \; \mbox{times}} \longrightarrow R $$In the present case the ring $R$ is $\mathbb{Z}$.
For free modules of finite rank, we have the canonical isomorphism $M^{**} \simeq M$, so that the set of all tensors of type $(k,l)$ can be identified with the tensor product
$$ T^{(k,l)}(M) = \underbrace{M\otimes\cdots\otimes M}_{k\ \; \mbox{times}} \otimes \underbrace{M^*\otimes\cdots\otimes M^*}_{l\ \; \mbox{times}}$$In particular, tensors of type $(1,0)$ are identified with elements of $M$:
M.tensor_module(1,0) is M
v.tensor_type()
According to the above definition, linear forms are tensors of type (0,1):
a in M.tensor_module(0,1)
We have the identification of $T^{(0,1)}(M)$ with $M^*$:
M.tensor_module(0,1) is M.dual()
Arbitrary tensors are constructed via the module method tensor()
, by providing the tensor type $(k,l)$ and possibly the symbol to denote the tensor:
t = M.tensor((1,1), name='t')
print(t)
Type-(1,1) tensor t on the Rank-3 free module M over the Integer Ring
Let us set some component of $t$ in the basis $e$, for instance the component $t^1_{\ \, 2}$:
t[e,1,2] = -3
Since $e$ is the default basis, a shortcut for the above is
t[1,2] = -3
The unset components are zero:
t[:]
Components can be set at any time:
t[2,3] = 4
t[:]
The components with respect to the basis $f$ are evaluated by the change-of-basis formula $e\rightarrow f$:
t[f,:]
Another view of $t$, which reflects the fact that $T^{(1,1)}(M) = M\otimes M^*$, is
t.display()
Recall that $(e^i)$ is the basis of $M^*$ that is dual to the basis $(e_i)$ of $M$.
In term of the basis $(f_i)$ and its dual basis $(f^i)$, we have
t.display(f)
As a tensor of type (1,1), $t$ maps pairs (linear form, module element) to ring elements:
t(a,v)
t(a,v).parent()
Tensors of type (1,1) can be considered as endomorphisms, thanks to the isomorphism
$$ \begin{array}{cccccc} \mathrm{End}(M) & \longrightarrow & T^{(1,1)}(M) \\ \tilde t & \longmapsto & t: & M^*\times M & \longrightarrow & R \\& & & (a,v) & \longmapsto & a(\tilde t(v)) \end{array} $$tt = End(M)(t)
print(tt)
Generic endomorphism of Rank-3 free module M over the Integer Ring
tt.parent()
In a given basis, the matrix ${\tilde t}^i_{\ \, j}$ of the endomorphism $\tilde t$ is identical to the matrix of the tensor $t$:
tt.matrix(e)
t[e,:]
tt.matrix(e) == t[e,:]
tt.matrix(f)
t[f,:]
tt.matrix(f) == t[f,:]
As an endomorphism, $t$ maps module elements to module elements:
tt(v)
tt(v).parent()
tt(v).display()
$t$ belongs to the module $T^{(1,1)}(M)$:
t in M.tensor_module(1,1)
or, in Sage terminology,
t.parent() is M.tensor_module(1,1)
M.tensor_module(1,1).base_ring()
M.tensor_module(1,1).rank()
In addition to the arithmetic operations inherent to the module structure of $T^{(k,l)}(M)$, the following operations are implemented:
The tensor product is formed with the * operator. For instance the tensor product $t\otimes a$ is
ta = t*a
print(ta)
Type-(1,2) tensor t⊗a on the Rank-3 free module M over the Integer Ring
ta
ta.display()
ta.display(f)
The components w.r.t. a given basis can also be displayed as an array:
ta[:] # components w.r.t. the default basis (e)
ta[f,:] # components w.r.t. basis f
Each component ca be accessed individually:
ta[1,2,3] # access to a component w.r.t. the default basis
ta[f,1,2,3]
ta.parent()
ta in M.tensor_module(1,2)
The tensor product is not commutative:
print(a*t)
Type-(1,2) tensor a⊗t on the Rank-3 free module M over the Integer Ring
a*t == t*a
Forming a tensor of rank 4:
tav = ta*v
print(tav)
Type-(2,2) tensor t⊗a⊗v on the Rank-3 free module M over the Integer Ring
tav.display()
The (anti)symmetrization of a tensor $t$ over $n$ arguments involve the division by $n!$, which does not always make sense in the base ring $R$. In the present case, $R=\mathbb{Z}$ and to (anti)symmetrize over 2 arguments, we restrict to tensors with even components:
g = M.tensor((0,2), name='g')
g[1,2], g[2,1], g[2,2], g[3,2], g[3,3] = 2, -4, 8, 2, -6
g[:]
s = g.symmetrize() ; s
s.symmetries()
symmetry: (0, 1); no antisymmetry
s[:]
Symmetrization can be performed on an arbitray number of arguments, by providing their positions (first position = 0). In the present case
s == g.symmetrize(0,1)
One may use index notation to specify the symmetry:
s == g['_(ab)']
s == g['_{(ab)}'] # LaTeX type notation
Of course, since $s$ is already symmetric:
s.symmetrize() == s
The antisymmetrization proceeds accordingly:
s = g.antisymmetrize() ; s
s.symmetries()
no symmetry; antisymmetry: (0, 1)
s[:]
s == g.antisymmetrize(0,1)
As for symmetries, index notation can be used, instead ofantisymmetrize()
:
s == g['_[ab]']
s == g['_{[ab]}'] # LaTeX type notation
Of course, since $s$ is already antisymmetric:
s == s.antisymmetrize()
Contracting the type-(1,1) tensor $t$ with the module element $v$ results in another module element:
t.contract(v)
The components (w.r.t. a given basis) of the contraction are of course $t^i_{\\ j} v^j$:
t.contract(v)[i] == sum(t[i,j]*v[j] for j in M.irange())
This contraction coincides with the action of $t$ as an endomorphism:
t.contract(v) == tt(v)
Instead of contract()
, index notations can be used to denote the contraction:
t['^i_j']*v['j'] == t.contract(v)
Contracting the linear form $a$ with the module element $v$ results in a ring element:
a.contract(v)
It is of course the result of the linear form acting on the module element:
a.contract(v) == a(v)
By default, the contraction is performed on the last index of the first tensor and the first index of the second one. To perform contraction on other indices, one should specify the indices positions (with the convention position=0 for the first index): for instance to get the contraction $z^i_{\ \, j} = T^i_{\ \, kj} v^k$ (with $T=t\otimes a$):
z = ta.contract(1,v) # 1 -> second index of ta
print(z)
Type-(1,1) tensor on the Rank-3 free module M over the Integer Ring
To get $z^i_{\ \, jk} = t^l_{\ \, j} T^i_{\ \, l k}$:
z = t.contract(0, ta, 1) # 0 -> first index of t, 1 -> second index of ta
print(z)
Type-(1,2) tensor on the Rank-3 free module M over the Integer Ring
or, in terms of index notation:
z1 = t['^l_j']*ta['^i_lk']
z1 == z
As for any function, inline documentation is obtained via the question mark:
t.contract?
Signature: t.contract(*args) Docstring: Contraction on one or more indices with another tensor. INPUT: * "pos1" -- positions of the indices in "self" involved in the contraction; "pos1" must be a sequence of integers, with 0 standing for the first index position, 1 for the second one, etc; if "pos1" is not provided, a single contraction on the last index position of "self" is assumed * "other" -- the tensor to contract with * "pos2" -- positions of the indices in "other" involved in the contraction, with the same conventions as for "pos1"; if "pos2" is not provided, a single contraction on the first index position of "other" is assumed OUTPUT: * tensor resulting from the contraction at the positions "pos1" and "pos2" of "self" with "other" EXAMPLES: Contraction of a tensor of type (0,1) with a tensor of type (1,0): sage: M = FiniteRankFreeModule(ZZ, 3, name='M') sage: e = M.basis('e') sage: a = M.linear_form() # tensor of type (0,1) is a linear form sage: a[:] = [-3,2,1] sage: b = M([2,5,-2]) # tensor of type (1,0) is a module element sage: s = a.contract(b) ; s 2 sage: s in M.base_ring() True sage: s == a[0]*b[0] + a[1]*b[1] + a[2]*b[2] # check of the computation True The positions of the contraction indices can be set explicitly: sage: s == a.contract(0, b, 0) True sage: s == a.contract(0, b) True sage: s == a.contract(b, 0) True Instead of the explicit call to the method "contract()", the index notation can be used to specify the contraction, via Einstein convention (summation on repeated indices); it suffices to pass the indices as a string inside square brackets: sage: s1 = a['_i']*b['^i'] ; s1 2 sage: s1 == s True In the present case, performing the contraction is identical to applying the linear form to the module element: sage: a.contract(b) == a(b) True or to applying the module element, considered as a tensor of type (1,0), to the linear form: sage: a.contract(b) == b(a) True We have also: sage: a.contract(b) == b.contract(a) True Contraction of a tensor of type (1,1) with a tensor of type (1,0): sage: a = M.tensor((1,1)) sage: a[:] = [[-1,2,3],[4,-5,6],[7,8,9]] sage: s = a.contract(b) ; s Element of the Rank-3 free module M over the Integer Ring sage: s.display() 2 e_0 - 29 e_1 + 36 e_2 Since the index positions have not been specified, the contraction takes place on the last position of a (i.e. no. 1) and the first position of "b" (i.e. no. 0): sage: a.contract(b) == a.contract(1, b, 0) True sage: a.contract(b) == b.contract(0, a, 1) True sage: a.contract(b) == b.contract(a, 1) True Using the index notation with Einstein convention: sage: a['^i_j']*b['^j'] == a.contract(b) True The index "i" can be replaced by a dot: sage: a['^._j']*b['^j'] == a.contract(b) True and the symbol "^" may be omitted, the distinction between contravariant and covariant indices being the position with respect to the symbol "_": sage: a['._j']*b['j'] == a.contract(b) True Contraction is possible only between a contravariant index and a covariant one: sage: a.contract(0, b) Traceback (most recent call last): ... TypeError: contraction on two contravariant indices not permitted Contraction of a tensor of type (2,1) with a tensor of type (0,2): sage: a = a*b ; a Type-(2,1) tensor on the Rank-3 free module M over the Integer Ring sage: b = M.tensor((0,2)) sage: b[:] = [[-2,3,1], [0,-2,3], [4,-7,6]] sage: s = a.contract(1, b, 1) ; s Type-(1,2) tensor on the Rank-3 free module M over the Integer Ring sage: s[:] [[[-9, 16, 39], [18, -32, -78], [27, -48, -117]], [[36, -64, -156], [-45, 80, 195], [54, -96, -234]], [[63, -112, -273], [72, -128, -312], [81, -144, -351]]] Check of the computation: sage: all(s[i,j,k] == a[i,0,j]*b[k,0]+a[i,1,j]*b[k,1]+a[i,2,j]*b[k,2] ....: for i in range(3) for j in range(3) for k in range(3)) True Using index notation: sage: a['il_j']*b['_kl'] == a.contract(1, b, 1) True LaTeX notation are allowed: sage: a['^{il}_j']*b['_{kl}'] == a.contract(1, b, 1) True Indices not involved in the contraction may be replaced by dots: sage: a['.l_.']*b['_.l'] == a.contract(1, b, 1) True The two tensors do not have to be defined on the same basis for the contraction to take place, reflecting the fact that the contraction is basis-independent: sage: A = M.automorphism() sage: A[:] = [[0,0,1], [1,0,0], [0,-1,0]] sage: h = e.new_basis(A, 'h') sage: b.comp(h)[:] # forces the computation of b's components w.r.t. basis h [-2 -3 0] [ 7 6 -4] [ 3 -1 -2] sage: b.del_other_comp(h) # deletes components w.r.t. basis e sage: list(b._components) # indeed: [Basis (h_0,h_1,h_2) on the Rank-3 free module M over the Integer Ring] sage: list(a._components) # while a is known only in basis e: [Basis (e_0,e_1,e_2) on the Rank-3 free module M over the Integer Ring] sage: s1 = a.contract(1, b, 1) ; s1 # yet the computation is possible Type-(1,2) tensor on the Rank-3 free module M over the Integer Ring sage: s1 == s # ... and yields the same result as previously: True The contraction can be performed on more than a single index; for instance a 2-indices contraction of a type-(2,1) tensor with a type-(1,2) one is: sage: a # a is a tensor of type-(2,1) Type-(2,1) tensor on the Rank-3 free module M over the Integer Ring sage: b = M([1,-1,2])*b ; b # a tensor of type (1,2) Type-(1,2) tensor on the Rank-3 free module M over the Integer Ring sage: s = a.contract(1,2,b,1,0) ; s # the double contraction Type-(1,1) tensor on the Rank-3 free module M over the Integer Ring sage: s[:] [ -36 30 15] [-252 210 105] [-204 170 85] sage: s == a['^.k_l']*b['^l_k.'] # the same thing in index notation True Init docstring: Initialize self. See help(type(self)) for accurate signature. File: ~/sage/10.3/src/sage/tensor/modules/free_module_tensor.py Type: method