#!/usr/bin/env python
# coding: utf-8
# # 9a. Tensors on modules
#
# This notebook is part of the [Introduction to manifolds in SageMath](https://sagemanifolds.obspm.fr/intro_to_manifolds.html) by Andrzej Chrzeszczyk (Jan Kochanowski University of Kielce, Poland).
# In[1]:
version()
# ### Reminder. Vectors and linear forms on modules
#
#
#
# Recall that for free modules of finite rank (in [notebook 6](https://nbviewer.org/github/sagemanifolds/IntroToManifolds/blob/main/06Manifold_VectSpaces_Modules.ipynb)) we have introduced **vectors** and **linear forms**.
#
#
#
# **Example 9.1a**
#
# Define a free module of finite rank and some vector and linear form.
# In[2]:
N=3 # dimension of the module
get_ipython().run_line_magic('display', 'latex')
M=FiniteRankFreeModule(SR,3,name='M') # 3-dim module over SR
e = M.basis('e') # basis of M
v=var('v',n=3) # components of vector v
v=M(list(v)) # define vector v
# In[3]:
print(v) # information on v
v.comp()[:] # components of v
# General linear forms on a 3-dimensional module can be defined as follows:
# In[4]:
a=M.linear_form() # linear form a on M
a[:]=var('a',n=4) # components of a
# In[5]:
a.disp() # show a as combination
# of dual basis
# The linear form applied to a vector gives a scalar.
# In[6]:
a(v) # value a(v)
# The value of a vector $v\in M$ on a linear form $a\in M^*$ is by definition equal to $a(v)$.
# In[7]:
v(a) # value v(a)
#
#
# Now we are ready to define more general objects.
#
#
#
# ## Tensors on modules
#
#
# If $M$ is a vector space or a module, by a **multilinear** or more precisely $k$-**linear form** we mean a function $t:
# M^k\to R$ which is linear in each of its arguments, i.e., for $i=1,\ldots,k$
#
# $$t(v_1\ldots,\alpha v_i+\beta w_i,\ldots v_k)=
# \alpha t(v_1\ldots,v_i,\ldots v_k)+\beta t(v_1\ldots,w_i,\ldots v_k),\quad \alpha,\beta\in R,\quad v_i,w_i\in M.$$
#
#
#
# Assume that $M$ is a module.
#
#
# ### Tensor module $T^{(k,l)}M$
#
#
#
#
# **Tensors** of type (k,l) on $ M$
# are multilinear maps:
# $$ T^{(k,l)}M = \{t:\underbrace{M^*\times\cdots\times M^*}_{k\ \; \mbox{times}}
# \times \underbrace{M\times\cdots\times M}_{l\ \; \mbox{times}}\to R\},$$
# where $\ M^*\ $ denotes the dual module, i.e. the module of linear forms on $M$ (cf. [notebook 6](https://nbviewer.org/github/sagemanifolds/IntroToManifolds/blob/main/06Manifold_VectSpaces_Modules.ipynb)).
#
# Since $a\to v(a)$ defines a linear form $M^*\to R$, the elements $v\in M$ can be considered as elements of $T^{(1,0)}M$. On the other hand $T^{(0,1)}M$ as the space of linear forms on $M$ is equal to $M^*$. Thus, **tensors generalize vectors and linear forms**.
#
#
# **Example 9.2a**
#
# Let us show how to use `tensor_module` method of SageMath Manifolds.
# In[8]:
# continuation
M.tensor_module(1,0) # elements of T^(1,0) are vectors
# In[9]:
print(M.tensor_module(1,0)) # T^(1,0) = M
# since the ring SR is a field we obtain vector space
# In[10]:
M.tensor_module(0,1) # T^(0,1) = M*
# In[11]:
print(M.tensor_module(0,1))
#
#
# ### Module $T^{(0,k)}M$ - of covariant tensors of rank $k$
#
#
#
# is the module of multilinear maps
# $$ T^{(0,k)}M = \{t:\underbrace{M\times\cdots\times M}_{k\ \; \mbox{times}}\to R\}.$$
# For k=1 we obtain the module of linear forms or **covectors** on $M$.
# In $T^{(0,k)}M$ we introduce the **algebraic operations** by
#
# $$ (at+bs)(v_1\ldots,v_k)=at(v_1\ldots,v_k)+bs(v_1\ldots,v_k),$$
#
# where $t,s\in T^{(0,k)}M$, $v_1,\ldots,v_k\in M$ and $a,b\in R$.
# For $t\in T^{(0,k)},\ s\in T^{(0,l)}M$ we define the **tensor product** $\ t\otimes s\in T^{(0,k+l)}M\ $ by
#
# \begin{equation}
# (t ⊗ s )(v_1 ,\ldots , v_{k+l} ) = t (v_1 ,\ldots, v_k ) s (v_{k+1} , . . . , v_{k+l} ),
# \label{eq:tensor_product}\tag{9.1a}
# \end{equation}
#
# for $v_1,\ldots,v_{k+l}\in M$.
# In SageMath Manifolds the symbol of tensor product is simply $*$.
#
#
#
# **Example 9.3a**
#
# Let us check the last formula in the case of two general tensors from $T^{(0,2)}$ and 2-dimensional module.
# In[12]:
N=2; # dimension of module
get_ipython().run_line_magic('display', 'latex')
Mo=FiniteRankFreeModule(SR,2,name='Mo') # module Mo
e = Mo.basis('e') # basis of Mo
t=Mo.tensor((0,2)) # (0,2) type tensor t
s=Mo.tensor((0,2)) # (0,2) type tensor s
# In[13]:
M=4 # number of vectors
vv=[[var('v'+str(i)+str(j), # components of four vectors
latex_name='v'+'^'+str(i)+'_'+str(j))
for i in range(N)] for j in range(M)]
vv
# In[14]:
v=[Mo(vv[k]) for k in range(M)] # list of vectors
tt=[[var('t'+str(i)+str(j)) # components of tensor t
for j in range(M)] for i in range(N)]
ss=[[var('t'+str(i)+str(j)) # components of tensor s
for j in range(M)] for i in range(N)]
t[:]=tt # define tensor t components t_{ij}
s[:]=ss # define tensor s components s_{ij}
# In[15]:
# check the equality (9.1a) defining tensor product
bool((t*s)(v[0],v[1],v[2],v[3])==t(v[0],v[1])*s(v[2],v[3]))
#
#
# Tensor product has the following properties (proofs for modules $T_pM,$ where $M$ denotes a manifold are given in the [next notebook](https://nbviewer.org/github/sagemanifolds/IntroToManifolds/blob/main/09Manifold_LinearFormsTensors_onTp.ipynb))
#
# \begin{equation}
# \begin{matrix}
# (ar + bs ) ⊗ t = ar ⊗ t + bs ⊗ t ,\\
# r ⊗ (as + bt ) = ar ⊗ s + br ⊗ t ,\\
# (r ⊗ s ) ⊗ t = r ⊗ (s ⊗ t ),
# \end{matrix}
# \label{eq:tensor_product_prop}\tag{9.2a}
# \end{equation}
#
# for $a,b\in R\ $ and for arbitrary covariant tensors $r,s,t$ (the addition is defined only for tensors of the same rank $(0,k)$).
#
#
# ### Covariant tensors in components
#
#
#
#
# One can check that if $e_1,\ldots,e_n$ is a basis of the module $M$
# and $e^1,\ldots,e^n\ $ its dual basis (defined in [notebook 6](https://nbviewer.org/github/sagemanifolds/IntroToManifolds/blob/main/06Manifold_VectSpaces_Modules.ipynb)), then the elements
#
# \begin{equation}
# e^{j_1}\otimes\ldots \otimes e^{j_m}, \quad j_q\in\{1,\ldots,n\}
# \tag{9.2a}
# \end{equation}
#
# form a basis for $T^{(0,m)}M$ and if we put
#
# $$t_{j_1\ldots j_m}
# =t(e_{j_1},\ldots,e_{j_m}),
# $$
#
# then
#
# \begin{equation}
# t=t_{j_1\ldots j_m}
# e^{j_1}\otimes\ldots\otimes e^{j_m}, \quad \text{for }\ t\in T^{(0,m)}M.
# \tag{9.3a}
# \end{equation}
#
# In fact, since $\ v_1=e^{j_1}(v_1)e_{j_1},\ldots, v_m=e^{j_m}(v_m)e_{j_m},\ $ then
#
# $$t(v_1,\ldots,v_m)=t(e^{j_1}(v_1)e_{j_1},\ldots,e^{j_m}(v_m)e_{j_m})\\
# =e^{j_1}(v_1)\ldots e^{j_m}(v_m)t(e_{j_1},\ldots,e_{j_m})\\
# =t_{j_1\ldots j_m}e^{j_1}\otimes \ldots \otimes e^{j_m}(v_1,\ldots v_m).
# $$
#
#
#
#
# **Example 9.4a**
#
# Let us show the representation of a tensor $\ t\in T^{(0,3)}M\ $ in components.
# In[16]:
N=2 # dimension of module
st=[[[var('t'+str(i0)+str(i1)+str(i2)) # components of t
for i2 in range(N)]
for i1 in range(N)]
for i0 in range(N)]
st # show components
# In[17]:
Mo=FiniteRankFreeModule(SR,2,name='Mo') # module Mo
e = Mo.basis('e') # basis of Mo
get_ipython().run_line_magic('display', 'latex')
t = Mo.tensor((0,3), name='t') # tensor of (0,3) type
t[:]=st # define all components
# General tensor of type (0,3) on a 2-dimensional module:
# In[18]:
t.disp() # show t
# Tensor of type (0,3) on a 2-dimensional module with concrete components:
# In[19]:
# continuation
ct=range(1,9) # [1,...,8] consecutive components
for i0 in range(N):
for i1 in range(N):
for i2 in range(N):
t[i0,i1,i2]=ct[i0*N*N+i1*N+i2]
t.disp()
# In[20]:
print(e) # basis
# Check that the coefficient $t_{011}$ is equal to $t(e_0,e_1,e_1)$:
# In[21]:
t(e[0],e[1],e[1]) # t(e_0,e_1,e_1)
#
#
# ### Module $T^{(k,0)}M $ of contravariant tensors of rank $k$
#
#
#
# is the module of $k$-linear forms:
# $$ T^{(k,0)}M = \{t:\underbrace{M^*\times\cdots\times M^*}_{k\ \;\mbox{times}}
# \to R\}.$$
#
# In $T^{(k,0)}M$ we introduce the module structure by
#
# $$ (at+bs)(\alpha_1\ldots,\alpha_k)=at(\alpha_1\ldots,\alpha_k)+bs(\alpha_1\ldots,\alpha_k),$$
#
# where $t,s\in T^{(k,0)}M$, $\alpha_1,\ldots,\alpha_k\in M^*$ and $a,b\in R$.
# For $t,s\in T^{(k,0)}M$ we define the **tensor product** $t\otimes s\ $ by
#
# \begin{equation}
# (t ⊗ s )(\alpha_1 ,\ldots , \alpha_{k+l} ) ≡ t (\alpha_1 ,\ldots, \alpha_k ) s(\alpha_{k+1} , \ldots , \alpha_{k+l} ),
# \tag{9.4a}
# \end{equation}
#
# for $\alpha_1,\ldots,\alpha_{k+l}\in M^*$.
#
#
#
# ### Contravariant tensors in components
#
#
#
# One can check that if $e_1,\ldots,e_n$ is a basis of the module $M$
# and $e^1,\ldots,e^n$ its dual basis then the elements
#
# $$e_{i_1}\otimes\ldots\otimes e_{i_k}, \quad i_p\in\{1,\ldots,n\}
# $$
#
# form a basis for $T^{(k,0)}M$ and if we put
#
# $$t^{i_1\ldots i_k}
# =t(e^{i_1},\ldots,e^{i_k}),
# $$
#
# then
#
# \begin{equation}
# t=t^{i_1\ldots i_k}
# e_{i_1}\otimes\ldots\otimes e_{i_k}, \quad \text{for }\ t\in T^{(k,0)}M.
# \tag{9.5a}
# \end{equation}
#
# This follows from $\alpha(v)=\alpha(e^i(v)e_
# i)=\alpha(e_i)e^i(v),\ $ and $\ v(\alpha)=\alpha(v)$ for linear form $\ \alpha\in M^*\ $ and $\ v\in M,\ $since we have
#
# $$t(\alpha_1,\ldots,\alpha_k)=
# t(\alpha_1(e_{i_1})e^{i_1},\ldots,\alpha_k(e_{i_k})e^{i_k})\\
# =\alpha_1(e_{i_1})\ldots \alpha_k(e_{i_k})t(e^{i_1},\ldots,e^{i_k})\\
# =e_{i_1}(\alpha_1)\ldots e_{i_k}(\alpha_k)t(e^{i_1},\ldots, e^{i_k})\\
# =t^{i_1\ldots i_k} e_{i_1}\otimes \ldots \otimes e_{i_k}(\alpha_1,\ldots,\alpha_k).
# $$
#
#
# **Example 9.5a**
#
# Let us show the representation of a tensor of type (3,0) in components.
# In[22]:
get_ipython().run_line_magic('display', 'latex')
N=2 # dimension of module
st=[[[var('t'+str(i0)+str(i1)+str(i2),
latex_name='t'+'^'+'{'+str(i0)+str(i1)+str(i2)+'}')
for i2 in range(N)]
for i1 in range(N)] # components of t
for i0 in range(N)] # with superscripts
st # show components
# In[23]:
Mo=FiniteRankFreeModule(SR,2,name='Mo') # module Mo
e = Mo.basis('e') # basis of Mo
t = Mo.tensor((3,0), name='t') # tensor of (3,0) type
t[:]=st # define all components
# General tensor of type (3,0) on a 2-dimensional module:
# In[24]:
t.disp() # show t
# Tensor of type (3,0) on a 2-dimensional module with concrete components:
# In[25]:
# continuation
ct=range(1,9) # [1,...,8]- consecutive components
for i0 in range(N):
for i1 in range(N):
for i2 in range(N):
t[i0,i1,i2]=ct[i0*N*N+i1*N+i2]
t.disp()
# In[26]:
d=e.dual_basis() # dual basis
print(d)
# Check that the coefficient $t^{011}$ is equal to $t( e^0,e^1, e^1)$:
# In[27]:
t(d[0],d[1],d[1]) # t(e^0,e^1,e^1)
#
#
# ### General tensors from $T^{(k,l)}M$ in components
#
#
# For $t\in T^{(k_1,l_1)}M,\ \ s\in T^{(k_2,l_2)}M$ we define the **tensor product** $t\otimes s\in T^{(k_1+k_2,l_1+l_2)}$ by
#
# \begin{equation}
# (t ⊗ s )(\alpha_1,\ldots\alpha_{k_1+k_2},v_1 ,\ldots , v_{l_1+l_2} )\\
# =t(\alpha_1,\ldots,\alpha_{k_1},v_1 ,\ldots, v_{l_1} ) s (\alpha_{k_1+1},\ldots,\alpha_{k_1+k_2},v_{l_1+1},\ldots, v_{l_1+l_2} ),
# \tag{9.6a}
# \end{equation}
#
# for $\alpha_1,\ldots,\alpha_{k_1+k_2}\in M^*$ and $v_1,\ldots,v_{l_1+l_2}\in M$.
#
# Generalizing the formulas (9.3a) and (9.5a) we obtain the following expression for the general tensor $t\in T^{(k,l)}M$ in components
#
# \begin{equation}
# t=
# t\big(e^{i_1},..,e^{i_k},e_{j_1},..,e_{j_l})
# e_{i_1}\otimes\dots\otimes e_{i_k}\otimes e^{j_1}\otimes\ldots\otimes e^{j_l}.
# \label{}\tag{9.7a}
# \end{equation}
#
# Very often the notation
# $$t^{i_1\ldots i_k}_{j_1\ldots j_l}=t_p\big(e^{i_1},..,e^{i_k},e_{j_1},..,e_{j_l})
# $$
# is used and then
# $$t=t^{i_1\ldots i_k}_{j_1\ldots j_l}e_{i_1}\otimes\dots\otimes e_{i_k}\otimes e^{j_1}\otimes\ldots\otimes e^{j_l}.
# $$
# To check that the elements
# \begin{equation}
# e_{i_1}\otimes\dots\otimes e_{i_k}\otimes e^{j_1}\otimes\ldots\otimes e^{j_l}
# \label{} \tag{9.8a}
# \end{equation}
#
# are linearly independent, assume that the linear combination
#
# $$a^{i_1\ldots i_k}_{j_1\ldots j_l}e_{i_1}\otimes\dots\otimes e_{i_k}\otimes e^{j_1}\otimes\ldots\otimes e^{j_l}
# $$
#
# vanishes. If we apply this combination to $\quad(e^{m_1},\ldots,e^{m_k}, e_{n_1},..,e_{n_l}\big)$ we get $\displaystyle a^{m_1\ldots m_k}_{n_1\ldots n_l}=0$.
# Since as in previous cases we can check that elements of the type (9.8a) span $T^{(k,l)}M$, we have proved that these elements form a **basis** for $T^{(k,l)}M$.
#
#
# **Example 9.6a**
#
# Now we show the representation of a tensor of (2,2) type in components.
# In[28]:
get_ipython().run_line_magic('display', 'latex')
N=2 # dimension of module
st=[[[[var('t'+str(i0)+str(i1)+str(i2)+str(i3),
latex_name='t'+'^'+'{'+str(i0)+str(i1)+'}'+'_'+
'{'+str(i2)+str(i3)+'}')
for i3 in range(N)] for i2 in range(N)] # comp. with upper
for i1 in range(N)] for i0 in range(N)] # and lower indices
st # show components
# In[29]:
Mo=FiniteRankFreeModule(SR,2,name='Mo') # module Mo
e = Mo.basis('e') # basis of Mo
t = Mo.tensor((2,2), name='t') # tensor of (2,2) type
t[:]=st # define all components
# General tensor from $T^{(2,2)}M$ on a 2-dimensional module ($2^4$ components):
# In[30]:
t.disp() # show t
# Here is a version with concrete components:
# In[31]:
ct=range(1,17) # [1,..,16]-consecutive components
for i0 in range(N):
for i1 in range(N):
for i2 in range(N):
for i3 in range(N):
t[i0,i1,i2,i3]=ct[i0*N*N*N+i1*N*N+i2*N+i3]
# In[32]:
t.disp()
# In[33]:
d=e.dual_basis() # dual basis
# Check that the coefficient $t^{10}_{01}$ is equal to $t(e^1,e^0,e_0,e_1)$:
# In[34]:
t(d[1],d[0],e[0],e[1]) # t(e^1,e^0,e_0,e_1)
# ## What's next?
#
# Take a look at the notebook [Tensors on $T_pM$](https://nbviewer.org/github/sagemanifolds/IntroToManifolds/blob/main/09Manifold_LinearFormsTensors_onTp.ipynb).