#!/usr/bin/env python
# coding: utf-8
# # 12. Vector fields - continuation
#
# 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()
# ### Vector fields defined in the intersection of two coordinate neighborhoods
#
#
#
# The vector field which is defined in the intersection of two coordinate neighborhoods, can be expressed using two different charts and two different frames.
#
#
#
# **Example 12.1**
#
# Define a vector field $v$ in Cartesian coordinates in $R^2$. Check that if transition maps from Cartesian to polar and from polar to Cartesian coordinates are defined, then we have automatically the access to the representation of $v$ in both coordinate systems.
# In[2]:
R2 = Manifold(2, 'R^2') # manifold R2
c_cart. = R2.chart() # Cartesian coordinates on R2
c_pol. = R2.chart(r'r:(0,+oo) ph:(0,2*pi):\phi') # polar coord.
# U is an open subset of R2, such that, the transition from Cart.to pol.
# coord. and from polar to Cartesian coord. are smooth and bijective
U = R2.open_subset('U',coord_def={c_cart:(y!=0,x<0),c_pol:(ph!=0)});
cart_to_pol = c_cart.transition_map(c_pol, # Cart. --> polar
[sqrt(x^2 + y^2),arctan2(y, x) ]) # transition
pol_to_cart = c_pol.transition_map(c_cart, # polar --> Cart.
[r*cos(ph), r*sin(ph)]) # transition
v = R2.vector_field(2*x*y, (1-x^2+y^2), name='v') # vector field v
# Display vector field $v$ in default chart and default frame:
# In[3]:
get_ipython().run_line_magic('display', 'latex # display v in default chart')
v.display() # and frame
# Display $v$ in polar frame, Cartesian chart:
# In[4]:
v.display(c_pol.frame()) # v in polar frame, Cart.chart
# In[5]:
# the same with factored components
v.apply_map(factor,frame=c_pol.frame()) # apply factor to comp.
v.display(c_pol.frame())
# We can also display components separately:
# In[6]:
v.display_comp(c_pol.frame()) # display components
# Now use the polar basis and polar coordinates:
# In[7]:
v.display(c_pol.frame(),c_pol) # pol.chart, pol.frame
# In[8]:
# the same with factored components
v.apply_map(factor,frame=c_pol.frame(),chart=c_pol)
v.display(c_pol.frame(),c_pol) # factor comp.
# Polar components, separately:
# In[9]:
v.display_comp(c_pol.frame(),c_pol) # display comp.
#
#
# ### Module $\mathfrak{X}(M)$
#
#
#
# Let $\mathfrak{X}(M)$ be the set of all **smooth vector fields** on a smooth manifold $M$.
#
# The set $\mathfrak{X}(M)$ is a module (over $C^\infty(M)\ $) if we define the operations
#
# $$(X+Y)_p=X_p+Y_p,\\
# (fX)_p=f(p)X_p,
# $$
#
# for $X,Y\in \mathfrak{X}(M),\ \ f\in C^\infty(M).$
#
# One can check that the just defined operations have the properties
#
# $$(aX+bY)f=aXf+bYf,\\
# (gX)f=g(Xf),
# $$
#
# for $X,Y\in \mathfrak{X}(M),\ \ a,b\in R.\ \ f,g\in C^\infty(M).$
#
#
#
# ### Lie bracket
#
#
#
# For $X,Y\in \mathfrak{X}(M)$ the **Lie bracket** is defined by
#
# $$[X,Y]f=X(Yf)-Y(Xf),\quad\mbox{for}\quad f\in C^\infty(M).$$
#
# From this definition it follows that $$[X,Y]=-[Y,X].$$
#
# One can also prove that $[X,Y]\in\mathfrak{X}(M)$ and the Lie bracket is bilinear.
#
# In fact if $X,Y\in\mathfrak{X}(M)$ and $f ∈ C^∞ (M)$, then $\ X(Y f )\ $ and $\ Y(X f )\ $ also belong to $C^∞ (M)$. We need to show that $[X,Y]$ has the derivation property.
# For that purpose let us note that for $\ f,g\in C^\infty(M),\ p\in M$
#
# $$[X,Y]_p(fg)=X_p(Y_p(fg))-Y_p(X_p(fg))\\=X_p(f(p)Y_p(g)+g(p)Y_p(f))-Y_p(f(p)X_p(g)+g(p)X_p(f))\\
# =f(p)X_p(Y_p(g))+g(p)X_p(Y_p(f))-f(p)Y_p(X_p(g))-g(p)(Y_p(X_p(f))\\
# =f(p)[X,Y]_p(g)+g(p)[X,Y]_p(f),
# $$
#
# and for $a,b\in R$
#
# $$
# [X,Y]_p(af+bg)=X_p(Y_p(af+bg))-Y_p(X_p(af+bg))\\
# =X_p(aY_p(f)+bY_p(g))-Y_p(aX_p(f)+bX_p(g))\\
# =aX_p(Y_p(f))+bX_p(Y_p(g))-aY_p(X_p(f))-bY_p(X_p(g))\\=a[X,Y]_p(f)-b[X,Y]_p(g).
# $$
#
#
# The bilinearity denotes
#
# $$[aX + bY, Z] = a[X, Z] + b[Y, Z],\\
# [X, aY + bZ] = a[X, Y] + b[X, Z],
# $$
#
# for $a, b ∈ R,\quad X, Y, Z ∈ \mathfrak{X}(M)$.
#
# This is true since
# $$
# [aX + bY, Z] f = (aX + bY)(Z f ) − Z (aX + bY) f\\
# = aX(Z f ) + bY(Z f ) − Z(aX f + bY f )\\
# = aX(Z f ) + bY(Z f ) − aZ(X f ) − bZ(Y f )\\
# = a[X, Z] f + b[Y, Z] f.
# $$
#
# From the antisymmetry it follows that the linearity in the first argument implies the linearity in the second argument.
#
#
#
# ### Jacobi identity
#
#
#
#
# The following **Jacobi identity**
# holds true
#
# $$[X, [Y, Z]] + [Y, [Z, X]] + [Z, [X, Y]] = 0.$$
#
# Note that
#
# $$
# ([X, [Y, Z]] + [Y, [Z, X]] + [Z, [X, Y]]) f\\
# = X([Y, Z] f ) − [Y, Z]X f + Y([Z, X] f ) − [Z, X]Y f + Z([X, Y] f ) − [X, Y]Z f\\
# = X (Y(Z f )) − X(Z(Y f )) − Y (Z(X f )) + Z( Y(X f ))\\
# +Y (Z(X f )) − Y(X(Z f )) − Z (X(Y f )) + X(Z(Y f ))\\
# +Z( X(Y f )) − Z(Y(X f )) − X (Y(Z f )) + Y (X(Z f ))
# = 0.
# $$
#
#
#
# ### Lie bracket of vector fields multiplied by functions
#
#
#
# For $X,Y\in\mathfrak{X}(M)$ and $f,g\in C^\infty(M)$ we have
#
# $$[ f X, gY] = f g[X, Y] + f \cdot(Xg)Y − g\cdot(Y f )X.$$
#
# To prove this equality let us observe that for $h \in C^\infty(M)$
#
# $$
# [ f X, gY]h = f X(gYh) − gY( f Xh)
# = f \cdot(gX(Yh) + (Xg)Yh )− g\cdot (f Y(Xh) + (Y f )Xh)\\
# = f g[X, Y]h + f \cdot(Xg)Yh − g\cdot(Y f )Xh.
# $$
#
#
#
#
# ### Lie bracket for the local basis vector fields
#
#
#
# If $(U, φ)$ is a chart on $M$ with coordinates $x^1 , x^2 ,\ldots , x^n$ , then we have
# $$\Big[\frac{\partial}{\partial x^i},\frac{\partial}{\partial x^j}\Big]=0.$$
#
# Recall that from definition (8.3) it follows
# $\quad \frac{\partial}{\partial x^i}f=
# [D_i(f\circ\phi^{-1})]\circ\phi.\quad$
# Note that if we set $F=\{[D_j(f\circ\phi^{-1})]\circ \phi\}$, then
# $$
# \frac{\partial}{\partial x^i}\{[D_j(f\circ\phi^{-1})]\circ \phi\}
# =\frac{\partial}{\partial x^i}F=[D_i(F\circ\phi^{-1})]\circ\phi\\
# =\{[D_i[D_j(f\circ\phi^{-1})]\circ\phi]\circ\phi^{-1}\}\circ\phi
# =D_i(D_j(f\circ\phi^{-1})\circ\phi).
# $$
# Therefore
# $$\Big[\frac{\partial}{\partial x^i},\frac{\partial}{\partial x^j}\Big]f =
# \frac{\partial}{\partial x^i}\{[D_j(f\circ\phi^{-1})]\circ \phi\}
# -\frac{∂}{∂ x^j}\{[D_i ( f ◦ φ^{−1} )] ◦ φ\}\\
# = \{{D_i D_j ( f ◦ φ^{−1} ) − D_j D_i ( f ◦ φ^{−1} )}\} ◦ φ
# = 0.$$
#
#
# ### Lie bracket in components
#
#
#
# If $X,Y\in\mathfrak{X}(M)$ are expressed in local coordinates:
# $X=X^i\frac{\partial}{\partial x^i},\quad Y=Y^j\frac{\partial}{\partial x^j},\quad$ then
# $$[X,Y]=(XY^i-YX^i)\frac{\partial}{\partial x^i}.$$
#
# The components of the Lie bracket $[X, Y]$
# are obtained by applying the vector field $[X, Y]$ to the coordinates $x^i$ (cf. (8.5)), so
#
# $$[X, Y]x^i = X(Yx^i ) − Y(Xx^i ) = XY^i − YX^i.$$
#
# Therefore $\ [X, Y] = (XY^i − YX^i ) \frac{∂}{∂ x^i}.$
#
#
#
# **Example 12.2**
#
# Compute the Lie bracket for vector fields
# $$X=xy\frac{\partial}{\partial x}
# +\frac{1}{z}\frac{\partial}{\partial y}
# -3yz^3\frac{\partial}{\partial z},\quad
# Y=\frac{\partial}{\partial x}+(x+y)\frac{\partial}{\partial z}.$$
#
#
#
# According to the general formula, the components of $[X,Y]$ are
# $$XY^1-YX^1=X(1)-Y(xy)=-\frac{\partial}{\partial x}(xy)=-y,$$
# $$XY^2-YX^2=X(0)-Y(1/z)=-(x+y)\frac{\partial}{\partial z}(1/z)=\frac{x+y}{z^2},$$
# $$XY^3-YX^3=X(x+y)-Y(-3yz^3)$$
# $$=
# xy\frac{\partial}{\partial x}(x+y)
# +\frac{1}{z}\frac{\partial}{\partial y}(x+y)
# -3yz^3\frac{\partial}{\partial z}(x+y)$$
# $$-[\frac{\partial}{\partial x}+(x+y)\frac{\partial}{\partial z}](-3yz^3)
# =xy+\frac{1}{z}+(x+y)(9yz^2).$$
#
# Define the corresponding vector fields and their Lie bracket in SageMath.
# In[10]:
R3 = Manifold(3, 'R^3') # manifold R3
c_cart. = R3.chart() # Cart. coord
X = R3.vector_field(x*y, 1/z, -3*y*z^3, name='X') # X v.field
Y = R3.vector_field(1, 0, x+y, name='Y') # Y v.field
get_ipython().run_line_magic('display', 'latex')
v=X.bracket(Y) # [X,Y]
v.display()
# We can make some simplifications to all components using `apply_map`:
# In[11]:
v.apply_map(expand)
v.display()
# The above hand-made computations can be replaced by:
# In[12]:
Xx=R3.scalar_field(x*y,name='Xx') # x-coordinate of X
Yx=R3.scalar_field(1,name='Yx') # x-coordinate of Y
Xy=R3.scalar_field(1/z,name='Xy') # y-coordinate of X
Yy=R3.scalar_field(0,name='Yy') # y-coordinate of Y
Xz=R3.scalar_field(-3*y*z^3,name='Xz') # z-coordinate of X
Yz=R3.scalar_field(x+y,name='Yx') # z-coordinate of Y
# use the general formula to compute the coordinates of [X,Y]
[(X(Yx)-Y(Xx)).expr(),(X(Yy)-Y(Xy)).expr(),
(X(Yz)-Y(Xz)).expr().expand()]
# To obtain the components of $[X,Y]$,
# one can also compute the values of Lie bracket $[X,Y]$ on coordinate functions $x,y,z$.
# In[13]:
fx=R3.scalar_field(x,name='fx') # fx(x,y,z)=x
fy=R3.scalar_field(y,name='fy') # fy(x,y,z)=y
fz=R3.scalar_field(z,name='fz') # fz(x,y,z)=z
[(X.bracket(Y))(fx).expr(),(X.bracket(Y))(fy).expr(),
(X.bracket(Y))(fz).expr().expand()] # [X,Y] on fx,fy,fz
#
#
# **Example 12.3**
#
# Compute $[X,Y],\ [X,Z]$ and $\ [Y,Z]\ $ for
#
# $$X=(1+r^2)\sin(\theta)\frac{\partial}{\partial r} + \frac{1-r^2}{r}\cos(\theta) \frac{\partial}{\partial \theta}, \\
# Y=-(1+r^2)\cos(\theta)\frac{\partial}{\partial r} + \frac{1-r^2}{r}\sin(\theta)
# \frac{\partial}{\partial \theta},\\
# Z=\frac{\partial}{\partial \theta}.
# $$
# In[14]:
R2 = Manifold(2, 'R^2') # manifold R2
# polar coordinates:
c_pol. = R2.chart(r'r:(0,+oo) th:(0,2*pi):\theta')
# vector fields X,Y,Z:
X=R2.vector_field((1+r^2)*sin(th),(1-r^2)/r*cos(th),name='X')
Y=R2.vector_field(-(1+r^2)*cos(th),(1-r^2)/r*sin(th),name='Y')
Z=R2.vector_field(0,1,name='Z')
v1=X.bracket(Y) # [X,Y]
v1.display() # show [X,Y]
# In[15]:
v2=X.bracket(Z) # [X,Z]
v2.display() # show [X,Z]
# In[16]:
v3=Y.bracket(Z) # [Y,Z]
v3.display() # show [Y,Z]
# Components of the last result can be also obtained by applying $[Y,Z]$ to coordinate functions $r,\theta$:
# In[17]:
fr=R2.scalar_field(r,name='fr') # fr(r,th)=r
# [Y,Z]r
v3(fr).display() # [Y,Z](fr)
# In[18]:
fth=R2.scalar_field(th,name='fth') # fth(r,th)=th
# [Y,Z] theta
v3(fth).display() # [Y,Z](fth)
# ## What's next?
#
# Take a look at the notebook [Tensor fields](https://nbviewer.org/github/sagemanifolds/IntroToManifolds/blob/main/13Manifold_TensorFields.ipynb).