This example is kindly contributed by FreddyBaudine for reproducing pygae/galgebra#26 and pygae/galgebra#30 with modifications by utensil.

Please note before Python code, there's an invisible markdown cell with the following code to enable color and define some colors from http://latexcolor.com/:

$$
\require{color}
\definecolor{airforceblue}{rgb}{0.36, 0.54, 0.66}
\definecolor{applegreen}{rgb}{0.55, 0.71, 0.0}
\definecolor{atomictangerine}{rgb}{1.0, 0.6, 0.4}
$$
$$ \require{color} \definecolor{airforceblue}{rgb}{0.36, 0.54, 0.66} \definecolor{applegreen}{rgb}{0.55, 0.71, 0.0} \definecolor{atomictangerine}{rgb}{1.0, 0.6, 0.4} $$

In [1]:
from __future__ import print_function
import sys
from galgebra.printer import  Format, xpdf
Format()
from sympy import symbols, sin, pi, latex, Array, permutedims
from galgebra.ga import Ga

In [2]:
from IPython.display import Math

Base manifold (three dimensional)

Metric tensor (cartesian coordinates - norm = False)


In [3]:
from sympy import cos, sin, symbols
g3coords = (x,y,z) = symbols('x y z')
g3 = Ga('ex ey ez', g = [1,1,1], coords = g3coords,norm=False) # Create g3
(e_x,e_y,e_z) = g3.mv()

In [4]:
Math(r'g =%s' % latex(g3.g))


Out[4]:
$\displaystyle g =\left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$

Two dimensioanal submanifold - Unit sphere

Basis not normalised


In [5]:
sp2coords = (theta, phi) = symbols(r'{\color{airforceblue}\theta} {\color{applegreen}\phi}', real = True)
sp2param = [sin(theta)*cos(phi), sin(theta)*sin(phi), cos(theta)]

sp2 = g3.sm(sp2param, sp2coords, norm = False) # submanifold

(etheta, ephi) = sp2.mv() # sp2 basis vectors
(rtheta, rphi) = sp2.mvr() # sp2 reciprocal basis vectors

sp2grad = sp2.grad

sph_map = [1, theta, phi]  # Coordinate map for sphere of r = 1

In [6]:
Math(r'(\theta,\phi)\rightarrow (r,\theta,\phi) = %s' % latex(sph_map))


Out[6]:
$\displaystyle (\theta,\phi)\rightarrow (r,\theta,\phi) = \left[ 1, \ {\color{airforceblue}\theta}, \ {\color{applegreen}\phi}\right]$

In [7]:
Math(r'e_\theta \cdot e_\theta = %s' % (etheta|etheta))


Out[7]:
$\displaystyle e_\theta \cdot e_\theta = 1$

In [8]:
Math(r'e_\phi \cdot e_\phi = %s' % (ephi|ephi))


Out[8]:
$\displaystyle e_\phi \cdot e_\phi = {\sin{\left ({\color{airforceblue}\theta} \right )}}^{2}$

In [9]:
Math('g = %s' % latex(sp2.g))


Out[9]:
$\displaystyle g = \left[\begin{matrix}1 & 0\\0 & \sin^{2}{\left({\color{airforceblue}\theta} \right)}\end{matrix}\right]$

In [10]:
Math(r'g^{-1} = %s' % latex(sp2.g_inv))


Out[10]:
$\displaystyle g^{-1} = \left[\begin{matrix}1 & 0\\0 & \frac{1}{\sin^{2}{\left({\color{airforceblue}\theta} \right)}}\end{matrix}\right]$

Christoffel symbols of the first kind:


In [11]:
Cf1 = sp2.Christoffel_symbols(mode=1)
Cf1 = permutedims(Array(Cf1), (2, 0, 1))

In [12]:
Math(r'\Gamma_{1, \alpha, \beta} = %s \quad \Gamma_{2, \alpha, \beta} = %s ' % (latex(Cf1[0, :, :]), latex(Cf1[1, :, :])))


Out[12]:
$\displaystyle \Gamma_{1, \alpha, \beta} = \left[\begin{matrix}0 & 0\\0 & - \frac{\sin{\left(2 {\color{airforceblue}\theta} \right)}}{2}\end{matrix}\right] \quad \Gamma_{2, \alpha, \beta} = \left[\begin{matrix}0 & \frac{\sin{\left(2 {\color{airforceblue}\theta} \right)}}{2}\\\frac{\sin{\left(2 {\color{airforceblue}\theta} \right)}}{2} & 0\end{matrix}\right] $

In [13]:
Cf2 = sp2.Christoffel_symbols(mode=2)
Cf2 = permutedims(Array(Cf2), (2, 0, 1))

In [14]:
Math(r'\Gamma^{1}_{\phantom{1,}\alpha, \beta} = %s \quad \Gamma^{2}_{\phantom{2,}\alpha, \beta} = %s ' % (latex(Cf2[0, :, :]), latex(Cf2[1, :, :])))


Out[14]:
$\displaystyle \Gamma^{1}_{\phantom{1,}\alpha, \beta} = \left[\begin{matrix}0 & 0\\0 & - \frac{\sin{\left(2 {\color{airforceblue}\theta} \right)}}{2}\end{matrix}\right] \quad \Gamma^{2}_{\phantom{2,}\alpha, \beta} = \left[\begin{matrix}0 & \frac{1}{\tan{\left({\color{airforceblue}\theta} \right)}}\\\frac{1}{\tan{\left({\color{airforceblue}\theta} \right)}} & 0\end{matrix}\right] $

In [15]:
F = sp2.mv('F','vector',f=True) #scalar function
f = sp2.mv('f','scalar',f=True) #vector function

In [16]:
Math(r'\nabla = %s' % sp2grad)


Out[16]:
$\displaystyle \nabla = \boldsymbol{e}_{{\color{airforceblue}\theta}} \frac{\partial}{\partial {\color{airforceblue}\theta}} + \boldsymbol{e}_{{\color{applegreen}\phi}} \frac{1}{{\sin{\left ({\color{airforceblue}\theta} \right )}}^{2}} \frac{\partial}{\partial {\color{applegreen}\phi}}$

In [17]:
Math(r'\nabla f = %s' % (sp2.grad * f))


Out[17]:
$\displaystyle \nabla f = \partial_{{\color{airforceblue}\theta}} f \boldsymbol{e}_{{\color{airforceblue}\theta}} + \frac{\partial_{{\color{applegreen}\phi}} f }{{\sin{\left ({\color{airforceblue}\theta} \right )}}^{2}} \boldsymbol{e}_{{\color{applegreen}\phi}}$

In [18]:
Math(r'F = %s' % F)


Out[18]:
$\displaystyle F = F^{{\color{airforceblue}\theta}} \boldsymbol{e}_{{\color{airforceblue}\theta}} + F^{{\color{applegreen}\phi}} \boldsymbol{e}_{{\color{applegreen}\phi}}$

In [19]:
Math(r'\nabla F = %s' % (sp2.grad * F))


Out[19]:
$\displaystyle \nabla F = \left ( \frac{F^{{\color{airforceblue}\theta}} }{\tan{\left ({\color{airforceblue}\theta} \right )}} + \partial_{{\color{airforceblue}\theta}} F^{{\color{airforceblue}\theta}} + \partial_{{\color{applegreen}\phi}} F^{{\color{applegreen}\phi}} \right ) + \left ( \frac{2 F^{{\color{applegreen}\phi}} }{\tan{\left ({\color{airforceblue}\theta} \right )}} + \partial_{{\color{airforceblue}\theta}} F^{{\color{applegreen}\phi}} - \frac{\partial_{{\color{applegreen}\phi}} F^{{\color{airforceblue}\theta}} }{{\sin{\left ({\color{airforceblue}\theta} \right )}}^{2}}\right ) \boldsymbol{e}_{{\color{airforceblue}\theta}}\wedge \boldsymbol{e}_{{\color{applegreen}\phi}}$

One dimensioanal submanifold

Basis not normalised


In [20]:
cir_th = phi = symbols(r'{\color{atomictangerine}\phi}',real = True)
cir_map = [pi/8, phi]

In [21]:
Math(r'(\phi)\rightarrow (\theta,\phi) = %s' % latex(cir_map))


Out[21]:
$\displaystyle (\phi)\rightarrow (\theta,\phi) = \left[ \frac{\pi}{8}, \ {\color{atomictangerine}\phi}\right]$

In [22]:
cir1d = sp2.sm( cir_map , (cir_th,), norm = False) # submanifold

cir1dgrad = cir1d.grad

(ephi) = cir1d.mv()

In [23]:
Math(r'e_\phi \cdot e_\phi = %s' % latex(ephi[0] | ephi[0]))


Out[23]:
$\displaystyle e_\phi \cdot e_\phi = \frac{1}{2} - \frac{\sqrt{2}}{4}$

In [24]:
Math('g = %s' % latex(cir1d.g))


Out[24]:
$\displaystyle g = \left[\begin{matrix}\frac{1}{2} - \frac{\sqrt{2}}{4}\end{matrix}\right]$

In [25]:
h = cir1d.mv('h','scalar',f= True)

H = cir1d.mv('H','vector',f= True)

In [26]:
Math(r'\nabla = %s' % cir1dgrad)


Out[26]:
$\displaystyle \nabla = \boldsymbol{e}_{{\color{atomictangerine}\phi}} \left ( 2 \sqrt{2} + 4\right ) \frac{\partial}{\partial {\color{atomictangerine}\phi}}$

In [27]:
Math(r'\nabla h = %s' %(cir1d.grad * h).simplify())


Out[27]:
$\displaystyle \nabla h = \left(2 \sqrt{2} + 4\right) \partial_{{\color{atomictangerine}\phi}} h \boldsymbol{e}_{{\color{atomictangerine}\phi}}$

In [28]:
Math('H = %s' % H)


Out[28]:
$\displaystyle H = H^{{\color{atomictangerine}\phi}} \boldsymbol{e}_{{\color{atomictangerine}\phi}}$

In [29]:
Math(r'\nabla H = %s' % (cir1d.grad * H).simplify())


Out[29]:
$\displaystyle \nabla H = \partial_{{\color{atomictangerine}\phi}} H^{{\color{atomictangerine}\phi}} $

In [ ]: