Printing¶
As we have already seen, Diofant can pretty print its output using Unicode characters. This is a short introduction to the most common printing options available in Diofant. The most common ones are
 Str
 Repr
 2D Pretty Printer (Unicode or ASCII)
 LaTeX
 Dot
In addition to these, there are also “printers” that can output Diofant objects to code, such as C, Fortran, or Mathematica.
Best printer is enabled automatically for interactive session
(i.e. \(\LaTeX\) in the IPython notebooks, pretty printer in the IPython
console or str printer in the Python console). If you want manually
configure pretty printing, please use the
init_printing()
function.
Lets take this simple expression
>>> expr = Integral(sqrt(1/x), x)
and try several available printers.
Str¶
To get a string form of an expression, use str
. This is also
the form that is produced by print()
. String forms are designed
to be easy to read and mostly to be in a form that is a correct Python syntax
so that it can be copied and pasted.
>>> str(expr)
'Integral(sqrt(1/x), x)'
>>> print(expr)
Integral(sqrt(1/x), x)
Repr¶
The repr form of an expression is designed to show the exact form of
an expression, it would yield an object with the same value when
passed to eval()
. To get it, use repr()
.
>>> repr(expr)
"Integral(Pow(Pow(Symbol('x'), Integer(1)), Rational(1, 2)), Tuple(Symbol('x')))"
The repr form is mostly useful for understanding how an expression is built internally.
2D Pretty Printer¶
A twodimensional (2D) textual representation of the expression can be
obtained with pretty()
.
>>> pretty(expr)
'⌠ \n⎮ ___ \n⎮ ╱ 1 \n⎮ ╱ ─ dx\n⎮ ╲╱ x \n⌡ '
>>> print(_)
⌠
⎮ ___
⎮ ╱ 1
⎮ ╱ ─ dx
⎮ ╲╱ x
⌡
You can pass use_unicode=False
to use ASCII symbols.
>>> print(pretty(expr, use_unicode=False))
/

 ___
 / 1
 /  dx
 \/ x

/
pprint()
prints the output to
the screen.
>>> pprint(expr)
⌠
⎮ ___
⎮ ╱ 1
⎮ ╱ ─ dx
⎮ ╲╱ x
⌡
LaTeX¶
To get the \(\LaTeX\) form of an expression, use
latex()
.
>>> print(latex(expr))
\int \sqrt{\frac{1}{x}}\, dx
Dot¶
dotprint()
function prints output to dot
format, which can be rendered with Graphviz: