Utilities for dealing with expressions

Parser

pymbolic.parse(expr_str)

Return a pymbolic.primitives.ExpressionNode tree corresponding to expr_str.

The parser is also relatively easy to extend. See the source code of the following class.

class pymbolic.parser.Parser[source]

Compiler

class pymbolic.compiler.CompiledExpression(expression, variables=None)[source]

This class encapsulates an expression compiled into Python bytecode for faster evaluation.

Its instances (unlike plain lambdas) are pickleable.

__call__(*args)[source]

Interoperability with other symbolic systems

Interoperability with sympy

class pymbolic.interop.sympy.SympyToPymbolicMapper[source]
__call__(expr)[source]
class pymbolic.interop.sympy.PymbolicToSympyMapper[source]
__call__(expr)[source]

Interoperability with Maxima

class pymbolic.interop.maxima.MaximaStringifyMapper[source]
class pymbolic.interop.maxima.MaximaParser[source]
class pymbolic.interop.maxima.MaximaKernel(executable='maxima', timeout=30)[source]
restart()[source]
shutdown()[source]
reset()[source]
exec_str(s, enforce_prompt_numbering=True)[source]
eval_str(s, enforce_prompt_numbering=True)[source]
eval_expr(expr)[source]
pymbolic.interop.maxima.eval_expr_with_setup(assignments, expr)[source]
pymbolic.interop.maxima.diff(expr, var, count=1, assignments=())[source]

Interoperability with Python’s ast module

An example:

src = """
def f():
    xx = 3*y + z * (12 if x < 13 else 13)
    yy = f(x, y=y)
"""

import ast
mod = ast.parse(src.replace("\n    ", "\n"))

print(ast.dump(mod))

from pymbolic.interop.ast import ASTToPymbolic
ast2p = ASTToPymbolic()

for f in mod.body:
    if not isinstance(f, ast.FunctionDef):
        continue

    for stmt in f.body:
        if not isinstance(stmt, ast.Assign):
            continue

        lhs, = stmt.targets
        lhs = ast2p(lhs)
        rhs = ast2p(stmt.value)

        print(lhs, rhs)
class pymbolic.interop.ast.ASTToPymbolic[source]

Interoperability with matchpy.functions module

Visualizing Expressions

pymbolic.imperative.utils.get_dot_dependency_graph(statements, use_stmt_ids=None, preamble_hook=<function _default_preamble_hook>, additional_lines_hook=<class 'list'>, statement_stringifier=None, use_insn_ids=None)[source]

Return a string in the dot language depicting dependencies among kernel statements.

Parameters:
  • statements – A sequence of statements, each of which is stringified by calling statement_stringifier.

  • statement_stringifier – The function to use for stringifying the statements. The default stringifier uses str and escapes all double quotes (") in the string representation.

  • preamble_hook – A function that returns an iterable of lines to add at the beginning of the graph

  • additional_lines_hook – A function that returns an iterable of lines to add at the end of the graph