#!/usr/bin/env python # coding: utf-8 # # 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](https://sagemanifolds.obspm.fr/) project (version 1.3), which does not depend on other parts of SageManifolds and which has been integrated in SageMath 6.6. # # Click [here](https://raw.githubusercontent.com/sagemanifolds/SageManifolds/master/Worksheets/v1.3/SM_tensors_modules.ipynb) 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[1]: get_ipython().run_line_magic('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[2]: 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[3]: print(M) #

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[4]: M # In[5]: 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:

# In[6]: for i in M.irange(): print(i) #

If the parameter start_index had not been specified, the default range of the indices would have been $\{0,1,2\}$ instead:

# In[7]: M0 = FiniteRankFreeModule(ZZ, 3, name='M') for i in M0.irange(): print(i) #

$M$ is the category of finite dimensional modules over $\mathbb{Z}$:

# In[8]: print(M.category()) #

Self-inquiry commands are

# In[9]: M.base_ring() # In[10]: M.rank() # ## Defining bases on the free module # # At construction, the free module $M$ has no pre-defined basis: # In[11]: M.print_bases() # In[12]: M.bases() #

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

# In[13]: isinstance(M, CombinatorialFreeModule) # and $M$ does not belong to the category of modules with a distinguished basis: # In[14]: M in ModulesWithBasis(ZZ) # It simply belongs to the category of modules over $\mathbb{Z}$: # In[15]: M in Modules(ZZ) # More precisely, it belongs to the subcategory of finite dimensional modules over $\mathbb{Z}$: # In[16]: M in Modules(ZZ).FiniteDimensional() #

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

# In[17]: e = M.basis('e'); e # In[18]: M.print_bases() #

The elements of the basis are accessed via their indices:

# In[19]: e[1] # In[20]: print(e[1]) # In[21]: e[1] in M # In[22]: e[1].parent() # Let us introduce a second basis on the free module $M$ from a family of 3 linearly independent module elements: # In[23]: 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 # 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[24]: f[1].disp(e) # In[25]: f[2].disp(e) # In[26]: f[3].disp(e) # Conversely, the expression of basis $e$ is terms of basis $f$ is # In[27]: e[1].disp(f) # In[28]: e[2].disp(f) # In[29]: e[3].disp(f) # The module automorphism $a$ relating the two bases is obtained as # In[30]: a = M.change_of_basis(e,f) ; a # It belongs to the general linear group of the free module $M$: # In[31]: a.parent() #

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

# In[32]: a.matrix(e) #

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

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

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

# In[34]: M.change_of_basis(f,e) == a^(-1) # In[35]: (a^(-1)).matrix(f) # At this stage, two bases have been defined on $M$: # In[36]: M.print_bases() # 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[37]: f[1].display(e) #

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

# In[38]: f[1].display() #

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

# In[39]: f[1].display(f) # In[40]: 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():

# In[41]: M.set_default_basis(f) e[1].display() #

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

# In[42]: 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[43]: v = M([3,-4,1], basis=e, name='v') print(v) #

Since $e$ is the default basis, its mention can be skipped:

# In[44]: v = M([3,-4,1], name='v') print(v) # In[45]: v.disp() # 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[46]: v.disp(f) #

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

# In[47]: v.parent() #

We have also

# In[48]: v in M # Let us define a second module element, from the basis $f$ this time: # In[49]: u = M([-1,3,5], basis=f, name='u') u.disp(f) #

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

# In[50]: w = 2*e[1] - e[2] - 3*e[3] print(w) # In[51]: w.disp() # 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[52]: w.set_name('w', latex_name=r'\omega') w.disp() #

Module operations are implemented, independently of the bases:

# In[53]: s = u + 3*v print(s) # In[54]: s.disp() # In[55]: s.disp(f) # In[56]: s = u - v print(s) # In[57]: s.disp() # In[58]: s.disp(f) #

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

# In[59]: v.components(f) #

A shortcut is comp():

# In[60]: v.comp(f) is v.components(f) # In[61]: for i in M.irange(): print(v.comp(f)[i]) # In[62]: v.comp(f)[:] #

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

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

# In[64]: v[f,2] # In[65]: v[f,:] #

For the default basis, the basis can be omitted:

# In[66]: v[:] # In[67]: v[2] #

A specific module element is the zero one:

# In[68]: print(M.zero()) # In[69]: M.zero()[:] # In[70]: M.zero()[f,:] # In[71]: v + M.zero() == v # ## Linear forms # # Let us introduce some linear form on the free module $M$: # In[72]: a = M.linear_form('a') print(a) # $a$ is specified by its components with respect to the basis dual to $e$: # In[73]: a[:] = [2,-1,3] a.disp() # 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[74]: ed = e.dual_basis() ed # In[75]: print(ed[1]) # In[76]: ed[1](e[1]), ed[1](e[2]), ed[1](e[3]) # In[77]: ed[2](e[1]), ed[2](e[2]), ed[2](e[3]) # In[78]: 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$: # In[79]: a[f,:] = [2,-1,3] a.disp(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: # In[80]: a.disp() #

By definition, linear forms belong to the dual module:

# In[81]: a.parent() # In[82]: print(a.parent()) # In[83]: a.parent() is M.dual() #

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

# In[84]: isinstance(M.dual(), FiniteRankFreeModule) # In[85]: M.dual().rank() #

Linear forms map module elements to ring elements:

# In[86]: a(v) # In[87]: a(u) #

in a linear way:

# In[88]: a(u+2*v) == a(u) + 2*a(v) #

Alternating forms

#

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

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

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

# In[90]: c = a.wedge(b) print(c) c # In[91]: c.disp() # In[92]: c.disp(f) # In[93]: c(u,v) #

$c$ is antisymmetric:

# In[94]: c(v,u) #

and is multilinear:

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

# In[96]: c(u,v) == a(u)*b(v) - b(u)*a(v) #

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

# In[97]: 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^*)$:

# In[98]: c.parent() # In[99]: print(c.parent()) #

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

# In[100]: c.tensor_type() #

whose components with respect to any basis are antisymmetric:

# In[101]: c[:] # components with respect to the default basis (e) # In[102]: c[f,:] # components with respect to basis f # In[103]: c.comp(f) #

An alternating form can be constructed from scratch:

# In[104]: 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[105]: c1[1,2] = -108 c1[1,3] = -164 c1[2,3] = -53 #

Then

# In[106]: c1[:] # In[107]: c1 == c #

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

# In[108]: c.comp(e)._comp # In[109]: 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:

# In[110]: d = M.linear_form('d') d[:] = [-1,-2,4] s = d.wedge(c) print(s) # In[111]: s.disp() # In[112]: s.disp(f) # In[113]: s(e[1], e[2], e[3]) # In[114]: s(f[1], f[2], f[3]) #

$s$ is antisymmetric:

# In[115]: s(u,v,w), s(u,w,v), s(v,w,u), s(v,u,w), s(w,u,v), s(w,v,u) # ## 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[116]: M.tensor_module(1,0) is M # In[117]: v.tensor_type() #

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

# In[118]: a in M.tensor_module(0,1) # Note that, at the Python level, we do *not* have the identification of $T^{(0,1)}(M)$ with $M^*$: # In[119]: M.tensor_module(0,1) is M.dual() # This is because $T^{(0,1)}(M)$ and $M^*$ are different objects: # In[120]: type(M.tensor_module(0,1)) # In[121]: type(M.dual()) #

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

# In[122]: M.tensor_module(0,1).has_coerce_map_from(M.dual()) #

as well as coercion in the reverse direction:

# In[123]: M.dual().has_coerce_map_from(M.tensor_module(0,1)) #

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[124]: t = M.tensor((1,1), name='t') print(t) #

Let us set some component of $t$ in the basis $e$, for instance the component $t^1_{\ \, 2}$:

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

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

# In[126]: t[1,2] = -3 #

The unset components are zero:

# In[127]: t[:] #

Components can be set at any time:

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

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

# In[129]: t[f,:] #

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

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

# In[131]: t.display(f) #

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

# In[132]: t(a,v) # In[133]: 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} $$ # # In[134]: tt = End(M)(t) print(tt) # In[135]: 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$:

# In[136]: tt.matrix(e) # In[137]: t[e,:] # In[138]: tt.matrix(e) == t[e,:] # In[139]: tt.matrix(f) # In[140]: t[f,:] # In[141]: tt.matrix(f) == t[f,:] #

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

# In[142]: tt(v) # In[143]: tt(v).parent() # In[144]: tt(v).disp() #

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

# In[145]: t in M.tensor_module(1,1) #

or, in Sage terminology,

# In[146]: t.parent() is M.tensor_module(1,1) #

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

# In[147]: isinstance(M.tensor_module(1,1), FiniteRankFreeModule) # In[148]: M.tensor_module(1,1).base_ring() # In[149]: M.tensor_module(1,1).rank() #

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

#

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

# In[150]: ta = t*a print(ta) # In[151]: ta # In[152]: ta.disp() # In[153]: ta.disp(f) #

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

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

Each component ca be accessed individually:

# In[156]: ta[1,2,3] # access to a component w.r.t. the default basis # In[157]: ta[f,1,2,3] # In[158]: ta.parent() # In[159]: ta in M.tensor_module(1,2) #

The tensor product is not commutative:

# In[160]: print(a*t) # In[161]: a*t == t*a #

Forming a tensor of rank 4:

# In[162]: tav = ta*v print(tav) # In[163]: tav.disp() #

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[164]: 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[:] # In[165]: s = g.symmetrize() ; s # In[166]: s.symmetries() # In[167]: s[:] #

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

# In[168]: s == g.symmetrize(0,1) #

One may use index notation to specify the symmetry:

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

Of course, since $s$ is already symmetric:

# In[171]: s.symmetrize() == s #

The antisymmetrization proceeds accordingly:

# In[172]: s = g.antisymmetrize() ; s # In[173]: s.symmetries() # In[174]: s[:] # In[175]: s == g.antisymmetrize(0,1) #

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

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

Of course, since $s$ is already antisymmetric:

# In[178]: s == s.antisymmetrize() #

Tensor contractions

#

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

# In[179]: t.contract(v) #

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

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

# In[181]: t.contract(v) == tt(v) #

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

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

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

# In[183]: a.contract(v) #

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

# In[184]: 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$):

# In[185]: z = ta.contract(1,v) # 1 -> second index of ta print(z) #

To get $z^i_{\ \, jk} = t^l_{\ \, j} T^i_{\ \, l k}$:

# In[186]: z = t.contract(0, ta, 1) # 0 -> first index of t, 1 -> second index of ta print(z) #

or, in terms of index notation:

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

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

# In[188]: get_ipython().run_line_magic('pinfo', 't.contract') # In[ ]: