Skip to content

Commit 0cc73f5

Browse files
utensileric-wieser
authored andcommitted
Add 4 ways to print in GAlgebra
1 parent 503a652 commit 0cc73f5

File tree

1 file changed

+263
-0
lines changed

1 file changed

+263
-0
lines changed
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 4 ways to print in GAlgebra"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import sys\n",
17+
"import subprocess\n",
18+
"from IPython.core.display import display_pretty\n",
19+
"from sympy import *\n",
20+
"from galgebra.ga import Ga\n",
21+
"from galgebra.printer import Eprint, Format, xpdf"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 2,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"cga3d = Ga(r'e_1 e_2 e_3 e e_{0}',g='1 0 0 0 0,0 1 0 0 0,0 0 1 0 0,0 0 0 0 -1,0 0 0 -1 0')"
31+
]
32+
},
33+
{
34+
"cell_type": "markdown",
35+
"metadata": {},
36+
"source": [
37+
"## Printing in plain text"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 3,
43+
"metadata": {},
44+
"outputs": [
45+
{
46+
"name": "stdout",
47+
"output_type": "stream",
48+
"text": [
49+
"e_1^e_2^e_3^e^e_{0}\n"
50+
]
51+
}
52+
],
53+
"source": [
54+
"print(cga3d.I())"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"## Enhanced Console Printing\n",
62+
"\n",
63+
"If called `Eprint()` upfront, `print()` will do Enhanced Console Printing(colored printing with ANSI escape sequences):\n",
64+
"\n",
65+
"- code: https://github.com/pygae/galgebra/blob/master/examples/Terminal/terminal_check.py\n",
66+
"- output: https://nbviewer.jupyter.org/github/pygae/galgebra/blob/master/examples/ipython/Terminal.ipynb"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 4,
72+
"metadata": {},
73+
"outputs": [
74+
{
75+
"data": {
76+
"text/plain": [
77+
"\u001b[0;34me_1\u001b[0m^\u001b[0;34me_2\u001b[0m^\u001b[0;34me_3\u001b[0m^\u001b[0;34me\u001b[0m^\u001b[0;34me_{0}\u001b[0m\n"
78+
]
79+
},
80+
"metadata": {},
81+
"output_type": "display_data"
82+
}
83+
],
84+
"source": [
85+
"# Eprint modifies global state, start a new python process to avoid it\n",
86+
"# affecting the rest of this notebook.\n",
87+
"code = \"\"\"\n",
88+
"from galgebra.ga import Ga\n",
89+
"from galgebra.printer import Eprint\n",
90+
"Eprint()\n",
91+
"cga3d = Ga(r'e_1 e_2 e_3 e e_{0}',g='1 0 0 0 0,0 1 0 0 0,0 0 1 0 0,0 0 0 0 -1,0 0 0 -1 0')\n",
92+
"print(cga3d.I())\n",
93+
"\"\"\"\n",
94+
"result = subprocess.check_output([sys.executable, '-c', code], universal_newlines=True)\n",
95+
"display_pretty(result, raw=True)"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"metadata": {},
101+
"source": [
102+
"## MathJax printing in Jupyter Notebook\n",
103+
"\n",
104+
"In a Jupyter Notebook upfront, without calling `print()`, it will do LaTeX printing with MathJax for Geometric Algebra expressions.\n",
105+
"\n",
106+
"- code & output: https://nbviewer.jupyter.org/github/pygae/galgebra/blob/master/examples/ipython/colored_christoffel_symbols.ipynb"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 5,
112+
"metadata": {},
113+
"outputs": [
114+
{
115+
"data": {
116+
"text/latex": [
117+
"\\begin{equation*} \\boldsymbol{e}_{1}\\wedge \\boldsymbol{e}_{2}\\wedge \\boldsymbol{e}_{3}\\wedge \\boldsymbol{e}\\wedge \\boldsymbol{e}_{{0}} \\end{equation*}"
118+
],
119+
"text/plain": [
120+
"e_1^e_2^e_3^e^e_{0}"
121+
]
122+
},
123+
"execution_count": 5,
124+
"metadata": {},
125+
"output_type": "execute_result"
126+
}
127+
],
128+
"source": [
129+
"cga3d.I()"
130+
]
131+
},
132+
{
133+
"cell_type": "markdown",
134+
"metadata": {},
135+
"source": [
136+
"## LaTeX printing\n",
137+
"\n",
138+
"If called `Format()` upfront and `xpdf()` eventually, `print()` will do LaTeX printing (internally, the standard output will be redirected to a buffer for later consumption).\n",
139+
"\n",
140+
"- code: https://github.com/pygae/galgebra/blob/master/examples/LaTeX/colored_christoffel_symbols.py\n",
141+
"- output: https://nbviewer.jupyter.org/github/pygae/galgebra/blob/master/examples/ipython/LaTeX.ipynb"
142+
]
143+
},
144+
{
145+
"cell_type": "code",
146+
"execution_count": 6,
147+
"metadata": {},
148+
"outputs": [],
149+
"source": [
150+
"Format()"
151+
]
152+
},
153+
{
154+
"cell_type": "code",
155+
"execution_count": 7,
156+
"metadata": {},
157+
"outputs": [],
158+
"source": [
159+
"print(cga3d.I())"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": 8,
165+
"metadata": {},
166+
"outputs": [],
167+
"source": [
168+
"xpdf(filename='test_latex_output.tex', paper=(9, 10), pdfprog=None)"
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": 9,
174+
"metadata": {},
175+
"outputs": [
176+
{
177+
"data": {
178+
"text/plain": [
179+
"\n",
180+
"\\documentclass[10pt,fleqn]{report}\n",
181+
"\\usepackage[vcentering]{geometry}\n",
182+
"\\geometry{papersize={9in,10in},total={8in,9in}}\n",
183+
"\n",
184+
"\\pagestyle{empty}\n",
185+
"\\usepackage[latin1]{inputenc}\n",
186+
"\\usepackage{amsmath}\n",
187+
"\\usepackage{amsfonts}\n",
188+
"\\usepackage{amssymb}\n",
189+
"\\usepackage{amsbsy}\n",
190+
"\\usepackage{tensor}\n",
191+
"\\usepackage{listings}\n",
192+
"\\usepackage{color}\n",
193+
"\\usepackage{xcolor}\n",
194+
"\\usepackage{bm}\n",
195+
"\\usepackage{breqn}\n",
196+
"\\definecolor{gray}{rgb}{0.95,0.95,0.95}\n",
197+
"\\setlength{\\parindent}{0pt}\n",
198+
"\\DeclareMathOperator{\\Tr}{Tr}\n",
199+
"\\DeclareMathOperator{\\Adj}{Adj}\n",
200+
"\\newcommand{\\bfrac}[2]{\\displaystyle\\frac{#1}{#2}}\n",
201+
"\\newcommand{\\lp}{\\left (}\n",
202+
"\\newcommand{\\rp}{\\right )}\n",
203+
"\\newcommand{\\paren}[1]{\\lp {#1} \\rp}\n",
204+
"\\newcommand{\\half}{\\frac{1}{2}}\n",
205+
"\\newcommand{\\llt}{\\left <}\n",
206+
"\\newcommand{\\rgt}{\\right >}\n",
207+
"\\newcommand{\\abs}[1]{\\left |{#1}\\right | }\n",
208+
"\\newcommand{\\pdiff}[2]{\\bfrac{\\partial {#1}}{\\partial {#2}}}\n",
209+
"\\newcommand{\\lbrc}{\\left \\{}\n",
210+
"\\newcommand{\\rbrc}{\\right \\}}\n",
211+
"\\newcommand{\\W}{\\wedge}\n",
212+
"\\newcommand{\\prm}[1]{{#1}'}\n",
213+
"\\newcommand{\\ddt}[1]{\\bfrac{d{#1}}{dt}}\n",
214+
"\\newcommand{\\R}{\\dagger}\n",
215+
"\\newcommand{\\deriv}[3]{\\bfrac{d^{#3}#1}{d{#2}^{#3}}}\n",
216+
"\\newcommand{\\grade}[1]{\\left < {#1} \\right >}\n",
217+
"\\newcommand{\\f}[2]{{#1}\\lp{#2}\\rp}\n",
218+
"\\newcommand{\\eval}[2]{\\left . {#1} \\right |_{#2}}\n",
219+
"\\newcommand{\\Nabla}{\\boldsymbol{\\nabla}}\n",
220+
"\\newcommand{\\eb}{\\boldsymbol{e}}\n",
221+
"\\usepackage{float}\n",
222+
"\\floatstyle{plain} % optionally change the style of the new float\n",
223+
"\\newfloat{Code}{H}{myc}\n",
224+
"\\lstloadlanguages{Python}\n",
225+
"\n",
226+
"\\begin{document}\n",
227+
"\\begin{equation*} \\boldsymbol{e}_{1}\\wedge \\boldsymbol{e}_{2}\\wedge \\boldsymbol{e}_{3}\\wedge \\boldsymbol{e}\\wedge \\boldsymbol{e}_{{0}} \\end{equation*}\n",
228+
"\\end{document}\n"
229+
]
230+
},
231+
"metadata": {},
232+
"output_type": "display_data"
233+
}
234+
],
235+
"source": [
236+
"with open('test_latex_output.tex') as f:\n",
237+
" data = f.read()\n",
238+
"display_pretty(data, raw=True)"
239+
]
240+
}
241+
],
242+
"metadata": {
243+
"kernelspec": {
244+
"display_name": "Python 3",
245+
"language": "python",
246+
"name": "python3"
247+
},
248+
"language_info": {
249+
"codemirror_mode": {
250+
"name": "ipython",
251+
"version": 3
252+
},
253+
"file_extension": ".py",
254+
"mimetype": "text/x-python",
255+
"name": "python",
256+
"nbconvert_exporter": "python",
257+
"pygments_lexer": "ipython3",
258+
"version": "3.8.0"
259+
}
260+
},
261+
"nbformat": 4,
262+
"nbformat_minor": 2
263+
}

0 commit comments

Comments
 (0)