galgebra.printer

ANSI Enhanced Text Printing, Text Printer and LaTeX Printer for all Geometric Algebra classes

\(\LaTeX\) printing

Note

galgebra works out of the box with the usual sympy printing and will show Latex in IPython by default. In many cases, all that is needed is:

sympy.init_printing(
    use_latex='mathjax',
    latex_printer=galgebra.printer.latex,
    # described below in `GaLatexPrinter`
    omit_function_args=True,
    omit_partial_derivative_fraction=True,
)

The rest of this section primarily describes an orthogonal feature for writing out .tex files with print().

The latex printer is turned on with the Format() function

galgebra.printer.Format(Fmode=True, Dmode=True, ipy=False)[source]

where Fmode is the function printing mode that suppresses printing arguments, Dmode is the derivative printing mode that does not use fractions, and ipy=True is the IPython notebook mode that does not redirect the print output.

The latex output is post processed and displayed with the function

galgebra.printer.xpdf(filename='tmplatex.tex', debug=False)[source]

where filename is the name of the tex file one would keep for future inclusion in documents and debug=True would display the tex file immediately.

There are three options for printing multivectors in latex. They are accessed with the multivector member function

galgebra.mv.Mv.Fmt(self, fmt=1, title=None)

where fmt of 1, 2, or 3 determines whether the entire multivector A is printed entirely on one line, or one grade is printed per line, or one base is printed per line. If title is not None then the latex string generated is of the form:

title + ' = ' + str(A)

where it is assumed that title is a latex math mode string. If title contains ‘%’ it is treated as a pure latex math mode string. If it does not contain ‘%’ then the following character mappings are applied:

'grad' -> '\bm{\nabla} '
'*'    -> ''
'^'    -> '\W '
'|'    -> '\cdot '
'>'    -> '\lfloor '
'<'    -> '\rfloor '

In the case of a print statement of the form:

print(title, A)

everything in the title processing still applies except that the multivector formatting is one multivector per line.

For print statements of the form:

print(title)

where no program variables are printed if title contains # then title is printed as regular latex line. If title does not contain # then title is printed in equation mode. % has the same effect in title as in the Fmt() member function.

Members

galgebra.printer.isinteractive()[source]

We will assume that if ipython is running then jupyter notebook is running.

galgebra.printer.oprint(*args, dict_mode=False)[source]

Debug printing for iterated (list/tuple/dict/set) objects. args is of form (title1, object1, title2, object2, ...) and prints:

title1 = object1
title2 = object2
...

If you only wish to print a title set object = None.

galgebra.printer.enhance_print(base='blue', fct='red', deriv='cyan')[source]

Enable ansi color codes in plain-text formatting.

Valid color names are:

  • 'black'

  • 'dark gray'

  • 'red'

  • 'bright red'

  • 'green'

  • 'bright green'

  • 'yellow'

  • 'bright yellow'

  • 'blue'

  • 'bright blue'

  • 'purple'

  • 'bright purple'

  • 'cyan'

  • 'bright cyan'

  • 'bright gray'

  • 'white'

Pass None to disable coloring.

galgebra.printer.Eprint(*args, **kwargs)[source]

Alias for enhance_print()

class galgebra.printer.GaPrinter(settings=None)[source]

Bases: sympy.printing.str.StrPrinter

This subclass of the builtin string printer makes some customizations which make output a little more readable for GA usage.

The customizations are:

  • Derivative objects are printed as D{x}y instead of Derivative(y, x).

  • Function objects are printed without arguments. This is useful for defining fields over coords, but sometimes misfires.

  • A new dict_mode setting, which when True prints dict objects with -> and one entry per line.

  • New ANSI color settings:

    • derivative_color, for adjusting the color of D{x}.

    • function_color, for adjusting the color of argument-less functions.

    • basis_vector_color, for adjusting the color of basis vector symbols.

When galgebra.printer is imported, builtin sympy objects are patched to use this printer for their __repr__ instead of the builtin StrPrinter. There is currently no way to disable this patching.

class galgebra.printer.GaPrintable[source]

Bases: galgebra._utils.printable.Printable

Mixin class providing default implementations of printing hooks

class galgebra.printer.GaLatexPrinter(settings=None)[source]

Bases: sympy.printing.latex.LatexPrinter

This subclass of the builtin string printer makes some customizations which make output a little more readable for GA usage.

The customizations are:

  • A new omit_partial_derivative_fraction setting that affects the printing of Derivative objects, with possible values:

    • False, to use the sympy default, \(\pdiff{f}{x}\).

    • True, to use a condensed notation, \(\partial_{x}f\).

  • A new omit_function_args setting which affects the printing of Function objects, with possible values:

    • False, to use the sympy default, \({{f}\lp {x,y,z} \rp }\).

    • True, to print as \(f\). This is similar to the behavior of GaPrinter.

  • A change to function printing to allow function names to contain subscripts and superscripts.

  • Use of boldsymbol instead of mathbf for bold symbol names.

Note that this printer is not required for using GA objects, the base class printer will work fine too.

galgebra.printer.latex(expr, **settings)str[source]

Get the latex representation of expr using GaLatexPrinter.

Takes the same options as sympy.printing.latex.latex(); see that function for more information.

This can be used as the latex_printer argument to init_printing() to make IPython always use GaLatexPrinter.

galgebra.printer.print_latex(expr, **settings)[source]

Prints LaTeX representation of the given expression.

Takes the same settings as latex().

galgebra.printer.Format(Fmode: bool = True, Dmode: bool = True, inverse='full')[source]

Turns on latex printing with configurable options.

This redirects printer output so that latex compiler can capture it.

Format() is also required for printing from ipython notebook (note that xpdf() is not needed to print from ipython notebook).

Parameters
  • Fmode – Value for the omit_function_args setting of GaLatexPrinter.

  • Dmode – Value for the omit_partial_derivative_fraction setting of GaLatexPrinter.

galgebra.printer.tex(paper=14, 11, debug=False, prog=False, pt='10pt')[source]

Post processes LaTeX output (see comments below), adds preamble and postscript.

This postprocessing has two main behaviors:

  1. Converting strings on the left hand side of the last = into TeX. This translates the *, ^, |, >, <, <<, >>, grad, and rgrad operators of galgebra into the appropriate latex operators. If there is no = in the line, no conversion is applied.

  2. Wrapping lines of latex into equation* environments if they are not already in environments, and moving labels that were prepended outside align environments inside those environments.

Both behaviors are applied line by line, unless a line starts with the following text:

#% or %

Disables only behavior 1 for the rest of the line.

##

Disables behaviors 1 and 2 until the end of the next line starting with ##. This includes processing any of the other special characters, which will be emitted verbatim.

#

Disables behaviors 1 and 2 for the rest of the line.

We assume that if tex() is called, then Format() has been called at the beginning of the program.

galgebra.printer.xpdf(filename=None, paper=14, 11, crop=False, png=False, prog=False, debug=False, pt='10pt', pdfprog='pdflatex')[source]

Post processes LaTeX output (see comments below), adds preamble and postscript, generates tex file, inputs file to latex, displays resulting pdf file.

Arg Value Result pdfprog ‘pdflatex’ Use pdfprog to generate pdf output, only generate tex if pdfprog is None crop True Use “pdfcrop” to crop output file (pdfcrop must be installed, linux only) png True Use “convert” to produce png output (imagemagick must be installed, linux only)

We assume that if xpdf() is called then Format() has been called at the beginning of the program.

galgebra.printer.Print_Function()[source]

Print out the source of the current function

galgebra.printer.def_prec(gd: dict, op_ord: str = '<>|,^,*')None[source]

This is used with the GAeval() function to evaluate a string representing a multivector expression with a revised operator precedence.

Parameters
  • gd – The globals() dictionary to lookup variable names in.

  • op_ord – The order of operator precedence from high to low with groups of equal precedence separated by commas. The default precedence, '<>|,^,*', is that used by Hestenes ([HS84], p7, [DL03], p38). This means that the <, >, and | operations have equal precedence, followed by ^, and lastly *.

galgebra.printer.GAeval(s: str, pstr: bool = False)[source]

Evaluate a multivector expression string s.

The operator precedence and variable values within the string are controlled by def_prec(). The documentation for that function describes the default precedence.

The implementation works by adding parenthesis to the input string s according to the requested precedence, and then calling eval() on the result.

For example consider where X, Y, Z, and W are multivectors:

def_prec(globals())
V = GAeval('X|Y^Z*W')

The sympy variable V would evaluate to ((X|Y)^Z)*W.

Parameters
  • s – The string to evaluate.

  • pstr – If True, the values of s and s with parenthesis added to enforce operator precedence are printed.