Utilities for dealing with expressions#

Parser#

pymbolic.parse(expr_str)#

Return a pymbolic.primitives.Expression 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#

Interoperability with matchpy.functions for pattern-matching and term-rewriting.

pymbolic.interop.matchpy.match(subject: Expression, pattern: Expression, to_matchpy_expr: Optional[Callable[[Expression], Expression]] = None, from_matchpy_expr: Optional[Callable[[Expression], Expression]] = None) Iterator[Mapping[str, Union[Expression, Number, int, bool_, bool]]][source]#
pymbolic.interop.matchpy.match_anywhere(subject: Expression, pattern: Expression, to_matchpy_expr: Optional[Callable[[Expression], Expression]] = None, from_matchpy_expr: Optional[Callable[[Expression], Expression]] = None) Iterator[Tuple[Mapping[str, Union[Expression, Number, int, bool_, bool]], Union[Expression, Number, int, bool_, bool]]][source]#
pymbolic.interop.matchpy.replace_all(expression: Expression, rules: Iterable[ReplacementRule], to_matchpy_expr: Optional[Callable[[Expression], Expression]] = None, from_matchpy_expr: Optional[Callable[[Expression], Expression]] = None) Union[Expression, Tuple[Expression, ...]][source]#
pymbolic.interop.matchpy.make_replacement_rule(pattern: Expression, replacement: Callable[[...], Expression], to_matchpy_expr: Optional[Callable[[Expression], Expression]] = None, from_matchpy_expr: Optional[Callable[[Expression], Expression]] = None) ReplacementRule[source]#

Returns a matchpy.functions.ReplacementRule from the objects declared via pymbolic.primitives instances.

Internal API#

class pymbolic.interop.matchpy.PymbolicOp(*operands: Expression, variable_name=None)[source]#

A base class for all pymbolic-like operations.

class pymbolic.interop.matchpy.Wildcard(min_count: int, fixed_size: bool, variable_name=None, optional=None)[source]#

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

pymbolic.imperative.utils.show_dot(dot_code, output_to=None)[source]#

Visualize the graph represented by dot_code. Can be called on the result of get_dot_dependency_graph().

Arg:

output_to

An instance of str that can be one of:

  • "xwindow" to visualize the graph as an X window.

  • "browser" to visualize the graph as an SVG file in the system’s default web-browser.

  • "svg" to store the dot code as an SVG file on the file system. Returns the path to the generated svg file.

Defaults to "xwindow" if X11 support is present, otherwise defaults to "browser".

Returns:

Depends on output_to.