Skip to content

algebra.render

Functionality for pretty-printing multivectors in Jupyter notebooks

align(lhs, mv, func=lambda x: x)

Render a vector or a bivector with each component on a separate line

Parameters:

Name Type Description Default
lhs Symbol

String to put on the left hand side of the rendered equality

required
mv Mv

Multivector whose components are to be rendered on the right hand side, one per line

required
func Callable[[Mv], Mv]

Manipulation to be performed on each coefficient of the given vector or bivector

lambda x: x

Returns:

Type Description
Math

IPython.display.Math object holding the rendered LaTeX output

Source code in src/analemma/algebra/render.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def align(
    lhs: sp.Symbol, mv: mv.Mv, func: Callable[[mv.Mv], mv.Mv] = lambda x: x
) -> Math:
    """
    Render a vector or a bivector with each component on a separate line

    Parameters:
        lhs: String to put on the left hand side of the rendered equality
        mv: Multivector whose components are to be rendered on the right hand side, one per line
        func: Manipulation to be performed on each coefficient of the given vector or bivector

    Returns:
        IPython.display.Math object holding the rendered LaTeX output
    """
    return Math(
        _beginning_string(lhs) + _middle_string.join(_lines(mv, func)) + _end_string
    )

expression(lhs, rhs)

Render an expression

For example,

\(f_1\wedge f_2 = \cos(\alpha) \, e_1 \wedge e_2 - \sin(\alpha) \, e_2 \wedge 3_3\)

Parameters:

Name Type Description Default
lhs str

String to put on the left hand side of the rendered equality

required
rhs Symbol

Expression to be placed on the right hand side of the rendered equality

required

Returns:

Type Description
Math

IPython.display.Math object holding the rendered LaTeX output

Source code in src/analemma/algebra/render.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def expression(lhs: str, rhs: sp.Symbol) -> Math:
    r"""
    Render an expression

    For example,

    $f_1\wedge f_2 = \cos(\alpha) \, e_1 \wedge e_2 - \sin(\alpha) \, e_2 \wedge 3_3$

    Parameters:
        lhs: String to put on the left hand side of the rendered equality
        rhs: Expression to be placed on the right hand side of the rendered equality

    Returns:
        IPython.display.Math object holding the rendered LaTeX output
    """
    # would like to use mv.Fmt but it seems to give LaTeX output which doesn't
    # render after eg jupyter nbconvert --to Markdown
    return Math(rf"""
        \begin{{equation}}
            {lhs} = {latex(rhs)} \nonumber
        \end{{equation}}
        """)

expressions(lhss, rhss)

Render multiple expressions

Parameters:

Name Type Description Default
lhss Tuple[str]

Tuple of strings to put on the left hand side of each rendered equality

required
rhss Tuple[Symbol]

Tuple of expressions to be placed on the right hand side of each rendered equality

required

Returns:

Type Description
Math

IPython.display.Math object holding the rendered LaTeX output

Source code in src/analemma/algebra/render.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def expressions(lhss: Tuple[str], rhss: Tuple[sp.Symbol]) -> Math:
    """
    Render multiple expressions

    Parameters:
        lhss: Tuple of strings to put on the left hand side of each rendered equality
        rhss: Tuple of expressions to be placed on the right hand side of each rendered equality

    Returns:
        IPython.display.Math object holding the rendered LaTeX output
    """
    equations = []
    for lhs, rhs in zip(lhss, rhss):
        equations.append(
            rf"""
        \begin{{equation}}
            {lhs} = {latex(rhs)} \nonumber
        \end{{equation}}
        """
        )
    return Math(r"\\".join(equations))

frame(symbol, frame)

Render a vector frame

For example,

\(f_1 = \cos(\alpha)\cos(\psi) e_1 + \sin(\psi) e_2 + \sin(\alpha)\cos(\psi) e_3\)

\(f_2 = -\cos(\alpha)\sin(\psi) e_1 + \cos(\psi) e_2 - \sin(\alpha)\sin(\psi) e_3\)

\(f_3 = -\sin(\alpha) e_1 + \cos(\alpha) e_3\)

Parameters:

Name Type Description Default
symbol str

Symbol to use for each vector in the frame

required
frame Tuple[Mv]

Tuple of vectors to be rendered

required

Returns:

Type Description
Math

IPython.display.Math object holding the rendered LaTeX output

Source code in src/analemma/algebra/render.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def frame(symbol: str, frame: Tuple[mv.Mv]) -> Math:
    r"""
    Render a vector frame

    For example,

    $f_1 = \cos(\alpha)\cos(\psi) e_1 + \sin(\psi) e_2 + \sin(\alpha)\cos(\psi) e_3$

    $f_2 = -\cos(\alpha)\sin(\psi) e_1 + \cos(\psi) e_2 - \sin(\alpha)\sin(\psi) e_3$

    $f_3 = -\sin(\alpha) e_1 + \cos(\alpha) e_3$

    Parameters:
        symbol: Symbol to use for each vector in the frame
        frame: Tuple of vectors to be rendered

    Returns:
        IPython.display.Math object holding the rendered LaTeX output
    """

    beginning = r"\begin{align} "

    lines = _frame_lines(symbol, frame)
    middle = r"\nonumber \\ ".join(lines)

    end = r"\nonumber \end{align}"

    return Math(beginning + middle + end)

multivector(mv)

Render a GAlgebra multivector in a manner compatible with mkdocs

Multivector mv on its own in a cell appears fine in the notebook but when nbconverted to Markdown comes out as \begin{equation} x \end{equation} instead of eg \(\displaystyle x\) and consequently isn't rendered in HTML after running mkdocs.

Source code in src/analemma/algebra/render.py
12
13
14
15
16
17
18
19
20
def multivector(mv: mv.Mv) -> Math:
    r"""
    Render a GAlgebra multivector in a manner compatible with mkdocs

    Multivector mv on its own in a cell appears fine in the notebook but when nbconverted
    to Markdown comes out as \begin{equation*} x \end{equation*} instead of eg $\displaystyle x$
    and consequently isn't rendered in HTML after running mkdocs.
    """
    return Math(latex(mv))

simplification(lhs, stages, as_lines=None)

Render the simplification of a vector or a bivector

Parameters:

Name Type Description Default
lhs str

String to put on the left hand side of the rendered equality

required
stages Tuple[Mv]

Tuple of equivalent multivectors in different forms

required
as_lines Tuple[bool]

Flag to indicate whether to render each stage in multiline format

None

Returns:

Type Description
Math

IPython.display.Math object holding the rendered LaTeX output

Source code in src/analemma/algebra/render.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
def simplification(
    lhs: str, stages: Tuple[mv.Mv], as_lines: Tuple[bool] = None
) -> Math:
    """
    Render the simplification of a vector or a bivector

    Parameters:
        lhs: String to put on the left hand side of the rendered equality
        stages: Tuple of equivalent multivectors in different forms
        as_lines: Flag to indicate whether to render each stage in multiline format

    Returns:
        IPython.display.Math object holding the rendered LaTeX output
    """

    if as_lines is None:
        as_lines = [False] * len(stages)

    stage_middles = []
    for do_lines, stage in zip(as_lines, stages):
        if do_lines:
            lines = _lines(stage)
            stage_middles.append(_middle_string.join(lines))
        else:
            stage_middles.append(latex(stage))

    middle = (_middle_string + " = ").join(stage_middles)

    return Math(_beginning_string(lhs) + middle + _end_string)