pyccel.codegen.printing package

Submodules

pyccel.codegen.printing.ccode module

class pyccel.codegen.printing.ccode.CCodePrinter(settings={})[source]

Bases: pyccel.codegen.printing.codeprinter.CodePrinter

A printer to convert python expressions to strings of c code

indent_code(code)[source]

Accepts a string of code or a list of code lines

language = 'C'
printmethod = '_ccode'
pyccel.codegen.printing.ccode.ccode(expr, assign_to=None, **settings)[source]

Converts an expr to a string of c code

expr : Expr
A sympy 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.
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.

pyccel.codegen.printing.codeprinter module

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

Bases: sympy.printing.str.StrPrinter

The base class for code-printing subclasses.

doprint(expr, assign_to=None)[source]

Print the expression as code.

expr : Expression
The expression to be printed.
assign_to : Symbol, MatrixSymbol, or string (optional)
If provided, the printed code will set the expression to a variable with name assign_to.

pyccel.codegen.printing.fcode module

class pyccel.codegen.printing.fcode.FCodePrinter(settings={})[source]

Bases: pyccel.codegen.printing.codeprinter.CodePrinter

A printer to convert sympy expressions to strings of Fortran code

indent_code(code)[source]

Accepts a string of code or a list of code lines

language = 'Fortran'
printmethod = '_fcode'
pyccel.codegen.printing.fcode.fcode(expr, assign_to=None, **settings)[source]

Converts an expr to a string of c code

expr : Expr
A sympy 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.

pyccel.codegen.printing.luacode module

A complete code generator, which uses lua_code extensively, can be found in sympy.utilities.codegen. The codegen module can be used to generate complete source code files.

class pyccel.codegen.printing.luacode.LuaCodePrinter(settings={})[source]

Bases: pyccel.codegen.printing.codeprinter.CodePrinter

A printer to convert python expressions to strings of Lua code

indent_code(code)[source]

Accepts a string of code or a list of code lines

language = 'Lua'
printmethod = '_lua_code'
pyccel.codegen.printing.luacode.lua_code(expr, assign_to=None, **settings)[source]

Converts an expr to a string of Lua code

expr : Expr
A sympy 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)]. 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].
locals: dict
A dictionary that contains the list of local symbols. these symbols will be preceeded by local for their first assignment.

Examples

>>> from sympy import lua_code, symbols, Rational, sin, ceiling, Abs, Function
>>> x, tau = symbols("x, tau")
>>> lua_code((2*tau)**Rational(7, 2))
'8*1.4142135623731*tau.powf(7_f64/2.0)'
>>> lua_code(sin(x), assign_to="s")
's = x.sin();'

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", 4),
...           (lambda x: x.is_integer, "ABS", 4)],
...   "func": "f"
... }
>>> func = Function('func')
>>> lua_code(func(Abs(x) + ceiling(x)), user_functions=custom_functions)
'(fabs(x) + x.CEIL()).f()'

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.

>>> from sympy import Piecewise
>>> expr = Piecewise((x + 1, x > 0), (x, True))
>>> print(lua_code(expr, tau))
tau = if (x > 0) {
    x + 1
} else {
    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:

>>> from sympy import Eq, IndexedBase, Idx
>>> 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]))
>>> lua_code(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:

>>> from sympy import Matrix, MatrixSymbol
>>> mat = Matrix([x**2, Piecewise((x + 1, x > 0), (x, True)), sin(x)])
>>> A = MatrixSymbol('A', 3, 1)
>>> print(lua_code(mat, A))
A = [x.powi(2), if (x > 0) {
    x + 1
} else {
    x
}, x.sin()];

Module contents