Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Github repo cloud-examples: https://github.com/sagemath/cloud-examples

Views: 8060
License: MIT
%auto typeset_mode(True, display=False)

Tensors on free modules

  A tutorial

This Sage worksheet provides some introduction to tensors on free modules of finite rank. This is a pure algebraic subpart of SageManifolds (version 0.8), which does not depend on other parts of SageManifolds and which has been integrated in Sage 6.6.

This worksheet is released under the GNU General Public License version 2.

(c) Eric Gourgoulhon, Michal Bejger (2015)

The whole worksheet file can be downloaded from here.

Constructing a free module of finite rank

Let RR be a commutative ring and MM a free module of finite rank over RR, i.e. a module over RR that admits a finite basis (finite family of linearly independent generators). Since RR is commutative, it has the invariant basis number property: all bases of MM have the same cardinality, which is called the rank of MM. In this tutorial, we consider a free module of rank 3 over the integer ring Z\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}\{1,2,3\}. The default value is start_index=0.

The command print 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 (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):

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

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}\{0,1,2\} instead:

M0 = FiniteRankFreeModule(ZZ, 3, name='M') for i in M0.irange(): print i,
0 1 2

MM is the category of modules over Z\mathbb{Z}:

M.category()
ModulesZ\mathbf{Modules}_{\Bold{Z}}

Self-inquiry commands are

M.base_ring()
Z\Bold{Z}
M.rank()
33

Defining bases on the free module

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

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)
M.bases()
[(e1,e2,e3)\left(e_1,e_2,e_3\right), (f1,f2,f3)\left(f_1,f_2,f_3\right)]

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

isinstance(M, CombinatorialFreeModule)
False\mathrm{False}

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

M in ModulesWithBasis(ZZ)
False\mathrm{False}

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

M in Modules(ZZ)
True\mathrm{True}

We define a first basis on MM as follows:

e = M.basis('e') ; e
(e1,e2,e3)\left(e_1,e_2,e_3\right)
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 elements of the basis are accessed via their indices:

e[1]
e1e_1
print e[1]
Element e_1 of the Rank-3 free module M over the Integer Ring
e[1] in M
True\mathrm{True}
e[1].parent()
MM

Let us introduce a second basis on the free module MM 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
(f1,f2,f3)\left(f_1,f_2,f_3\right)

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

f[1].disp(e)
f1=e1+2e24e3f_1 = -e_1 + 2 e_2 -4 e_3
f[2].disp(e)
f2=e2+2e3f_2 = e_2 + 2 e_3
f[3].disp(e)
f3=e2+3e3f_3 = e_2 + 3 e_3

Conversely, the expression of basis ee is terms of basis ff is

e[1].disp(f)
e1=f1+10f28f3e_1 = -f_1 + 10 f_2 -8 f_3
e[2].disp(f)
e2=3f22f3e_2 = 3 f_2 -2 f_3
e[3].disp(f)
e3=f2+f3e_3 = -f_2+f_3

The module automorphism aa relating the two bases is obtained as

a = M.change_of_basis(e,f) ; a
Automorphism of the Rank-3 free module M over the Integer Ring\mbox{Automorphism of the Rank-3 free module M over the Integer Ring}

It belongs to the general linear group of the free module MM:

a.parent()
GL(M)\mathrm{GL}\left( M \right)

and its matrix w.r.t. basis ee is

a.matrix(e)
(100211423)\left(\begin{array}{rrr} -1 & 0 & 0 \\ 2 & 1 & 1 \\ -4 & 2 & 3 \end{array}\right)

Let us check that the elements of basis ff are images of the elements of basis ee via aa:

f[1] == a(e[1]), f[2] == a(e[2]), f[3] == a(e[3])
(True\mathrm{True}, True\mathrm{True}, True\mathrm{True})

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

M.change_of_basis(f,e) == a^(-1)
True\mathrm{True}
(a^(-1)).matrix(f)
(1001031821)\left(\begin{array}{rrr} -1 & 0 & 0 \\ 10 & 3 & -1 \\ -8 & -2 & 1 \end{array}\right)

At this stage, two bases have been defined on MM:

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, ee, 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)
f1=e1+2e24e3f_1 = -e_1 + 2 e_2 -4 e_3

Since ee is the default basis, the above command is fully equivalent to

f[1].display()
f1=e1+2e24e3f_1 = -e_1 + 2 e_2 -4 e_3

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

f[1].display(f)
f1=f1f_1 = f_1
e[1].display(f)
e1=f1+10f28f3e_1 = -f_1 + 10 f_2 -8 f_3

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()
e1=f1+10f28f3e_1 = -f_1 + 10 f_2 -8 f_3

Let us revert to ee as the default basis:

M.set_default_basis(e)

Module elements

Elements of the free module MM 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 ee 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.disp()
v=3e14e2+e3v = 3 e_1 -4 e_2 +e_3

While vv has been defined from the basis ee, its expression in terms of the basis ff can be evaluated, thanks to the known relation between the two bases:

v.disp(f)
v=3f1+17f215f3v = -3 f_1 + 17 f_2 -15 f_3

According to Sage terminology, the parent of vv is of course MM:

v.parent()
MM

We have also

v in M
True\mathrm{True}

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

u = M([-1,3,5], basis=f, name='u') u.disp(f)
u=f1+3f2+5f3u = -f_1 + 3 f_2 + 5 f_3

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.disp()
2e1e23e32 e_1 -e_2 -3 e_3

As the result of a linear combination, ww 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.disp()
ω=2e1e23e3\omega = 2 e_1 -e_2 -3 e_3

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.disp()
10e16e2+28e310 e_1 -6 e_2 + 28 e_3
s.disp(f)
10f1+54f240f3-10 f_1 + 54 f_2 -40 f_3
s = u - v print s
Element u-v of the Rank-3 free module M over the Integer Ring
s.disp()
uv=2e1+10e2+24e3u-v = -2 e_1 + 10 e_2 + 24 e_3
s.disp(f)
uv=2f114f2+20f3u-v = 2 f_1 -14 f_2 + 20 f_3

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

v.components(f)
1-index components w.r.t. Basis (f_1,f_2,f_3) on the Rank-3 free module M over the Integer Ring\text{\texttt{1{-}index{ }components{ }w.r.t.{ }Basis{ }(f{\char`\_}1,f{\char`\_}2,f{\char`\_}3){ }on{ }the{ }Rank{-}3{ }free{ }module{ }M{ }over{ }the{ }Integer{ }Ring}}

A shortcut is comp():

v.comp(f) is v.components(f)
True\mathrm{True}
for i in M.irange(): print v.comp(f)[i],
-3 17 -15
v.comp(f)[:]
[3-3, 1717, 15-15]

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

v.display_comp(f)
v11=3v22=17v33=15\begin{array}{lcl} v_{\phantom{\, 1}}^{\,1} & = & -3 \\ v_{\phantom{\, 2}}^{\,2} & = & 17 \\ v_{\phantom{\, 3}}^{\,3} & = & -15 \end{array}

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]
1717
v[f,:]
[3-3, 1717, 15-15]

For the default basis, the basis can be omitted:

v[:]
[33, 4-4, 11]
v[2]
4-4

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()[:]
[00, 00, 00]
M.zero()[f,:]
[00, 00, 00]
v + M.zero() == v
True\mathrm{True}

Linear forms

Let us introduce some linear form on the free module MM:

a = M.linear_form('a') print a
Linear form a on the Rank-3 free module M over the Integer Ring

aa is specified by its components with respect to the basis dual to ee:

a[:] = [2,-1,3] a.disp()
a=2e1e2+3e3a = 2 e^1 -e^2 + 3 e^3

The notation eie^i stands for the elements of the basis dual to ee, i.e. the basis of the dual module MM^* such that ei(ej)=δ ji e^i(e_j) = \delta^i_{\ \, j}

Indeed

ed = e.dual_basis() ed
(e1,e2,e3)\left(e^1,e^2,e^3\right)
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])
(11, 00, 00)
ed[2](e[1]), ed[2](e[2]), ed[2](e[3])
(00, 11, 00)
ed[3](e[1]), ed[3](e[2]), ed[3](e[3])
(00, 00, 11)

The linear form aa can also be defined by its components with respect to the basis dual to ff:

a[f,:] = [2,-1,3] a.disp(f)
a=2f1f2+3f3a = 2 f^1 -f^2 + 3 f^3

For consistency, the previously defined components with respect to the basis dual to ee are automatically deleted and new ones are computed from the change-of-basis formula:

a.disp()
a=36e19e2+4e3a = -36 e^1 -9 e^2 + 4 e^3

By definition, linear forms belong to the dual module:

a.parent()
MM^*
print a.parent()
Dual of the Rank-3 free module M over the Integer Ring
a.parent() is M.dual()
True\mathrm{True}

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

isinstance(M.dual(), FiniteRankFreeModule)
True\mathrm{True}
M.dual().rank()
33

Linear forms map module elements to ring elements:

a(v)
68-68
a(u)
1010

in a linear way:

a(u+2*v) == a(u) + 2*a(v)
True\mathrm{True}

Alternating forms

Let us introduce a second linear form, bb, on the free module MM:

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

and take its exterior product with the linear form aa:

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
aba\wedge b
c.disp()
ab=108e1e2164e1e353e2e3a\wedge b = -108 e^1\wedge e^2 -164 e^1\wedge e^3 -53 e^2\wedge e^3
c.disp(f)
ab=12f1f2+70f1f353f2f3a\wedge b = 12 f^1\wedge f^2 + 70 f^1\wedge f^3 -53 f^2\wedge f^3
c(u,v)
88948894

cc is antisymmetric:

c(v,u)
8894-8894

and is multilinear:

c(u+4*w,v) == c(u,v) + 4*c(w,v)
True\mathrm{True}

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)
True\mathrm{True}

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

c == a*b - b*a
True\mathrm{True}

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

c.parent()
Λ2(M)\Lambda^{2}\left(M^*\right)
print c.parent()
2nd exterior power of the dual of the Rank-3 free module M over the Integer Ring

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

c.tensor_type()
(00, 22)

whose components with respect to any basis are antisymmetric:

c[:] # components with respect to the default basis (e)
(0108164108053164530)\left(\begin{array}{rrr} 0 & -108 & -164 \\ 108 & 0 & -53 \\ 164 & 53 & 0 \end{array}\right)
c[f,:] # components with respect to basis f
(012701205370530)\left(\begin{array}{rrr} 0 & 12 & 70 \\ -12 & 0 & -53 \\ -70 & 53 & 0 \end{array}\right)
c.comp(f)
Fully antisymmetric 2-indices components w.r.t. Basis (f_1,f_2,f_3) on the Rank-3 free module M over the Integer Ring\text{\texttt{Fully{ }antisymmetric{ }2{-}indices{ }components{ }w.r.t.{ }Basis{ }(f{\char`\_}1,f{\char`\_}2,f{\char`\_}3){ }on{ }the{ }Rank{-}3{ }free{ }module{ }M{ }over{ }the{ }Integer{ }Ring}}

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[:]
(0108164108053164530)\left(\begin{array}{rrr} 0 & -108 & -164 \\ 108 & 0 & -53 \\ 164 & 53 & 0 \end{array}\right)
c1 == c
True\mathrm{True}

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

c.comp(e)._comp
{(1,2):108,(1,3):164,(2,3):53}\left\{\left(1, 2\right) : -108, \left(1, 3\right) : -164, \left(2, 3\right) : -53\right\}
c.comp(f)._comp
{(1,2):12,(1,3):70,(2,3):53}\left\{\left(1, 2\right) : 12, \left(1, 3\right) : 70, \left(2, 3\right) : -53\right\}

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.disp()
dab=707e1e2e3d\wedge a\wedge b = -707 e^1\wedge e^2\wedge e^3
s.disp(f)
dab=707f1f2f3d\wedge a\wedge b = 707 f^1\wedge f^2\wedge f^3
s(e[1], e[2], e[3])
707-707
s(f[1], f[2], f[3])
707707

ss 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)
(144228-144228, 144228144228, 144228-144228, 144228144228, 144228-144228, 144228144228)

Tensors

kk and ll being non negative integers, a tensor of type (k,l)(k,l) on the free module MM 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 RR is Z\mathbb{Z}.

For free modules of finite rank, we have the canonical isomorphism MMM^{**} \simeq M, so that the set of all tensors of type (k,l)(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)(1,0) are identified with elements of MM:

M.tensor_module(1,0) is M
True\mathrm{True}
v.tensor_type()
(11, 00)

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

a in M.tensor_module(0,1)
True\mathrm{True}

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

M.tensor_module(0,1) is M.dual()
False\mathrm{False}

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

type(M.tensor_module(0,1))
<class 'sage.tensor.modules.tensor_free_module.TensorFreeModule_with_category'>
type(M.dual())
<class 'sage.tensor.modules.ext_pow_free_module.ExtPowerFreeModule_with_category'>

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

M.tensor_module(0,1).has_coerce_map_from(M.dual())
True\mathrm{True}

as well as coercion in the reverse direction:

M.dual().has_coerce_map_from(M.tensor_module(0,1))
True\mathrm{True}

Arbitrary tensors are constructed via the module method tensor(), by providing the tensor type (k,l)(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 tt in the basis ee, for instance the component t 21t^1_{\ \, 2}:

t[e,1,2] = -3

Since ee is the default basis, a shortcut for the above is

t[1,2] = -3

The unset components are zero:

t[:]
(030000000)\left(\begin{array}{rrr} 0 & -3 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{array}\right)

Components can be set at any time:

t[2,3] = 4 t[:]
(030004000)\left(\begin{array}{rrr} 0 & -3 & 0 \\ 0 & 0 & 4 \\ 0 & 0 & 0 \end{array}\right)

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

t[f,:]
(633108668080)\left(\begin{array}{rrr} 6 & 3 & 3 \\ -108 & -6 & 6 \\ 80 & 8 & 0 \end{array}\right)

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

t.disp()
t=3e1e2+4e2e3t = -3 e_1\otimes e^2 + 4 e_2\otimes e^3

Recall that (ei)(e^i) is the basis of MM^* that is dual to the basis (ei)(e_i) of MM.

In term of the basis (fi)(f_i) and its dual basis (fi)(f^i), we have

t.disp(f)
t=6f1f1+3f1f2+3f1f3108f2f16f2f2+6f2f3+80f3f1+8f3f2t = 6 f_1\otimes f^1 + 3 f_1\otimes f^2 + 3 f_1\otimes f^3 -108 f_2\otimes f^1 -6 f_2\otimes f^2 + 6 f_2\otimes f^3 + 80 f_3\otimes f^1 + 8 f_3\otimes f^2

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

t(a,v)
468-468
t(a,v).parent()
Z\Bold{Z}

Tensors of type (1,1) can be considered as endomorphisms, thanks to the isomorphism

End(M)T(1,1)(M)t~ t:M×MR    (a,v)a(t~(v)) \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()
Hom(M,M)\mathrm{Hom}\left(M,M\right)

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

tt.matrix(e)
(030004000)\left(\begin{array}{rrr} 0 & -3 & 0 \\ 0 & 0 & 4 \\ 0 & 0 & 0 \end{array}\right)
t[e,:]
(030004000)\left(\begin{array}{rrr} 0 & -3 & 0 \\ 0 & 0 & 4 \\ 0 & 0 & 0 \end{array}\right)
tt.matrix(e) == t[e,:]
True\mathrm{True}
tt.matrix(f)
(633108668080)\left(\begin{array}{rrr} 6 & 3 & 3 \\ -108 & -6 & 6 \\ 80 & 8 & 0 \end{array}\right)
t[f,:]
(633108668080)\left(\begin{array}{rrr} 6 & 3 & 3 \\ -108 & -6 & 6 \\ 80 & 8 & 0 \end{array}\right)
tt.matrix(f) == t[f,:]
True\mathrm{True}

As an endomorphism, tt maps module elements to module elements:

tt(v)
t(v)t\left(v\right)
tt(v).parent()
MM
tt(v).disp()
t(v)=12e1+4e2t\left(v\right) = 12 e_1 + 4 e_2

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

t in M.tensor_module(1,1)
True\mathrm{True}

or, in Sage terminology,

t.parent() is M.tensor_module(1,1)
True\mathrm{True}

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

isinstance(M.tensor_module(1,1), FiniteRankFreeModule)
True\mathrm{True}
M.tensor_module(1,1).base_ring()
Z\Bold{Z}
M.tensor_module(1,1).rank()
99

Tensor calculus

In addition to the arithmetic operations inherent to the module structure of T(k,l)(M)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 tat\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
tat\otimes a
ta.disp()
ta=108e1e2e1+27e1e2e212e1e2e3144e2e3e136e2e3e2+16e2e3e3t\otimes a = 108 e_1\otimes e^2\otimes e^1 + 27 e_1\otimes e^2\otimes e^2 -12 e_1\otimes e^2\otimes e^3 -144 e_2\otimes e^3\otimes e^1 -36 e_2\otimes e^3\otimes e^2 + 16 e_2\otimes e^3\otimes e^3
ta.disp(f)
ta=12f1f1f16f1f1f2+18f1f1f3+6f1f2f13f1f2f2+9f1f2f3+6f1f3f13f1f3f2+9f1f3f3216f2f1f1+108f2f1f2324f2f1f312f2f2f1+6f2f2f218f2f2f3+12f2f3f16f2f3f2+18f2f3f3+160f3f1f180f3f1f2+240f3f1f3+16f3f2f18f3f2f2+24f3f2f3t\otimes a = 12 f_1\otimes f^1\otimes f^1 -6 f_1\otimes f^1\otimes f^2 + 18 f_1\otimes f^1\otimes f^3 + 6 f_1\otimes f^2\otimes f^1 -3 f_1\otimes f^2\otimes f^2 + 9 f_1\otimes f^2\otimes f^3 + 6 f_1\otimes f^3\otimes f^1 -3 f_1\otimes f^3\otimes f^2 + 9 f_1\otimes f^3\otimes f^3 -216 f_2\otimes f^1\otimes f^1 + 108 f_2\otimes f^1\otimes f^2 -324 f_2\otimes f^1\otimes f^3 -12 f_2\otimes f^2\otimes f^1 + 6 f_2\otimes f^2\otimes f^2 -18 f_2\otimes f^2\otimes f^3 + 12 f_2\otimes f^3\otimes f^1 -6 f_2\otimes f^3\otimes f^2 + 18 f_2\otimes f^3\otimes f^3 + 160 f_3\otimes f^1\otimes f^1 -80 f_3\otimes f^1\otimes f^2 + 240 f_3\otimes f^1\otimes f^3 + 16 f_3\otimes f^2\otimes f^1 -8 f_3\otimes f^2\otimes f^2 + 24 f_3\otimes f^2\otimes f^3

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

ta[:] # components w.r.t. the default basis (e)
[[[00, 00, 00], [108108, 2727, 12-12], [00, 00, 00]], [[00, 00, 00], [00, 00, 00], [144-144, 36-36, 1616]], [[00, 00, 00], [00, 00, 00], [00, 00, 00]]]
ta[f,:] # components w.r.t. basis f
[[[1212, 6-6, 1818], [66, 3-3, 99], [66, 3-3, 99]], [[216-216, 108108, 324-324], [12-12, 66, 18-18], [1212, 6-6, 1818]], [[160160, 80-80, 240240], [1616, 8-8, 2424], [00, 00, 00]]]

Each component ca be accessed individually:

ta[1,2,3] # access to a component w.r.t. the default basis
12-12
ta[f,1,2,3]
99
ta.parent()
T(1,2)(M)T^{(1, 2)}\left(M\right)
ta in M.tensor_module(1,2)
True\mathrm{True}

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
False\mathrm{False}

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.disp()
tav=324e1e1e2e1+81e1e1e2e236e1e1e2e3432e1e2e2e1108e1e2e2e2+48e1e2e2e3+108e1e3e2e1+27e1e3e2e212e1e3e2e3432e2e1e3e1108e2e1e3e2+48e2e1e3e3+576e2e2e3e1+144e2e2e3e264e2e2e3e3144e2e3e3e136e2e3e3e2+16e2e3e3e3t\otimes a\otimes v = 324 e_1\otimes e_1\otimes e^2\otimes e^1 + 81 e_1\otimes e_1\otimes e^2\otimes e^2 -36 e_1\otimes e_1\otimes e^2\otimes e^3 -432 e_1\otimes e_2\otimes e^2\otimes e^1 -108 e_1\otimes e_2\otimes e^2\otimes e^2 + 48 e_1\otimes e_2\otimes e^2\otimes e^3 + 108 e_1\otimes e_3\otimes e^2\otimes e^1 + 27 e_1\otimes e_3\otimes e^2\otimes e^2 -12 e_1\otimes e_3\otimes e^2\otimes e^3 -432 e_2\otimes e_1\otimes e^3\otimes e^1 -108 e_2\otimes e_1\otimes e^3\otimes e^2 + 48 e_2\otimes e_1\otimes e^3\otimes e^3 + 576 e_2\otimes e_2\otimes e^3\otimes e^1 + 144 e_2\otimes e_2\otimes e^3\otimes e^2 -64 e_2\otimes e_2\otimes e^3\otimes e^3 -144 e_2\otimes e_3\otimes e^3\otimes e^1 -36 e_2\otimes e_3\otimes e^3\otimes e^2 + 16 e_2\otimes e_3\otimes e^3\otimes e^3

Symmetrization / antisymmetrization

The (anti)symmetrization of a tensor tt over nn arguments involve the division by n!n!, which does not always make sense in the base ring RR. In the present case, R=ZR=\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[:]
(020480026)\left(\begin{array}{rrr} 0 & 2 & 0 \\ -4 & 8 & 0 \\ 0 & 2 & -6 \end{array}\right)
s = g.symmetrize() ; s
Symmetric bilinear form on the Rank-3 free module M over the Integer Ring\mbox{Symmetric bilinear form on the Rank-3 free module M over the Integer Ring}
s.symmetries()
symmetry: (0, 1); no antisymmetry
s[:]
(010181016)\left(\begin{array}{rrr} 0 & -1 & 0 \\ -1 & 8 & 1 \\ 0 & 1 & -6 \end{array}\right)

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)
True\mathrm{True}

One may use index notation to specify the symmetry:

s == g['_(ab)']
True\mathrm{True}
s == g['_{(ab)}'] # LaTeX type notation
True\mathrm{True}

Of course, since ss is already symmetric:

s.symmetrize() == s
True\mathrm{True}

The antisymmetrization proceeds accordingly:

s = g.antisymmetrize() ; s
Alternating form of degree 2 on the Rank-3 free module M over the Integer Ring\mbox{Alternating form of degree 2 on the Rank-3 free module M over the Integer Ring}
s.symmetries()
no symmetry; antisymmetry: (0, 1)
s[:]
(030301010)\left(\begin{array}{rrr} 0 & 3 & 0 \\ -3 & 0 & -1 \\ 0 & 1 & 0 \end{array}\right)
s == g.antisymmetrize(0,1)
True\mathrm{True}

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

s == g['_[ab]']
True\mathrm{True}
s == g['_{[ab]}'] # LaTeX type notation
True\mathrm{True}

Of course, since ss is already antisymmetric:

s == s.antisymmetrize()
True\mathrm{True}

Tensor contractions

Contracting the type-(1,1) tensor tt with the module element vv results in another module element:

t.contract(v)
Element of the Rank-3 free module M over the Integer Ring\mbox{Element of the Rank-3 free module M over the Integer Ring}

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

t.contract(v)[i] == sum(t[i,j]*v[j] for j in M.irange())
True\mathrm{True}

This contraction coincides with the action of tt as an endomorphism:

t.contract(v) == tt(v)
True\mathrm{True}

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

t['^i_j']*v['j'] == t.contract(v)
True\mathrm{True}

Contracting the linear form aa with the module element vv results in a ring element:

a.contract(v)
68-68

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

a.contract(v) == a(v)
True\mathrm{True}

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 ji=T kjivkz^i_{\ \, j} = T^i_{\ \, kj} v^k (with T=taT=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 jki=t jlT lkiz^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
True\mathrm{True}

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

t.contract?
File: /projects/sage/sage-6.9/local/lib/python2.7/site-packages/sage/tensor/modules/free_module_tensor.py 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 explicitely: 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 conventation (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: b._components.keys() # indeed: [Basis (h_0,h_1,h_2) on the Rank-3 free module M over the Integer Ring] sage: a._components.keys() # 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