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#
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 viapymbolic.primitives
instances.
Internal API#
- class pymbolic.interop.matchpy.PymbolicOp(*operands: Expression, variable_name=None)[source]#
A base class for all pymbolic-like operations.
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.