# Demo of gprint

Author: Alan Bromborsky

Last updated: 2020-09-22

Original name: GAlgebraOutput.ipynb in http://www.faculty.luther.edu/~macdonal/GAfiles.zip

[1]:

# Make SymPy available to this program:
import sympy
from sympy import *

# Make GAlgebra available to this program:
from galgebra.ga import *
from galgebra.mv import *
from galgebra.printer import Fmt, GaPrinter, Format
# Fmt:       sets the way that a multivector's basis expansion is output.
# GaPrinter: makes GA output a little more readable.
# Format:    turns on latex printer.
from galgebra.gprinter import gFormat, gprint
gFormat()

$\displaystyle \DeclareMathOperator{\Tr}{Tr}\DeclareMathOperator{\Adj}{Adj}\newcommand{\bfrac}[2]{\displaystyle\frac{#1}{#2}}\newcommand{\lp}{\left (}\newcommand{\rp}{\right )}\newcommand{\paren}[1]{\lp {#1} \rp}\newcommand{\half}{\frac{1}{2}}\newcommand{\llt}{\left <}\newcommand{\rgt}{\right >}\newcommand{\abs}[1]{\left |{#1}\right | }\newcommand{\pdiff}[2]{\bfrac{\partial {#1}}{\partial {#2}}}\newcommand{\npdiff}[3]{\bfrac{\partial^{#3} {#1}}{\partial {#2}^{#3}}}\newcommand{\lbrc}{\left \{}\newcommand{\rbrc}{\right \}}\newcommand{\W}{\wedge}\newcommand{\prm}[1]{{#1}^{\prime}}\newcommand{\ddt}[1]{\bfrac{d{#1}}{dt}}\newcommand{\R}{\dagger}\newcommand{\deriv}[3]{\bfrac{d^{#3}#1}{d{#2}^{#3}}}\newcommand{\grade}[2]{\left < {#1} \right >_{#2}}\newcommand{\f}[2]{{#1}\lp {#2} \rp}\newcommand{\eval}[2]{\left . {#1} \right |_{#2}}\newcommand{\bs}[1]{\boldsymbol{#1}}\newcommand{\grad}{\bs{\nabla}}$
[2]:

# Set up standard G^3 geometric algebra
g3coords = (x,y,z) = symbols('x y z', real=True) # Without real=True, symbols are complex
g3 = Ga('\mathbf{e}', g=[1,1,1], coords=g3coords)
(ex, ey, ez) = g3.mv()     # Program names of basis vectors.
(exr, eyr, ezr) = g3.mvr() # Program names of reciprocal basis vectors.

[3]:

gprint(r'word word\ word \cdot ex<ey', r'\\ \text{word word\ word \cdot ex<ey)}')
# \\ gives a new line. The second string encloses the first in \text{}.

$\displaystyle \begin{equation*} word word\ word \cdot ex<ey\end{equation*}$
$\displaystyle \begin{equation*} \text{word word\ word \cdot ex<ey)}\end{equation*}$
[4]:

B = g3.mv('B', 'bivector')
Fmt(1)  # Set Fmt globally
gprint(r'\mathbf{B} =', B)         # B will be bold.
gprint(r'\mathbf{B} =', B.Fmt(3))  # Fmt(3) here only.
gprint(r'\mathbf{B} =', B)         # Global Fmt remembered.

$\displaystyle \mathbf{B} = B^{xy} \boldsymbol{\mathbf{e}}_{x}\wedge \boldsymbol{\mathbf{e}}_{y} + B^{xz} \boldsymbol{\mathbf{e}}_{x}\wedge \boldsymbol{\mathbf{e}}_{z} + B^{yz} \boldsymbol{\mathbf{e}}_{y}\wedge \boldsymbol{\mathbf{e}}_{z}$
\displaystyle \mathbf{B} = \begin{aligned}[t] & B^{xy} \boldsymbol{\mathbf{e}}_{x}\wedge \boldsymbol{\mathbf{e}}_{y} \\ & + B^{xz} \boldsymbol{\mathbf{e}}_{x}\wedge \boldsymbol{\mathbf{e}}_{z} \\ & + B^{yz} \boldsymbol{\mathbf{e}}_{y}\wedge \boldsymbol{\mathbf{e}}_{z} \end{aligned}
$\displaystyle \mathbf{B} = B^{xy} \boldsymbol{\mathbf{e}}_{x}\wedge \boldsymbol{\mathbf{e}}_{y} + B^{xz} \boldsymbol{\mathbf{e}}_{x}\wedge \boldsymbol{\mathbf{e}}_{z} + B^{yz} \boldsymbol{\mathbf{e}}_{y}\wedge \boldsymbol{\mathbf{e}}_{z}$
[5]:

gprint(r'\mathbf{B}^2 =', B*B)

$\displaystyle \mathbf{B}^2 = - {\left ( B^{xy} \right )}^{2} - {\left ( B^{xz} \right )}^{2} - {\left ( B^{yz} \right )}^{2}$
[6]:

M = g3.mv('M', 'mv')
gprint(r'\langle \mathbf{M} \rangle_2 =', M.grade(2))
# grade(2) could be replaced by, e.g., odd(), or omitted altogether.

$\displaystyle \langle \mathbf{M} \rangle_2 = M^{xy} \boldsymbol{\mathbf{e}}_{x}\wedge \boldsymbol{\mathbf{e}}_{y} + M^{xz} \boldsymbol{\mathbf{e}}_{x}\wedge \boldsymbol{\mathbf{e}}_{z} + M^{yz} \boldsymbol{\mathbf{e}}_{y}\wedge \boldsymbol{\mathbf{e}}_{z}$
[7]:

gprint(r'\alpha_1\mathbf{X}/\gamma_r^3')

$\displaystyle \alpha_1\mathbf{X}/\gamma_r^3$
[8]:

# Program name and output are different
theta = symbols('theta', real = True)
th = symbols('theta', real = True)  # This will save typing if theta is used a lot.
gprint(theta, ', ', th)

$\displaystyle \theta , \theta$
[9]:

grad = g3.grad

[10]:

grad

[10]:

$\displaystyle \mathbf{\mathbf{e}_x} \frac{\partial}{\partial x} + \mathbf{\mathbf{e}_y} \frac{\partial}{\partial y} + \mathbf{\mathbf{e}_z} \frac{\partial}{\partial z}$
[11]:

gprint(r'{\nabla}')

$\displaystyle {\nabla}$
[ ]: