qalgebra.printing.sympy module¶
Custom Printers for Sympy expressions
These classes are used by default by the QAlgebra printing systems as sub-printers for SymPy objects (e.g. for symbolic coefficients). They fix some issues with SymPy’s builtin printers:
factors like \(\frac{1}{\sqrt{2}}\) occur very commonly in quantum mechanics, and it is standard notation to write them as such. SymPy insists on rationalizing denominators, using \(\frac{\sqrt{2}}{2}\) instead. Our custom printers restore the canonical form. Note that internally, Sympy still uses the rationalized structure; but in any case, Sympy makes no guarantees between the algebraic structure of an expression and how it is printed.
Symbols (especially greek letters) are extremely common, and it’s much more readable if the string representation of an expression uses unicode for these. SymPy supports unicode “pretty-printing” (
sympy.printing.pretty.pretty.pretty_print()
) only in “2D”, where expressions are rendered as multiline unicode strings. While this is fine for interactive display, it does not work so well for a simplestr
. TheSympyUnicodePrinter
solves this by producing simple strings with unicode symbols.Some algebraic structures such as factorials, complex-conjugates and indexed symbols have sub-optimal rendering in
sympy.printing.str.StrPrinter
QAlgebra contains some custom subclasses of SymPy objects (e.g.
IdxSym
) that the default printers don’t know how to deal with (respectively, render incorrectly!)
Summary¶
Classes:
Variation of sympy |
|
Representation printer with support for |
|
Variation of sympy |
|
Printer that represents SymPy expressions as (single-line) unicode strings. |
Functions:
Try to de-rationalize the denominator of the given expression. |
Reference¶
-
qalgebra.printing.sympy.
derationalize_denom
(expr)[source]¶ Try to de-rationalize the denominator of the given expression.
The purpose is to allow to reconstruct e.g.
1/sqrt(2)
fromsqrt(2)/2
.Specifically, this matches expr against the following pattern:
Mul(..., Rational(n, d), Pow(d, Rational(1, 2)), ...)
and returns a tuple
(numerator, denom_sq, post_factor)
, wherenumerator
anddenom_sq
aren
andd
in the above pattern (of type int), respectively, andpost_factor
is the product of the remaining factors (...
in expr). The result will fulfill the following identity:(numerator / sqrt(denom_sq)) * post_factor == expr
If expr does not follow the appropriate pattern, a
ValueError
is raised.
-
class
qalgebra.printing.sympy.
SympyStrPrinter
(settings=None)[source]¶ Bases:
sympy.printing.str.StrPrinter
Variation of sympy
StrPrinter
that derationalizes denominators.Additionally, it contains the following modifications:
Support for
IdxSym
Rendering of
sympy.tensor.indexed.Indexed
as subscriptsRendering of
sympy.functions.combinatorial.factorials.factorial
as!
Option conjg_style to configure how complex conjugates are rendered:
'func' renders it as ``conjugate(...)
, and'star'
uses an exponentiated asterisk
-
class
qalgebra.printing.sympy.
SympyLatexPrinter
(settings=None)[source]¶ Bases:
sympy.printing.latex.LatexPrinter
Variation of sympy
LatexPrinter
that derationalizes denominators.Additionally, it contains the following modifications:
Support for
IdxSym
A setting conjg_style that allows to specify how complex conjugate are rendered:
'overline'
(the default) draws a line over the number, ‘star’ uses an exponentiated asterisk, and ‘func’ renders a aconjugate
function
-
class
qalgebra.printing.sympy.
SympyUnicodePrinter
(settings=None)[source]¶ Bases:
qalgebra.printing.sympy.SympyStrPrinter
Printer that represents SymPy expressions as (single-line) unicode strings.
This is a mixture of SymPy’s
StrPrinter
andsympy.printing.pretty.pretty.PrettyPrinter
(minus the 2D printing), with the same extensions asSympyStrPrinter
-
class
qalgebra.printing.sympy.
SympyReprPrinter
(settings=None)[source]¶ Bases:
sympy.printing.repr.ReprPrinter
Representation printer with support for
IdxSym