Printing

See the Printing section in Tutorial for introduction into printing.

This guide documents the printing system in Diofant and how it works internally.

Printer Class

Printing subsystem driver

Diofant’s printing system works the following way: Any expression can be passed to a designated Printer who then is responsible to return an adequate representation of that expression.

The basic concept is the following:
  1. Let the object print itself if it knows how.
  2. Take the best fitting method defined in the printer.
  3. As fall-back use the emptyPrinter method for the printer.

Some more information how the single concepts work and who should use which:

  1. The object prints itself

    This was the original way of doing printing in diofant. Every class had its own latex, mathml, str and repr methods, but it turned out that it is hard to produce a high quality printer, if all the methods are spread out that far. Therefore all printing code was combined into the different printers, which works great for built-in diofant objects, but not that good for user defined classes where it is inconvenient to patch the printers.

    Nevertheless, to get a fitting representation, the printers look for a specific method in every object, that will be called if it’s available and is then responsible for the representation. The name of that method depends on the specific printer and is defined under Printer.printmethod.

  2. Take the best fitting method defined in the printer.

    The printer loops through expr classes (class + its bases), and tries to dispatch the work to _print_<EXPR_CLASS>

    e.g., suppose we have the following class hierarchy:

        Basic
        |
        Atom
        |
        Number
        |
    Rational
    

    then, for expr=Rational(…), in order to dispatch, we will try calling printer methods as shown in the figure below:

    p._print(expr)
    |
    |-- p._print_Rational(expr)
    |
    |-- p._print_Number(expr)
    |
    |-- p._print_Atom(expr)
    |
    `-- p._print_Basic(expr)
    

    if ._print_Rational method exists in the printer, then it is called, and the result is returned back.

    otherwise, we proceed with trying Rational bases in the inheritance order.

  3. As fall-back use the emptyPrinter method for the printer.

    As fall-back self.emptyPrinter will be called with the expression. If not defined in the Printer subclass this will be the same as str(expr).

The main class responsible for printing is Printer (see also its source code):

class diofant.printing.printer.Printer(settings=None)[source]

Generic printer

Its job is to provide infrastructure for implementing new printers easily.

Basically, if you want to implement a printer, all you have to do is:

  1. Subclass Printer.

  2. Define Printer.printmethod in your subclass. If a object has a method with that name, this method will be used for printing.

  3. In your subclass, define _print_<CLASS> methods

    For each class you want to provide printing to, define an appropriate method how to do it. For example if you want a class FOO to be printed in its own way, define _print_FOO:

    def _print_FOO(self, e):
        ...
    

    this should return how FOO instance e is printed

    Also, if BAR is a subclass of FOO, _print_FOO(bar) will be called for instance of BAR, if no _print_BAR is provided. Thus, usually, we don’t need to provide printing routines for every class we want to support – only generic routine has to be provided for a set of classes.

    A good example for this are functions - for example PrettyPrinter only defines _print_Function, and there is no _print_sin, _print_tan, etc…

    On the other hand, a good printer will probably have to define separate routines for Symbol, Atom, Number, Integral, Limit, etc…

  4. If convenient, override self.emptyPrinter

    This callable will be called to obtain printing result as a last resort, that is when no appropriate print method was found for an expression.

Examples

Here we will overload StrPrinter.

>>> from diofant.printing.str import StrPrinter
>>> class CustomStrPrinter(StrPrinter):
...     def _print_Derivative(self, expr):
...         return str(expr.args[0].func) + "'"*len(expr.args[1:])
>>> def mystr(e):
...     return CustomStrPrinter().doprint(e)
>>> t = Symbol('t')
>>> x = Function('x')(t)
>>> print(mystr(x.diff(t, 2)))
x''
printmethod = None
_print(expr, *args, **kwargs)[source]

Internal dispatcher

Tries the following concepts to print an expression:
  1. Let the object print itself if it knows how.
  2. Take the best fitting method defined in the printer.
  3. As fall-back use the emptyPrinter method for the printer.
doprint(expr)[source]

Returns printer’s representation for expr (as a string)

classmethod set_global_settings(**settings)[source]

Set system-wide printing settings.

PrettyPrinter Class

The pretty printing subsystem is implemented in diofant.printing.pretty.pretty by the PrettyPrinter class deriving from Printer. It relies on the modules diofant.printing.pretty.stringPict, and diofant.printing.pretty.pretty_symbology for rendering nice-looking formulas.

The module stringPict provides a base class stringPict and a derived class prettyForm that ease the creation and manipulation of formulas that span across multiple lines.

The module pretty_symbology provides primitives to construct 2D shapes (hline, vline, etc) together with a technique to use unicode automatically when possible.

class diofant.printing.pretty.pretty.PrettyPrinter(settings=None)[source]

Printer, which converts an expression into 2D ASCII-art figure.

doprint(expr)[source]

Returns printer’s representation for expr (as a string)

diofant.printing.pretty.pretty.pprint(expr, **settings)
diofant.printing.pretty.pretty.pretty(expr, **settings)[source]

Returns a string containing the prettified form of expr.

For information on keyword arguments see pretty_print function.

diofant.printing.pretty.pretty.pretty_print(expr, **settings)[source]

Prints expr in pretty form.

pprint is just a shortcut for this function.

Parameters:
expr : expression

the expression to print

wrap_line : bool, optional

line wrapping enabled/disabled, defaults to True

num_columns : int or None, optional

number of columns before line breaking (default to None which reads the terminal width), useful when using Diofant without terminal.

use_unicode : bool or None, optional

use unicode characters, such as the Greek letter pi instead of the string pi.

full_prec : bool or string, optional

use full precision. Default to “auto”

order : bool or string, optional

set to ‘none’ for long expressions if slow; default is None

CCodePrinter

This class implements C code printing (i.e. it converts Python expressions to strings of C code).

Usage:

>>> print_ccode(sin(x)**2 + cos(x)**2)
pow(sin(x), 2) + pow(cos(x), 2)
>>> print_ccode(2*x + cos(x), assign_to="result")
result = 2*x + cos(x);
>>> print_ccode(Abs(x**2))
fabs(pow(x, 2))
diofant.printing.ccode.known_functions = {'Abs': [(<function <lambda> at 0x7fa3faf84b70>, 'fabs')], 'acos': 'acos', 'acosh': 'acosh', 'asin': 'asin', 'asinh': 'asinh', 'atan': 'atan', 'atan2': 'atan2', 'atanh': 'atanh', 'ceiling': 'ceil', 'cos': 'cos', 'cosh': 'cosh', 'erf': 'erf', 'exp': 'exp', 'floor': 'floor', 'gamma': 'tgamma', 'log': 'log', 'sin': 'sin', 'sinh': 'sinh', 'tan': 'tan', 'tanh': 'tanh'}
class diofant.printing.ccode.CCodePrinter(settings={})[source]

A printer to convert python expressions to strings of c code

printmethod = '_ccode'
indent_code(code)[source]

Accepts a string of code or a list of code lines

diofant.printing.ccode.ccode(expr, assign_to=None, **settings)[source]

Converts an expr to a string of c code

Parameters:
expr : Expr

A diofant expression to be converted.

assign_to : optional

When given, the argument is used as the name of the variable to which the expression is assigned. Can be a string, Symbol, MatrixSymbol, or Indexed type. This is helpful in case of line-wrapping, or for expressions that generate multi-line statements.

precision : integer, optional

The precision for numbers such as pi [default=15].

user_functions : dict, optional

A dictionary where the keys are string representations of either FunctionClass or UndefinedFunction instances and the values are their desired C string representations. Alternatively, the dictionary value can be a list of tuples i.e. [(argument_test, cfunction_string)] or [(argument_test, cfunction_formater)]. See below for examples.

dereference : iterable, optional

An iterable of symbols that should be dereferenced in the printed code expression. These would be values passed by address to the function. For example, if dereference=[a], the resulting code would print (*a) instead of a.

human : bool, optional

If True, the result is a single string that may contain some constant declarations for the number symbols. If False, the same information is returned in a tuple of (symbols_to_declare, not_supported_functions, code_text). [default=True].

contract: bool, optional

If True, Indexed instances are assumed to obey tensor contraction rules and the corresponding nested loops over indices are generated. Setting contract=False will not generate loops, instead the user is responsible to provide values for the indices in the code. [default=True].

Examples

>>> x, tau = symbols("x, tau")
>>> ccode((2*tau)**Rational(7, 2))
'8*sqrt(2)*pow(tau, 7.0L/2.0L)'
>>> ccode(sin(x), assign_to="s")
's = sin(x);'

Simple custom printing can be defined for certain types by passing a dictionary of {“type” : “function”} to the user_functions kwarg. Alternatively, the dictionary value can be a list of tuples i.e. [(argument_test, cfunction_string)].

>>> custom_functions = {
...   "ceiling": "CEIL",
...   "Abs": [(lambda x: not x.is_integer, "fabs"),
...           (lambda x: x.is_integer, "ABS")],
...   "func": "f"
... }
>>> func = Function('func')
>>> ccode(func(Abs(x) + ceiling(x)), user_functions=custom_functions)
'f(fabs(x) + CEIL(x))'

or if the C-function takes a subset of the original arguments:

>>> ccode(2**x + 3**x, user_functions={'Pow': [
...   (lambda b, e: b == 2, lambda b, e: 'exp2(%s)' % e),
...   (lambda b, e: b != 2, 'pow')]})
'exp2(x) + pow(3, x)'

Piecewise expressions are converted into conditionals. If an assign_to variable is provided an if statement is created, otherwise the ternary operator is used. Note that if the Piecewise lacks a default term, represented by (expr, True) then an error will be thrown. This is to prevent generating an expression that may not evaluate to anything.

>>> expr = Piecewise((x + 1, x > 0), (x, True))
>>> print(ccode(expr, tau))
if (x > 0) {
tau = x + 1;
}
else {
tau = x;
}

Support for loops is provided through Indexed types. With contract=True these expressions will be turned into loops, whereas contract=False will just print the assignment expression that should be looped over:

>>> len_y = 5
>>> y = IndexedBase('y', shape=[len_y])
>>> t = IndexedBase('t', shape=[len_y])
>>> Dy = IndexedBase('Dy', shape=[len_y - 1])
>>> i = Idx('i', len_y-1)
>>> e = Eq(Dy[i], (y[i+1]-y[i])/(t[i+1]-t[i]))
>>> ccode(e.rhs, assign_to=e.lhs, contract=False)
'Dy[i] = (y[i + 1] - y[i])/(t[i + 1] - t[i]);'

Matrices are also supported, but a MatrixSymbol of the same dimensions must be provided to assign_to. Note that any expression that can be generated normally can also exist inside a Matrix:

>>> mat = Matrix([x**2, Piecewise((x + 1, x > 0), (x, True)), sin(x)])
>>> A = MatrixSymbol('A', 3, 1)
>>> print(ccode(mat, A))
A[0] = pow(x, 2);
if (x > 0) {
   A[1] = x + 1;
}
else {
   A[1] = x;
}
A[2] = sin(x);
diofant.printing.ccode.print_ccode(expr, **settings)[source]

Prints C representation of the given expression.

Fortran Printing

The fcode function translates a diofant expression into Fortran code. The main purpose is to take away the burden of manually translating long mathematical expressions. Therefore the resulting expression should also require no (or very little) manual tweaking to make it compilable. The optional arguments of fcode can be used to fine-tune the behavior of fcode in such a way that manual changes in the result are no longer needed.

diofant.printing.fcode.fcode(expr, assign_to=None, **settings)[source]

Converts an expr to a string of fortran code

Parameters:
expr : Expr

A diofant expression to be converted.

assign_to : optional

When given, the argument is used as the name of the variable to which the expression is assigned. Can be a string, Symbol, MatrixSymbol, or Indexed type. This is helpful in case of line-wrapping, or for expressions that generate multi-line statements.

precision : integer, optional

The precision for numbers such as pi [default=15].

user_functions : dict, optional

A dictionary where keys are FunctionClass instances and values are their string representations. Alternatively, the dictionary value can be a list of tuples i.e. [(argument_test, cfunction_string)]. See below for examples.

human : bool, optional

If True, the result is a single string that may contain some constant declarations for the number symbols. If False, the same information is returned in a tuple of (symbols_to_declare, not_supported_functions, code_text). [default=True].

contract: bool, optional

If True, Indexed instances are assumed to obey tensor contraction rules and the corresponding nested loops over indices are generated. Setting contract=False will not generate loops, instead the user is responsible to provide values for the indices in the code. [default=True].

source_format : optional

The source format can be either ‘fixed’ or ‘free’. [default=’fixed’]

standard : integer, optional

The Fortran standard to be followed. This is specified as an integer. Acceptable standards are 66, 77, 90, 95, 2003, and 2008. Default is 77. Note that currently the only distinction internally is between standards before 95, and those 95 and after. This may change later as more features are added.

Examples

>>> x, tau = symbols("x, tau")
>>> fcode((2*tau)**Rational(7, 2))
'      8*sqrt(2.0d0)*tau**(7.0d0/2.0d0)'
>>> fcode(sin(x), assign_to="s")
'      s = sin(x)'

Custom printing can be defined for certain types by passing a dictionary of “type” : “function” to the user_functions kwarg. Alternatively, the dictionary value can be a list of tuples i.e. [(argument_test, cfunction_string)].

>>> custom_functions = {
...   "ceiling": "CEIL",
...   "floor": [(lambda x: not x.is_integer, "FLOOR1"),
...             (lambda x: x.is_integer, "FLOOR2")]
... }
>>> fcode(floor(x) + ceiling(x), user_functions=custom_functions)
'      CEIL(x) + FLOOR1(x)'

Piecewise expressions are converted into conditionals. If an assign_to variable is provided an if statement is created, otherwise the ternary operator is used. Note that if the Piecewise lacks a default term, represented by (expr, True) then an error will be thrown. This is to prevent generating an expression that may not evaluate to anything.

>>> expr = Piecewise((x + 1, x > 0), (x, True))
>>> print(fcode(expr, tau))
      if (x > 0) then
         tau = x + 1
      else
         tau = x
      end if

Support for loops is provided through Indexed types. With contract=True these expressions will be turned into loops, whereas contract=False will just print the assignment expression that should be looped over:

>>> len_y = 5
>>> y = IndexedBase('y', shape=[len_y])
>>> t = IndexedBase('t', shape=[len_y])
>>> Dy = IndexedBase('Dy', shape=[len_y - 1])
>>> i = Idx('i', len_y-1)
>>> e = Eq(Dy[i], (y[i+1]-y[i])/(t[i+1]-t[i]))
>>> fcode(e.rhs, assign_to=e.lhs, contract=False)
'      Dy(i) = (y(i + 1) - y(i))/(t(i + 1) - t(i))'

Matrices are also supported, but a MatrixSymbol of the same dimensions must be provided to assign_to. Note that any expression that can be generated normally can also exist inside a Matrix:

>>> mat = Matrix([x**2, Piecewise((x + 1, x > 0), (x, True)), sin(x)])
>>> A = MatrixSymbol('A', 3, 1)
>>> print(fcode(mat, A))
      A(1, 1) = x**2
         if (x > 0) then
      A(2, 1) = x + 1
         else
      A(2, 1) = x
         end if
      A(3, 1) = sin(x)
class diofant.printing.fcode.FCodePrinter(settings={})[source]

A printer to convert diofant expressions to strings of Fortran code

printmethod = '_fcode'
indent_code(code)[source]

Accepts a string of code or a list of code lines

Two basic examples:

>>> fcode(sqrt(1-x**2))
'      sqrt(-x**2 + 1)'
>>> fcode((3 + 4*I)/(1 - conjugate(x)))
'      (cmplx(3,4))/(-conjg(x) + 1)'

An example where line wrapping is required:

>>> expr = sqrt(1 - x**2).series(x, n=20).removeO()
>>> print(fcode(expr))
      -715.0d0/65536.0d0*x**18 - 429.0d0/32768.0d0*x**16 - 33.0d0/
     @ 2048.0d0*x**14 - 21.0d0/1024.0d0*x**12 - 7.0d0/256.0d0*x**10 -
     @ 5.0d0/128.0d0*x**8 - 1.0d0/16.0d0*x**6 - 1.0d0/8.0d0*x**4 - 1.0d0
     @ /2.0d0*x**2 + 1

In case of line wrapping, it is handy to include the assignment so that lines are wrapped properly when the assignment part is added.

>>> print(fcode(expr, assign_to="var"))
      var = -715.0d0/65536.0d0*x**18 - 429.0d0/32768.0d0*x**16 - 33.0d0/
     @ 2048.0d0*x**14 - 21.0d0/1024.0d0*x**12 - 7.0d0/256.0d0*x**10 -
     @ 5.0d0/128.0d0*x**8 - 1.0d0/16.0d0*x**6 - 1.0d0/8.0d0*x**4 - 1.0d0
     @ /2.0d0*x**2 + 1

For piecewise functions, the assign_to option is mandatory:

>>> print(fcode(Piecewise((x, x<1), (x**2, True)), assign_to="var"))
      if (x < 1) then
        var = x
      else
        var = x**2
      end if

Note that by default only top-level piecewise functions are supported due to the lack of a conditional operator in Fortran 77. Inline conditionals can be supported using the merge function introduced in Fortran 95 by setting of the kwarg standard=95:

>>> print(fcode(Piecewise((x, x<1), (x**2, True)), standard=95))
      merge(x, x**2, x < 1)

Loops are generated if there are Indexed objects in the expression. This also requires use of the assign_to option.

>>> A, B = map(IndexedBase, ['A', 'B'])
>>> m = Symbol('m', integer=True)
>>> i = Idx('i', m)
>>> print(fcode(2*B[i], assign_to=A[i]))
    do i = 1, m
        A(i) = 2*B(i)
    end do

Repeated indices in an expression with Indexed objects are interpreted as summation. For instance, code for the trace of a matrix can be generated with

>>> print(fcode(A[i, i], assign_to=x))
      x = 0
      do i = 1, m
          x = x + A(i, i)
      end do

By default, number symbols such as pi and E are detected and defined as Fortran parameters. The precision of the constants can be tuned with the precision argument. Parameter definitions are easily avoided using the N function.

>>> print(fcode(x - pi**2 - E))
      parameter (E = 2.71828182845905d0)
      parameter (pi = 3.14159265358979d0)
      x - pi**2 - E
>>> print(fcode(x - pi**2 - E, precision=25))
      parameter (E = 2.718281828459045235360287d0)
      parameter (pi = 3.141592653589793238462643d0)
      x - pi**2 - E
>>> print(fcode(N(x - pi**2, 25, strict=False)))
      x - 9.869604401089358618834491d0

When some functions are not part of the Fortran standard, it might be desirable to introduce the names of user-defined functions in the Fortran expression.

>>> print(fcode(1 - gamma(x)**2, user_functions={'gamma': 'mygamma'}))
      -mygamma(x)**2 + 1

However, when the user_functions argument is not provided, fcode attempts to use a reasonable default and adds a comment to inform the user of the issue.

>>> print(fcode(1 - gamma(x)**2))
C     Not supported in Fortran:
C     gamma
      -gamma(x)**2 + 1

By default the output is human readable code, ready for copy and paste. With the option human=False, the return value is suitable for post-processing with source code generators that write routines with multiple instructions. The return value is a three-tuple containing: (i) a set of number symbols that must be defined as ‘Fortran parameters’, (ii) a list functions that can not be translated in pure Fortran and (iii) a string of Fortran code. A few examples:

>>> fcode(1 - gamma(x)**2, human=False)
(set(), {gamma(x)}, '      -gamma(x)**2 + 1')
>>> fcode(1 - sin(x)**2, human=False)
(set(), set(), '      -sin(x)**2 + 1')
>>> fcode(x - pi**2, human=False)
({(pi, '3.14159265358979d0')}, set(), '      x - pi**2')

Mathematica code printing

diofant.printing.mathematica.known_functions = {'acos': [(<function <lambda> at 0x7fa3faf9dea0>, 'ArcCos')], 'asin': [(<function <lambda> at 0x7fa3faf9de18>, 'ArcSin')], 'atan': [(<function <lambda> at 0x7fa3faf9df28>, 'ArcTan')], 'binomial': [(<function <lambda> at 0x7fa3fafa6510>, 'Binomial')], 'cos': [(<function <lambda> at 0x7fa3faf9dc80>, 'Cos')], 'cosh': [(<function <lambda> at 0x7fa3fafa60d0>, 'Cosh')], 'cot': [(<function <lambda> at 0x7fa3faf9dd90>, 'Cot')], 'coth': [(<function <lambda> at 0x7fa3fafa61e0>, 'Coth')], 'csch': [(<function <lambda> at 0x7fa3fafa62f0>, 'Csch')], 'erfc': [(<function <lambda> at 0x7fa3fafa6598>, 'Erfc')], 'hyper': [(<function <lambda> at 0x7fa3fafa6488>, 'HypergeometricPFQ')], 'log': [(<function <lambda> at 0x7fa3faf9db70>, 'Log')], 'meijerg': [(<function <lambda> at 0x7fa3fafa6400>, 'MeijerG')], 'sech': [(<function <lambda> at 0x7fa3fafa6268>, 'Sech')], 'sign': [(<function <lambda> at 0x7fa3fafa6378>, 'Sign')], 'sin': [(<function <lambda> at 0x7fa3faf9dbf8>, 'Sin')], 'sinh': [(<function <lambda> at 0x7fa3fafa6048>, 'Sinh')], 'tan': [(<function <lambda> at 0x7fa3faf9dd08>, 'Tan')], 'tanh': [(<function <lambda> at 0x7fa3fafa6158>, 'Tanh')]}
class diofant.printing.mathematica.MCodePrinter(settings={})[source]

A printer to convert python expressions to strings of the Wolfram’s Mathematica code

printmethod = '_mcode'
doprint(expr)

Returns printer’s representation for expr (as a string)

diofant.printing.mathematica.mathematica_code(expr, **settings)[source]

Converts an expr to a string of the Wolfram Mathematica code

Examples

>>> mathematica_code(sin(x).series(x).removeO())
'(1/120)*x^5 - 1/6*x^3 + x'

LambdaPrinter

This classes implements printing to strings that can be used by the diofant.utilities.lambdify.lambdify() function.

class diofant.printing.lambdarepr.LambdaPrinter(settings=None)[source]

This printer converts expressions into strings that can be used by lambdify.

printmethod = '_diofantstr'
diofant.printing.lambdarepr.lambdarepr(expr, **settings)[source]

Returns a string usable for lambdifying.

LatexPrinter

This class implements LaTeX printing. See diofant.printing.latex.

diofant.printing.latex.accepted_latex_functions = ['arcsin', 'arccos', 'arctan', 'sin', 'cos', 'tan', 'sinh', 'cosh', 'tanh', 'sqrt', 'ln', 'log', 'sec', 'csc', 'cot', 'coth', 're', 'im', 'frac', 'root', 'arg']

list() -> new empty list list(iterable) -> new list initialized from iterable’s items

class diofant.printing.latex.LatexPrinter(settings=None)[source]
printmethod = '_latex'
doprint(expr)[source]

Returns printer’s representation for expr (as a string)

diofant.printing.latex.latex(expr, **settings)[source]

Convert the given expression to LaTeX representation.

>>> from diofant.abc import mu, r, tau
>>> print(latex((2*tau)**Rational(7, 2)))
8 \sqrt{2} \tau^{\frac{7}{2}}

Not using a print statement for printing, results in double backslashes for latex commands since that’s the way Python escapes backslashes in strings.

>>> latex((2*tau)**Rational(7, 2))
'8 \\sqrt{2} \\tau^{\\frac{7}{2}}'

order: Any of the supported monomial orderings (currently “lex”, “grlex”, or “grevlex”) and “none”. This parameter does nothing for Mul objects. For very large expressions, set the ‘order’ keyword to ‘none’ if speed is a concern.

mode: Specifies how the generated code will be delimited. ‘mode’ can be one of ‘plain’, ‘inline’, ‘equation’ or ‘equation*’. If ‘mode’ is set to ‘plain’, then the resulting code will not be delimited at all (this is the default). If ‘mode’ is set to ‘inline’ then inline LaTeX $ $ will be used. If ‘mode’ is set to ‘equation’ or ‘equation*’, the resulting code will be enclosed in the ‘equation’ or ‘equation*’ environment (remember to import ‘amsmath’ for ‘equation*’), unless the ‘itex’ option is set. In the latter case, the $$ $$ syntax is used.

>>> print(latex((2*mu)**Rational(7, 2), mode='plain'))
8 \sqrt{2} \mu^{\frac{7}{2}}
>>> print(latex((2*tau)**Rational(7, 2), mode='inline'))
$8 \sqrt{2} \tau^{7 / 2}$
>>> print(latex((2*mu)**Rational(7, 2), mode='equation*'))
\begin{equation*}8 \sqrt{2} \mu^{\frac{7}{2}}\end{equation*}
>>> print(latex((2*mu)**Rational(7, 2), mode='equation'))
\begin{equation}8 \sqrt{2} \mu^{\frac{7}{2}}\end{equation}

itex: Specifies if itex-specific syntax is used, including emitting $$ $$.

>>> print(latex((2*mu)**Rational(7, 2), mode='equation', itex=True))
$$8 \sqrt{2} \mu^{\frac{7}{2}}$$

fold_frac_powers: Emit “^{p/q}” instead of “^{frac{p}{q}}” for fractional powers.

>>> print(latex((2*tau)**Rational(7, 2), fold_frac_powers=True))
8 \sqrt{2} \tau^{7/2}

fold_func_brackets: Fold function brackets where applicable.

>>> print(latex((2*tau)**sin(Rational(7, 2))))
\left(2 \tau\right)^{\sin{\left (\frac{7}{2} \right )}}
>>> print(latex((2*tau)**sin(Rational(7, 2)), fold_func_brackets = True))
\left(2 \tau\right)^{\sin {\frac{7}{2}}}

fold_short_frac: Emit “p / q” instead of “frac{p}{q}” when the denominator is simple enough (at most two terms and no powers). The default value is \(True\) for inline mode, False otherwise.

>>> print(latex(3*x**2/y))
\frac{3 x^{2}}{y}
>>> print(latex(3*x**2/y, fold_short_frac=True))
3 x^{2} / y

long_frac_ratio: The allowed ratio of the width of the numerator to the width of the denominator before we start breaking off long fractions. The default value is 2.

>>> print(latex(Integral(r, r)/2/pi, long_frac_ratio=2))
\frac{\int r\, dr}{2 \pi}
>>> print(latex(Integral(r, r)/2/pi, long_frac_ratio=0))
\frac{1}{2 \pi} \int r\, dr

mul_symbol: The symbol to use for multiplication. Can be one of None, “ldot”, “dot”, or “times”.

>>> print(latex((2*tau)**sin(Rational(7, 2)), mul_symbol="times"))
\left(2 \times \tau\right)^{\sin{\left (\frac{7}{2} \right )}}

inv_trig_style: How inverse trig functions should be displayed. Can be one of “abbreviated”, “full”, or “power”. Defaults to “abbreviated”.

>>> print(latex(asin(Rational(7, 2))))
\operatorname{asin}{\left (\frac{7}{2} \right )}
>>> print(latex(asin(Rational(7, 2)), inv_trig_style="full"))
\arcsin{\left (\frac{7}{2} \right )}
>>> print(latex(asin(Rational(7, 2)), inv_trig_style="power"))
\sin^{-1}{\left (\frac{7}{2} \right )}

mat_str: Which matrix environment string to emit. “smallmatrix”, “matrix”, “array”, etc. Defaults to “smallmatrix” for inline mode, “matrix” for matrices of no more than 10 columns, and “array” otherwise.

>>> print(latex(Matrix(2, 1, [x, y])))
\left[\begin{matrix}x\\y\end{matrix}\right]
>>> print(latex(Matrix(2, 1, [x, y]), mat_str="array"))
\left[\begin{array}{c}x\\y\end{array}\right]

mat_delim: The delimiter to wrap around matrices. Can be one of “[“, “(“, or the empty string. Defaults to “[“.

>>> print(latex(Matrix(2, 1, [x, y]), mat_delim="("))
\left(\begin{matrix}x\\y\end{matrix}\right)

symbol_names: Dictionary of symbols and the custom strings they should be emitted as.

>>> print(latex(x**2, symbol_names={x: 'x_i'}))
x_i^{2}

latex also supports the builtin container types list, tuple, and dictionary.

>>> print(latex([2/x, y], mode='inline'))
$\left [ 2 / x, \quad y\right ]$

MathMLPrinter

This class is responsible for MathML printing. See diofant.printing.mathml.

More info on mathml content: http://www.w3.org/TR/MathML2/chapter4.html

class diofant.printing.mathml.MathMLPrinter(settings=None)[source]

Prints an expression to the MathML markup language

Whenever possible tries to use Content markup and not Presentation markup.

References

[1]http://www.w3.org/TR/MathML2/
printmethod = '_mathml'
doprint(expr)[source]

Prints the expression as MathML.

mathml_tag(e)[source]

Returns the MathML tag for an expression.

diofant.printing.mathml.mathml(expr, **settings)[source]

Returns the MathML representation of expr

diofant.printing.mathml.print_mathml(expr, **settings)[source]

Prints a pretty representation of the MathML code for expr

Examples

>>> print_mathml(x+1)  
<apply>
    <plus/>
    <ci>x</ci>
    <cn>1</cn>
</apply>

PythonPrinter

This class implements Python printing. Usage:

>>> print_python(5*x**3 + sin(x))
x = Symbol('x')
e = 5*x**3 + sin(x)

ReprPrinter

This printer generates executable code. This code satisfies the identity eval(srepr(expr)) == expr.

class diofant.printing.repr.ReprPrinter(settings=None)[source]
printmethod = '_diofantrepr'
emptyPrinter(expr)[source]

The fallback printer.

reprify(args, sep)[source]

Prints each item in \(args\) and joins them with \(sep\).

diofant.printing.repr.srepr(expr, **settings)[source]

return expr in repr form

StrPrinter

This module generates readable representations of Diofant expressions.

class diofant.printing.str.StrPrinter(settings=None)[source]
printmethod = '_diofantstr'
emptyPrinter(expr)[source]

str(object=’‘) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to ‘strict’.

diofant.printing.str.sstr(expr, **settings)[source]

Returns the expression as a string.

For large expressions where speed is a concern, use the setting order=’none’.

Examples

>>> a, b = symbols('a b')
>>> sstr(Eq(a + b, 0))
'Eq(a + b, 0)'
diofant.printing.str.sstrrepr(expr, **settings)[source]

return expr in mixed str/repr form

i.e. strings are returned in repr form with quotes, and everything else is returned in str form.

This function could be useful for hooking into sys.displayhook

Tree Printing

The functions in this module create a representation of an expression as a tree.

diofant.printing.tree.pprint_nodes(subtrees)[source]

Prettyprints systems of nodes.

Examples

>>> print(pprint_nodes(["a", "b1\nb2", "c"]))
+-a
+-b1
| b2
+-c
diofant.printing.tree.print_node(node)[source]

Returns information about the “node”.

This includes class name, string representation and assumptions.

diofant.printing.tree.tree(node)[source]

Returns a tree representation of “node” as a string.

It uses print_node() together with pprint_nodes() on node.args recursively.

See also: print_tree()

diofant.printing.tree.print_tree(node)[source]

Prints a tree representation of “node”.

Examples

>>> x = Symbol('x', odd=True)
>>> y = Symbol('y', even=True)
>>> print_tree(y**x)
Pow: y**x
commutative: True
+-Symbol: y
| algebraic: True
| commutative: True
| complex: True
| even: True
| extended_real: True
| finite: True
| hermitian: True
| infinite: False
| integer: True
| irrational: False
| noninteger: False
| odd: False
| rational: True
| real: True
| transcendental: False
+-Symbol: x
  algebraic: True
  commutative: True
  complex: True
  even: False
  extended_real: True
  finite: True
  hermitian: True
  imaginary: False
  infinite: False
  integer: True
  irrational: False
  noninteger: False
  nonzero: True
  odd: True
  rational: True
  real: True
  transcendental: False
  zero: False

See also: tree()

Implementation - Helper Classes/Functions

diofant.printing.conventions.split_super_sub(text)[source]

Split a symbol name into a name, superscripts and subscripts

The first part of the symbol name is considered to be its actual ‘name’, followed by super- and subscripts. Each superscript is preceded with a “^” character or by “__”. Each subscript is preceded by a “_” character. The three return values are the actual name, a list with superscripts and a list with subscripts.

>>> split_super_sub('a_x^1')
('a', ['1'], ['x'])
>>> split_super_sub('var_sub1__sup_sub2')
('var', ['sup'], ['sub1', 'sub2'])

CodePrinter

This class is a base class for other classes that implement code-printing functionality, and additionally lists a number of functions that cannot be easily translated to C or Fortran.

class diofant.printing.codeprinter.CodePrinter(settings=None)[source]

The base class for code-printing subclasses.

printmethod = '_diofantstr'
exception diofant.printing.codeprinter.AssignmentError[source]

Raised if an assignment variable for a loop is missing.

Precedence

A module providing information about the necessity of brackets

Pretty-Printing Implementation Helpers

diofant.printing.pretty.pretty_symbology.U(name)[source]

unicode character by name or None if not found

diofant.printing.pretty.pretty_symbology.pretty_use_unicode(flag=None)[source]

Set whether pretty-printer should use unicode by default

The following two functions return the Unicode version of the inputted Greek letter.

diofant.printing.pretty.pretty_symbology.g(l)[source]
diofant.printing.pretty.pretty_symbology.G(l)[source]
diofant.printing.pretty.pretty_symbology.greek_letters = ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta', 'iota', 'kappa', 'lamda', 'mu', 'nu', 'xi', 'omicron', 'pi', 'rho', 'sigma', 'tau', 'upsilon', 'phi', 'chi', 'psi', 'omega']

list() -> new empty list list(iterable) -> new list initialized from iterable’s items

diofant.printing.pretty.pretty_symbology.digit_2txt = {'0': 'ZERO', '1': 'ONE', '2': 'TWO', '3': 'THREE', '4': 'FOUR', '5': 'FIVE', '6': 'SIX', '7': 'SEVEN', '8': 'EIGHT', '9': 'NINE'}
diofant.printing.pretty.pretty_symbology.symb_2txt = {'(': 'LEFT PARENTHESIS', ')': 'RIGHT PARENTHESIS', '+': 'PLUS SIGN', '-': 'MINUS', '=': 'EQUALS SIGN', '[': 'LEFT SQUARE BRACKET', ']': 'RIGHT SQUARE BRACKET', 'int': 'INTEGRAL', 'sum': 'SUMMATION', '{': 'LEFT CURLY BRACKET', '{}': 'CURLY BRACKET', '}': 'RIGHT CURLY BRACKET'}

The following functions return the Unicode subscript/superscript version of the character.

diofant.printing.pretty.pretty_symbology.sub = {'(': '₍', ')': '₎', '+': '₊', '-': '₋', '0': '₀', '1': '₁', '2': '₂', '3': '₃', '4': '₄', '5': '₅', '6': '₆', '7': '₇', '8': '₈', '9': '₉', '=': '₌', 'a': 'ₐ', 'beta': 'ᵦ', 'chi': 'ᵪ', 'e': 'ₑ', 'gamma': 'ᵧ', 'h': 'ₕ', 'i': 'ᵢ', 'k': 'ₖ', 'l': 'ₗ', 'm': 'ₘ', 'n': 'ₙ', 'o': 'ₒ', 'p': 'ₚ', 'phi': 'ᵩ', 'r': 'ᵣ', 'rho': 'ᵨ', 's': 'ₛ', 't': 'ₜ', 'u': 'ᵤ', 'v': 'ᵥ', 'x': 'ₓ'}
diofant.printing.pretty.pretty_symbology.sup = {'(': '⁽', ')': '⁾', '+': '⁺', '-': '⁻', '0': '⁰', '1': '¹', '2': '²', '3': '³', '4': '⁴', '5': '⁵', '6': '⁶', '7': '⁷', '8': '⁸', '9': '⁹', '=': '⁼', 'i': 'ⁱ', 'n': 'ⁿ'}

The following functions return Unicode vertical objects.

diofant.printing.pretty.pretty_symbology.xobj(symb, length)[source]

Construct spatial object of given length.

return: [] of equal-length strings

diofant.printing.pretty.pretty_symbology.vobj(symb, height)[source]

Construct vertical object of a given height

see: xobj

diofant.printing.pretty.pretty_symbology.hobj(symb, width)[source]

Construct horizontal object of a given width

see: xobj

The following constants are for rendering roots and fractions.

diofant.printing.pretty.pretty_symbology.root = {2: '√', 3: '∛', 4: '∜'}
diofant.printing.pretty.pretty_symbology.VF(txt)[source]
diofant.printing.pretty.pretty_symbology.frac = {(1, 2): '½', (1, 3): '⅓', (1, 4): '¼', (1, 5): '⅕', (1, 6): '⅙', (1, 8): '⅛', (2, 3): '⅔', (2, 5): '⅖', (3, 4): '¾', (3, 5): '⅗', (3, 8): '⅜', (4, 5): '⅘', (5, 6): '⅚', (5, 8): '⅝', (7, 8): '⅞'}

The following constants/functions are for rendering atoms and symbols.

diofant.printing.pretty.pretty_symbology.xsym(sym)[source]

get symbology for a ‘character’

diofant.printing.pretty.pretty_symbology.atoms_table = {'EmptySet': '∅', 'Exp1': 'ℯ', 'ImaginaryUnit': 'ⅈ', 'Infinity': '∞', 'Integers': 'ℤ', 'Intersection': '∩', 'Naturals': 'ℕ', 'Naturals0': 'ℕ₀', 'NegativeInfinity': '-∞', 'Pi': 'π', 'Rationals': 'ℚ', 'Reals': 'ℝ', 'Ring': '∘', 'SymmetricDifference': '∆', 'Union': '∪'}
diofant.printing.pretty.pretty_symbology.pretty_atom(atom_name, default=None)[source]

return pretty representation of an atom

diofant.printing.pretty.pretty_symbology.pretty_symbol(symb_name)[source]

return pretty representation of a symbol

diofant.printing.pretty.pretty_symbology.annotated(letter)[source]

Return a stylised drawing of the letter letter, together with information on how to put annotations (super- and subscripts to the left and to the right) on it.

See pretty.py functions _print_meijerg, _print_hyper on how to use this information.

Prettyprinter by Jurjen Bos. (I hate spammers: mail me at pietjepuk314 at the reverse of ku.oc.oohay). All objects have a method that create a “stringPict”, that can be used in the str method for pretty printing.

Updates by Jason Gedge (email <my last name> at cs mun ca)
  • terminal_string() method
  • minor fixes and changes (mostly to prettyForm)
TODO:
  • Allow left/center/right alignment options for above/below and top/center/bottom alignment options for left/right
class diofant.printing.pretty.stringpict.stringPict(s, baseline=0)[source]

An ASCII picture. The pictures are represented as a list of equal length strings.

above(*args)[source]

Put pictures above this picture. Returns string, baseline arguments for stringPict. Baseline is baseline of bottom picture.

below(*args)[source]

Put pictures under this picture. Returns string, baseline arguments for stringPict. Baseline is baseline of top picture

Examples

>>> from diofant.printing.pretty.pretty_symbology import pretty_use_unicode
>>> f = pretty_use_unicode(flag=False)
>>> print(stringPict("x+3").below(
...       stringPict.LINE, '3')[0])  
x+3
---
 3
height()[source]

The height of the picture in characters.

left(*args)[source]

Put pictures (left to right) at left. Returns string, baseline arguments for stringPict.

static next(*args)[source]

Put a string of stringPicts next to each other. Returns string, baseline arguments for stringPict.

parens(left='(', right=')', ifascii_nougly=False)[source]

Put parentheses around self. Returns string, baseline arguments for stringPict.

left or right can be None or empty string which means ‘no paren from that side’

render(*args, **kwargs)[source]

Return the string form of self.

Unless the argument line_break is set to False, it will break the expression in a form that can be printed on the terminal without being broken up.

right(*args)[source]

Put pictures next to this one. Returns string, baseline arguments for stringPict. (Multiline) strings are allowed, and are given a baseline of 0.

Examples

>>> from diofant.printing.pretty.pretty_symbology import pretty_use_unicode
>>> f = pretty_use_unicode(flag=False)
>>> print(stringPict("10").right(" + ", stringPict("1\r-\r2", 1))[0])
     1
10 + -
     2
static stack(*args)[source]

Put pictures on top of each other, from top to bottom. Returns string, baseline arguments for stringPict. The baseline is the baseline of the second picture. Everything is centered. Baseline is the baseline of the second picture. Strings are allowed. The special value stringPict.LINE is a row of ‘-‘ extended to the width.

terminal_width()[source]

Return the terminal width if possible, otherwise return 0.

width()[source]

The width of the picture in characters.

class diofant.printing.pretty.stringpict.prettyForm(s, baseline=0, binding=0, unicode=None)[source]

Extension of the stringPict class that knows about basic math applications, optimizing double minus signs.

“Binding” is interpreted as follows:

ATOM this is an atom: never needs to be parenthesized
FUNC this is a function application: parenthesize if added (?)
DIV  this is a division: make wider division if divided
POW  this is a power: only parenthesize if exponent
MUL  this is a multiplication: parenthesize if powered
ADD  this is an addition: parenthesize if multiplied or powered
NEG  this is a negative number: optimize if added, parenthesize if
     multiplied or powered
OPEN this is an open object: parenthesize if added, multiplied, or
     powered (example: Piecewise)

dotprint

diofant.printing.dot.dotprint(expr, styles=[(<class 'diofant.core.basic.Basic'>, {'color': 'blue', 'shape': 'ellipse'}), (<class 'diofant.core.expr.Expr'>, {'color': 'black'})], atom=<function <lambda>>, maxdepth=None, repeat=True, labelfunc=<class 'str'>, **kwargs)[source]

DOT description of a Diofant expression tree

Options are

styles: Styles for different classes. The default is:

[(Basic, {'color': 'blue', 'shape': 'ellipse'}),
(Expr, {'color': 'black'})]``
atom: Function used to determine if an arg is an atom. The default is
lambda x: not isinstance(x, Basic). Another good choice is lambda x: not x.args.

maxdepth: The maximum depth. The default is None, meaning no limit.

repeat: Whether to different nodes for separate common subexpressions.
The default is True. For example, for x + x*y with repeat=True, it will have two nodes for x and with repeat=False, it will have one (warning: even if it appears twice in the same object, like Pow(x, x), it will still only appear only once. Hence, with repeat=False, the number of arrows out of an object might not equal the number of args it has).
labelfunc: How to label leaf nodes. The default is str. Another
good option is repr. For example with str, the leaf nodes of x + 1 are labeled, x and 1. With repr, they are labeled Symbol('x') and Integer(1).

Additional keyword arguments are included as styles for the graph.

Examples

>>> print(dotprint(x + 2)) 
digraph{

# Graph style
"bgcolor"="transparent"
"ordering"="out"
"rankdir"="TD"

#########
# Nodes #
#########

"Add(Symbol('x'), Integer(2))_()" ["color"="black", "label"="Add", "shape"="ellipse"];
"Integer(2)_(0,)" ["color"="black", "label"="2", "shape"="ellipse"];
"Symbol('x')_(1,)" ["color"="black", "label"="x", "shape"="ellipse"];

#########
# Edges #
#########

"Add(Symbol('x'), Integer(2))_()" -> "Integer(2)_(0,)";
"Add(Symbol('x'), Integer(2))_()" -> "Symbol('x')_(1,)";
}