# Tensors on free modules¶

## A tutorial¶

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 (version 1.3), which does not depend on other parts of SageManifolds and which has been integrated in SageMath 6.6.

Click here to download the notebook file (ipynb format). To run it, you must start SageMath with the Jupyter notebook, via the command sage -n jupyter

First we set up the notebook to display mathematical objects using LaTeX rendering:

In :
%display latex


## Constructing a free module of finite rank¶

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

In :
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 function print returns a short description of the just constructed module:

In :
print(M)

Rank-3 free module M over the Integer Ring


If we ask just for M,  the module's LaTeX symbol is returned (provided that the worksheet's Typeset box has been selected); by default, this is the same as the argument name in the constructor (this can be changed by providing the optional argument latex_name):

In :
M

Out:
In :
M1 = FiniteRankFreeModule(ZZ, 3, name='M', latex_name=r'\mathcal{M}', start_index=1)
M1

Out:

The indices of basis elements or tensor components on the module are generated by the method irange(), to be used in loops:

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

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

In :
print(M.category())

Category of finite dimensional modules over Integer Ring


Self-inquiry commands are

In :
M.base_ring()

Out:
In :
M.rank()

Out:

## Defining bases on the free module¶

At construction, the free module $M$ has no pre-defined basis:

In :
M.print_bases()

No basis has been defined on the Rank-3 free module M over the Integer Ring

In :
M.bases()

Out:

For this reason, the class FiniteRankFreeModule does not inherit from Sage class CombinatorialFreeModule:

In :
isinstance(M, CombinatorialFreeModule)

Out:

and $M$ does not belong to the category of modules with a distinguished basis:

In :
M in ModulesWithBasis(ZZ)

Out:

It simply belongs to the category of modules over $\mathbb{Z}$:

In :
M in Modules(ZZ)

Out:

More precisely, it belongs to the subcategory of finite dimensional modules over $\mathbb{Z}$:

In :
M in Modules(ZZ).FiniteDimensional()

Out:

We define a first basis on $M$ as follows:

In :
e = M.basis('e'); e

Out:
In :
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:

In :
e

Out:
In :
print(e)

Element e_1 of the Rank-3 free module M over the Integer Ring

In :
e in M

Out:
In :
e.parent()

Out:

Let us introduce a second basis on the free module $M$ from a family of 3 linearly independent module elements:

In :
f = M.basis('f', from_family=(-e+2*e-4*e, e+2*e, e+3*e))
print(f) ; f

Basis (f_1,f_2,f_3) on the Rank-3 free module M over the Integer Ring

Out:

We may ask to view each element of basis $f$ in terms of its expansion onto basis $e$, via the method display(), abridged as disp():

In :
f.disp(e)

Out:
In :
f.disp(e)

Out:
In :
f.disp(e)

Out:

Conversely, the expression of basis $e$ is terms of basis $f$ is

In :
e.disp(f)

Out:
In :
e.disp(f)

Out:
In :
e.disp(f)

Out:

The module automorphism $a$ relating the two bases is obtained as

In :
a = M.change_of_basis(e,f) ; a

Out:

It belongs to the general linear group of the free module $M$:

In :
a.parent()

Out:

and its matrix w.r.t. basis $e$ is

In :
a.matrix(e)

Out:

Let us check that the elements of basis $f$ are images of the elements of basis $e$ via $a$:

In :
all([f[i] == a(e[i]) for i in M.irange()])

Out:

The reverse change of basis is of course the inverse automorphism:

In :
M.change_of_basis(f,e) == a^(-1)

Out:
In :
(a^(-1)).matrix(f)

Out:

At this stage, two bases have been defined on $M$:

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

In :
f.display(e)

Out:

Since $e$ is the default basis, the above command is fully equivalent to

In :
f.display()

Out:

Of course, the names of non-default bases have to be specified:

In :
f.display(f)

Out:
In :
e.display(f)

Out:

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

In :
M.set_default_basis(f)
e.display()

Out:

Let us revert to $e$ as the default basis:

In :
M.set_default_basis(e)


## Module elements¶

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:

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

In :
v = M([3,-4,1], name='v')
print(v)

Element v of the Rank-3 free module M over the Integer Ring

In :
v.disp()

Out:

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:

In :
v.disp(f)

Out:

According to Sage terminology, the parent of $v$ is of course $M$:

In :
v.parent()

Out:

We have also

In :
v in M

Out:

Let us define a second module element, from the basis $f$ this time:

In :
u = M([-1,3,5], basis=f, name='u')
u.disp(f)

Out:

Another way to define module elements is of course via linear combinations:

In :
w = 2*e - e - 3*e
print(w)

Element of the Rank-3 free module M over the Integer Ring

In :
w.disp()

Out:

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:

In :
w.set_name('w', latex_name=r'\omega')
w.disp()

Out:

Module operations are implemented, independently of the bases:

In :
s = u + 3*v
print(s)

Element of the Rank-3 free module M over the Integer Ring

In :
s.disp()

Out:
In :
s.disp(f)

Out:
In :
s = u - v
print(s)

Element u-v of the Rank-3 free module M over the Integer Ring

In :
s.disp()

Out:
In :
s.disp(f)

Out:

The components of a module element with respect to a given basis are given by the method components():

In :
v.components(f)

Out:

A shortcut is comp():

In :
v.comp(f) is v.components(f)

Out:
In :
for i in M.irange():
print(v.comp(f)[i])

-3
17
-15

In :
v.comp(f)[:]

Out:

The function display_comp() provides a list of components w.r.t. to a given basis:

In :
v.display_comp(f)

Out:

As a shortcut, instead of calling the method comp(), the basis can be provided as the first argument of the square bracket operator:

In :
v[f,2]

Out:
In :
v[f,:]

Out:

For the default basis, the basis can be omitted:

In :
v[:]

Out:
In :
v

Out:

A specific module element is the zero one:

In :
print(M.zero())

Element zero of the Rank-3 free module M over the Integer Ring

In :
M.zero()[:]

Out:
In :
M.zero()[f,:]

Out:
In :
v + M.zero() == v

Out:

## Linear forms¶

Let us introduce some linear form on the free module $M$:

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

In :
a[:] = [2,-1,3]
a.disp()

Out:

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

In :
ed = e.dual_basis()
ed

Out:
In :
print(ed)

Linear form e^1 on the Rank-3 free module M over the Integer Ring

In :
ed(e), ed(e), ed(e)

Out:
In :
ed(e), ed(e), ed(e)

Out:
In :
ed(e), ed(e), ed(e)

Out:

The linear form $a$ can also be defined by its components with respect to the basis dual to $f$:

In :
a[f,:] = [2,-1,3]
a.disp(f)

Out:

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:

In :
a.disp()

Out:

By definition, linear forms belong to the dual module:

In :
a.parent()

Out:
In :
print(a.parent())

Dual of the Rank-3 free module M over the Integer Ring

In :
a.parent() is M.dual()

Out:

The dual module is itself a free module of the same rank as $M$:

In :
isinstance(M.dual(), FiniteRankFreeModule)

Out:
In :
M.dual().rank()

Out:

Linear forms map module elements to ring elements:

In :
a(v)

Out:
In :
a(u)

Out:

in a linear way:

In :
a(u+2*v) == a(u) + 2*a(v)

Out:

## Alternating forms

Let us introduce a second linear form, $b$, on the free module $M$:

In :
b = M.linear_form('b')
b[:] = [-4,2,5]


and take its exterior product with the linear form $a$:

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

Out:
In :
c.disp()

Out:
In :
c.disp(f)

Out:
In :
c(u,v)

Out:

$c$ is antisymmetric:

In :
c(v,u)

Out:

and is multilinear:

In :
c(u+4*w,v) == c(u,v) + 4*c(w,v)

Out:

We may check the standard formula for the exterior product of two linear forms:

In :
c(u,v) == a(u)*b(v) - b(u)*a(v)

Out:

In terms of tensor product (denoted here by *), it reads

In :
c == a*b - b*a

Out:

The parent of the alternating form $c$ is the second external power of the dual module $M^*$, which is denoted by $\Lambda^2(M^*)$:

In :
c.parent()

Out:
In :
print(c.parent())

2nd exterior power of the dual of the Rank-3 free module M over the Integer Ring


$c$ is a tensor field of type $(0,2)$:

In :
c.tensor_type()

Out:

whose components with respect to any basis are antisymmetric:

In :
c[:]  # components with respect to the default basis (e)

Out:
In :
c[f,:] # components with respect to basis f

Out:
In :
c.comp(f)

Out:

An alternating form can be constructed from scratch:

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

In :
c1[1,2] = -108
c1[1,3] = -164
c1[2,3] = -53


Then

In :
c1[:]

Out:
In :
c1 == c

Out:

Internally, only non-redundant components are stored, in a dictionary whose keys are the indices:

In :
c.comp(e)._comp

Out:
In :
c.comp(f)._comp

Out:

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:

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

In :
s.disp()

Out:
In :
s.disp(f)

Out:
In :
s(e, e, e)

Out:
In :
s(f, f, f)

Out:

$s$ is antisymmetric:

In :
s(u,v,w), s(u,w,v), s(v,w,u), s(v,u,w), s(w,u,v), s(w,v,u)

Out:

## Tensors¶

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

In :
M.tensor_module(1,0) is M

Out:
In :
v.tensor_type()

Out:

According to the above definition, linear forms are tensors of type (0,1):

In :
a in M.tensor_module(0,1)

Out:

Note that, at the Python level, we do not have the identification of $T^{(0,1)}(M)$ with $M^*$:

In :
M.tensor_module(0,1) is M.dual()

Out:

This is because $T^{(0,1)}(M)$ and $M^*$ are different objects:

In :
type(M.tensor_module(0,1))

Out:
In :
type(M.dual())

Out:

However, we have coercion (automatic conversion) of elements of $M^*$ into elements of $T^{(0,1)}(M)$:

In :
M.tensor_module(0,1).has_coerce_map_from(M.dual())

Out:

as well as coercion in the reverse direction:

In :
M.dual().has_coerce_map_from(M.tensor_module(0,1))

Out:

Arbitrary tensors are constructed via the module method tensor(), by providing the tensor type $(k,l)$ and possibly the symbol to denote the tensor:

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

In :
t[e,1,2] = -3


Since $e$ is the default basis, a shortcut for the above is

In :
t[1,2] = -3


The unset components are zero:

In :
t[:]

Out:

Components can be set at any time:

In :
t[2,3] = 4
t[:]

Out:

The components with respect to the basis $f$ are evaluated by the change-of-basis formula $e\rightarrow f$:

In :
t[f,:]

Out:

Another view of $t$, which reflects the fact that $T^{(1,1)}(M) = M\otimes M^*$, is

In :
t.display()

Out:

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

In :
t.display(f)

Out:

As a tensor of type (1,1), $t$ maps pairs (linear form, module element) to ring elements:

In :
t(a,v)

Out:
In :
t(a,v).parent()

Out:

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}$$
In :
tt = End(M)(t)
print(tt)

Generic endomorphism of Rank-3 free module M over the Integer Ring

In :
tt.parent()

Out:

In a given basis, the matrix ${\tilde t}^i_{\ \, j}$ of the endomorphism $\tilde t$ is identical to the matrix of the tensor $t$:

In :
tt.matrix(e)

Out:
In :
t[e,:]

Out:
In :
tt.matrix(e) == t[e,:]

Out:
In :
tt.matrix(f)

Out:
In :
t[f,:]

Out:
In :
tt.matrix(f) == t[f,:]

Out:

As an endomorphism, $t$ maps module elements to module elements:

In :
tt(v)

Out:
In :
tt(v).parent()

Out:
In :
tt(v).disp()

Out:

$t$ belongs to the module $T^{(1,1)}(M)$:

In :
t in M.tensor_module(1,1)

Out:

or, in Sage terminology,

In :
t.parent() is M.tensor_module(1,1)

Out:

$T^{(1,1)}(M)$ is itself a free module of finite rank over $\mathbb{Z}$:

In :
isinstance(M.tensor_module(1,1), FiniteRankFreeModule)

Out:
In :
M.tensor_module(1,1).base_ring()

Out:
In :
M.tensor_module(1,1).rank()

Out:

## Tensor calculus

In addition to the arithmetic operations inherent to the module structure of $T^{(k,l)}(M)$, the following operations are implemented:

• tensor product
• symmetrization and antisymmetrization
• tensor contraction

### Tensor product

The tensor product is formed with the * operator. For instance the tensor product $t\otimes a$ is

In :
ta = t*a
print(ta)

Type-(1,2) tensor t*a on the Rank-3 free module M over the Integer Ring

In :
ta

Out:
In :
ta.disp()

Out:
In :
ta.disp(f)

Out:

The components w.r.t. a given basis can also be displayed as an array:

In :
ta[:] # components w.r.t. the default basis (e)

Out:
In :
ta[f,:] # components w.r.t. basis f

Out:

Each component ca be accessed individually:

In :
ta[1,2,3] # access to a component w.r.t. the default basis

Out:
In :
ta[f,1,2,3]

Out:
In :
ta.parent()

Out:
In :
ta in M.tensor_module(1,2)

Out:

The tensor product is not commutative:

In :
print(a*t)

Type-(1,2) tensor a*t on the Rank-3 free module M over the Integer Ring

In :
a*t == t*a

Out:

Forming a tensor of rank 4:

In :
tav = ta*v
print(tav)

Type-(2,2) tensor t*a*v on the Rank-3 free module M over the Integer Ring

In :
tav.disp()

Out:

### Symmetrization / antisymmetrization

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:

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

Out:
In :
s = g.symmetrize() ; s

Out:
In :
s.symmetries()

symmetry: (0, 1); no antisymmetry

In :
s[:]

Out:

Symmetrization can be performed on an arbitray number of arguments, by providing their positions (first position = 0). In the present case

In :
s == g.symmetrize(0,1)

Out:

One may use index notation to specify the symmetry:

In :
s == g['_(ab)']

Out:
In :
s == g['_{(ab)}'] # LaTeX type notation

Out:

Of course, since $s$ is already symmetric:

In :
s.symmetrize() == s

Out:

The antisymmetrization proceeds accordingly:

In :
s = g.antisymmetrize() ; s

Out:
In :
s.symmetries()

no symmetry; antisymmetry: (0, 1)

In :
s[:]

Out:
In :
s == g.antisymmetrize(0,1)

Out:

As for symmetries, index notation can be used, instead of antisymmetrize():

In :
s == g['_[ab]']

Out:
In :
s == g['_{[ab]}'] # LaTeX type notation

Out:

Of course, since $s$ is already antisymmetric:

In :
s == s.antisymmetrize()

Out:

### Tensor contractions

Contracting the type-(1,1) tensor $t$ with the module element $v$ results in another module element:

In :
t.contract(v)

Out:

The components (w.r.t. a given basis) of the contraction are of course $t^i_{\\ j} v^j$:

In :
t.contract(v)[i] == sum(t[i,j]*v[j] for j in M.irange())

Out:

This contraction coincides with the action of $t$ as an endomorphism:

In :
t.contract(v) == tt(v)

Out:

Instead of contract(), index notations can be used to denote the contraction:

In :
t['^i_j']*v['j'] == t.contract(v)

Out:

Contracting the linear form $a$ with the module element $v$ results in a ring element:

In :
a.contract(v)

Out:

It is of course the result of the linear form acting on the module element:

In :
a.contract(v) == a(v)

Out:

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

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

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

In :
z1 = t['^l_j']*ta['^i_lk']
z1 == z

Out:

As for any function, inline documentation is obtained via the quotation mark:

In :
t.contract?

In [ ]: