CoCalc Public Filesspring2021 / G3.ipynb
Author: Carlos Rodriguez
Views : 70
Description: How to use the galgebra (Geometric Algebra) package at CoCalc.
Compute Environment: Ubuntu 20.04 (Default)
In [98]:
import sympy
from galgebra.ga import Ga
from galgebra.printer import latex
from IPython.display import Math

# tell sympy to use our printing by default
sympy.init_printing(latex_printer=latex, use_latex='mathjax')

In [11]:
g = sympy.Matrix([[9,-2,3],
[-2,6,2],
[3,2,3]
])
uvw = (u,v,w) = sympy.symbols('u v w', real=True)
g3d = Ga('a_1 a_2 a_3', g=g, coords=uvw)
a1,a2,a3 = g3d.mv()

In [13]:
a1|a3

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [15]:
V = (a1^a3)*(a1^a3^a2)
V

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [16]:
V.norm()

$\displaystyle 18 \sqrt{2}$
In [17]:
(a1^a3).norm()

$\displaystyle 3 \sqrt{2}$
In [19]:
B = a1^a3
B**2

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [21]:
T = a1^a3^a2
B*T

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [22]:
B|T

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [23]:
(1/2)*(B*T-T*B)

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [25]:
(B|B)*a2 - (B|(a1^a2))*a3 + (B|(a3^a2))*a1

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [26]:
A = sympy.Matrix([[-1,2,1],
[2,-1,1],
[2,1,1]])
A

$\displaystyle \left[\begin{array}{ccc}-1 & 2 & 1\\2 & -1 & 1\\2 & 1 & 1\end{array}\right]$
In [27]:
A.inv()

$\displaystyle \left[\begin{array}{ccc}- \frac{1}{3} & - \frac{1}{6} & \frac{1}{2}\\0 & - \frac{1}{2} & \frac{1}{2}\\\frac{2}{3} & \frac{5}{6} & - \frac{1}{2}\end{array}\right]$
In [29]:
e1 = -(1/3)*a1 + (2/3)*a3
e2 = -(1/6)*a1 -(1/2)*a2 + (5/6)*a3
e3 = (1/2)*a1 + (1/2)*a2 - (1/2)*a3

In [31]:
e1.norm(),e2.norm(),e3.norm()

$\displaystyle \left( 1.0, \ 1.0, \ 1.0\right)$
In [32]:
e1|e2, e1|e3, e2|e3

(5.55111512312578e-17, -2.22044604925031e-16, 1.38777878078145e-16)
In [34]:
B.exp()

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [35]:
B

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [36]:
i = B/B.norm()
i

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [43]:
pi = sympy.pi
sympy.cos(pi)

$\displaystyle -1$
In [46]:
(i*pi).exp()

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [47]:
theta = sympy.symbols('theta')
theta

$\displaystyle \theta$
In [48]:
R = ((1/2)*theta*i).exp()
R

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [49]:
R.rev()

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [54]:
Q = R.rev()*(a1^a2)*R
Q

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [51]:
I = (a1^a2^a3)/(a1^a2^a3).norm()
I

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [52]:
(a1^a2).inv()

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [53]:
1/(a1^a2)

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-53-12ee0e7071ee> in <module> ----> 1 1/(a1^a2) TypeError: unsupported operand type(s) for /: 'int' and 'Mv' 
In [55]:
I.rev()

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [57]:
q = Q*I.rev()
q

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [58]:
q|Q

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [59]:
Q^q

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [60]:
(Q^q).norm()

$\displaystyle 50$
In [63]:
B1 = a1^a2
B2 = a1^a3
B1 = B1/B1.norm()
B2 = B2/B2.norm()
B1|B2

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [71]:
def ang(u,v):
u1 = u/u.norm()
v1 = v/v.norm()
return u1|v1

In [72]:
ang(a2,a3)

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [75]:
b1 = 2.977*e1 + 0.253*e2 + 0.272*e3
b2 = -0.892*e1 + 2.261*e2 + 0.305*e3
b3 = 0.946*e1 + 1.335*e2 - 0.569*e3

In [156]:
import numpy as np
import scipy.linalg as la

def getG(L):
G = sympy.eye(len(L))
for i in range(len(L)):
for j in range(len(L)):
return G

def getA(g):
evals,P = la.eig(g)
evals = evals.real
sqrtD = np.diag(np.sqrt(evals))
A = sqrtD @ P.transpose()
return A


In [135]:
getG((b1,b2,b3))

$\displaystyle \left[\begin{array}{ccc}9.0 & -2.0 & 3.0\\-2.0 & 6.0 & 2.0\\3.0 & 2.0 & 3.0\end{array}\right]$
In [157]:
b1 = 2.977*e1 + 0.253*e2 + 0.272*e3
b2 = -0.892*e1 + 2.261*e2 + 0.305*e3
b3 = 0.946*e1 + 1.335*e2 - 0.569*e3

A = getA(G)
c1 = A[0,0]*e1 + A[1,0]*e2 + A[2,0]*e3
c2 = A[0,1]*e1 + A[1,1]*e2 + A[2,1]*e3
c3 = A[0,2]*e1 + A[1,2]*e2 + A[2,2]*e3

G,getG((b1,b2,b3)),getG((c1,c2,c3))

(array([[ 9, -2, 3], [-2, 6, 2], [ 3, 2, 3]]), Matrix([ [ 9.0, -2.0, 3.0], [-2.0, 6.0, 2.0], [ 3.0, 2.0, 3.0]]), Matrix([ [ 9.0, -2.0, 3.0], [-2.0, 6.0, 2.0], [ 3.0, 2.0, 3.0]]))
In [142]:
G

array([[ 9, -2, 3], [-2, 6, 2], [ 3, 2, 3]])
In [143]:
getG((b1,b2,b3))

$\displaystyle \left[\begin{array}{ccc}9.0 & -2.0 & 3.0\\-2.0 & 6.0 & 2.0\\3.0 & 2.0 & 3.0\end{array}\right]$
In [144]:
getG((c1,c2,c3))

$\displaystyle \left[\begin{array}{ccc}9.0 & -2.0 & 3.0\\-2.0 & 6.0 & 2.0\\3.0 & 2.0 & 3.0\end{array}\right]$
In [137]:
c1

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [138]:
b1

(Share server only supports KaTeX; open in CoCalc to see this formula.)
In [145]:
np.set_printoptions(precision=3)
A

array([[-0.272, -0.305, 0.569], [-2.977, 0.892, -0.946], [ 0.253, 2.261, 1.335]])
In [146]:
type(A)

numpy.ndarray
In [148]:
type(sympy.eye(3))

sympy.matrices.dense.MutableDenseMatrix
In [152]:
print(A)

[[-0.272 -0.305 0.569] [-2.977 0.892 -0.946] [ 0.253 2.261 1.335]]
In [ ]: