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.
Compiler¶
Interoperability with other symbolic systems¶
Interoperability with sympy
¶
Interoperability with Maxima¶
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)
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