Mappers¶
Basic dispatch¶
-
class
pymbolic.mapper.
Mapper
¶ A visitor for trees of
pymbolic.primitives.Expression
subclasses. Each expression-derived object is dispatched to the method named by thepymbolic.primitives.Expression.mapper_method
attribute.-
__call__
(expr, *args, **kwargs)¶ Dispatch expr to its corresponding mapper method. Pass on
*args
and**kwargs
unmodified.This method is intended as the top-level dispatch entry point and may be overridden by subclasses to present a different/more convenient interface.
rec()
on the other hand is intended as the recursive dispatch method to be used to recurse within mapper method implementations.
-
rec
(expr, *args, **kwargs)¶ Identical to
__call__()
, but intended for use in recursive dispatch in mapper methods.
-
handle_unsupported_expression
(expr, *args, **kwargs)¶ Mapper method that is invoked for
pymbolic.primitives.Expression
subclasses for which a mapper method does not exist in this mapper.
Handling objects that don’t declare mapper methods
In particular, this includes many non-subclasses of
pymbolic.primitives.Expression
.These are abstract methods for foreign objects that should be overridden in subclasses:
-
map_constant
(expr, *args, **kwargs)¶ Mapper method for constants. See
pymbolic.primitives.register_constant_class()
.
-
map_list
(expr, *args, **kwargs)¶
-
map_tuple
(expr, *args, **kwargs)¶
-
map_numpy_array
(expr, *args, **kwargs)¶
-
Base classes for new mappers¶
-
class
pymbolic.mapper.
CombineMapper
¶ A mapper whose goal it is to combine all branches of the expression tree into one final result. The default implementation of all mapper methods simply recurse (
Mapper.rec()
) on all branches emanating from the current expression, and then callcombine()
on a tuple of results.-
combine
(values)¶ Combine the mapped results of multiple expressions (given in values) into a single result, often by summing or taking set unions.
The
pymbolic.mapper.flop_counter.FlopCounter
is a very simple example. (Look at its source for an idea of how to derive fromCombineMapper
.) Thepymbolic.mapper.dependency.DependencyMapper
is another example.-
-
class
pymbolic.mapper.
Collector
¶ A subclass of
CombineMapper
for the common purpose of collecting data derived from an expression in a set that gets ‘unioned’ across children at each non-leaf node in the expression tree.By default, nothing is collected. All leaves return empty sets.
New in version 2014.3.
-
class
pymbolic.mapper.
IdentityMapper
¶ A
Mapper
whose default mapper methods make a deep copy of each subexpression.See Manipulating expressions for an example of the manipulations that can be implemented this way.
-
class
pymbolic.mapper.
WalkMapper
¶ A mapper whose default mapper method implementations simply recurse without propagating any result. Also calls
visit()
for each visited subexpression.map_...
methods are required to callvisit()
beforedescending to visit their chidlren.
-
visit
(expr, *args, **kwargs)¶ Returns False if no children of this node should be examined.
-
post_visit
(expr, *args, **kwargs)¶ Is called after a node’s children are visited.
-
class
pymbolic.mapper.
CSECachingMapperMixin
¶ A mix-in that helps subclassed mappers implement caching for
pymbolic.primitives.CommonSubexpression
instances.Instead of the common mapper method for
pymbolic.primitives.CommonSubexpression
, subclasses should implement the following method:-
map_common_subexpression_uncached
(expr)¶
This method deliberately does not support extra arguments in mapper dispatch, to avoid spurious dependencies of the cache on these arguments.
-
More specialized mappers¶
Converting to strings and code¶
Precedence constants¶
-
pymbolic.mapper.stringifier.
PREC_CALL
¶
-
pymbolic.mapper.stringifier.
PREC_POWER
¶
-
pymbolic.mapper.stringifier.
PREC_UNARY
¶
-
pymbolic.mapper.stringifier.
PREC_PRODUCT
¶
-
pymbolic.mapper.stringifier.
PREC_SUM
¶
-
pymbolic.mapper.stringifier.
PREC_SHIFT
¶
-
pymbolic.mapper.stringifier.
PREC_BITWISE_AND
¶
-
pymbolic.mapper.stringifier.
PREC_BITWISE_XOR
¶
-
pymbolic.mapper.stringifier.
PREC_BITWISE_OR
¶
-
pymbolic.mapper.stringifier.
PREC_COMPARISON
¶
-
pymbolic.mapper.stringifier.
PREC_LOGICAL_AND
¶
-
pymbolic.mapper.stringifier.
PREC_LOGICAL_OR
¶
-
pymbolic.mapper.stringifier.
PREC_NONE
¶
Mappers¶
-
class
pymbolic.mapper.stringifier.
StringifyMapper
¶ A mapper to turn an expression tree into a string.
pymbolic.primitives.Expression.__str__
is often implemented using this mapper.When it encounters an unsupported
pymbolic.primitives.Expression
subclass, it calls itspymbolic.primitives.Expression.make_stringifier()
method to get aStringifyMapper
that potentially does.-
__call__
(expr, prec=0, *args, **kwargs)¶ Return a string corresponding to expr. If the enclosing precedence level prec is higher than prec (see Precedence constants), parenthesize the result.
-
-
class
pymbolic.mapper.stringifier.
CSESplittingStringifyMapperMixin
¶ A mix-in for subclasses of
StringifyMapper
that collects “variable assignments” forpymbolic.primitives.CommonSubexpression
objects.-
cse_name_list
¶ A
list
of tuples of names and their string representations, in order of their dependencies. When generating code, walk down these names in order, and the generated code will never reference an undefined variable.
See
pymbolic.mapper.c_code.CCodeMapper
for an example of the use of this mix-in.-
-
class
pymbolic.mapper.stringifier.
LaTeXMapper
¶
-
class
pymbolic.mapper.c_code.
CCodeMapper
(reverse=True, cse_prefix='_cse', complex_constant_base_type='double', cse_name_list=[])¶ Generate C code for expressions, while extracting
pymbolic.primitives.CommonSubexpression
instances.As an example, define a fairly simple expression expr:
>>> import pymbolic.primitives as p >>> x = p.Variable("x") >>> CSE = p.CommonSubexpression >>> u = CSE(3*x**2-5, "u") >>> expr = u/(u+3)*(u+5) >>> print(expr) (CSE(3*x**2 + -5) / (CSE(3*x**2 + -5) + 3))*(CSE(3*x**2 + -5) + 5)
Notice that if we were to directly generate code from this, the subexpression u would be evaluated multiple times.
>>> from pymbolic.mapper.c_code import CCodeMapper as CCM >>> ccm = CCM() >>> result = ccm(expr) >>> for name, value in ccm.cse_name_list: ... print("%s = %s;" % (name, value)) ... _cse_u = 3 * x * x + -5; >>> print(result) _cse_u / (_cse_u + 3) * (_cse_u + 5)
See
pymbolic.mapper.stringifier.CSESplittingStringifyMapperMixin
for thecse_*
attributes.
Some minimal mathematics¶
-
class
pymbolic.mapper.evaluator.
EvaluationMapper
(context={})¶ Example usage:
>>> import pymbolic.primitives as p >>> x = p.Variable("x") >>> u = 5*x**2 - 3*x >>> print(u) 5*x**2 + (-1)*3*x >>> from pymbolic.mapper.evaluator import EvaluationMapper as EM >>> EM(context={"x": 5})(u) 110
-
class
pymbolic.mapper.differentiator.
DifferentiationMapper
(variable, func_map=<function map_math_functions_by_name>, allowed_nonsmoothness=None)¶ Example usage:
>>> import pymbolic.primitives as p >>> x = p.Variable("x") >>> expr = x*(x+5)**3/(x-1)**2 >>> from pymbolic.mapper.differentiator import DifferentiationMapper as DM >>> print(DM(x)(expr)) (((x + 5)**3 + x*3*(x + 5)**2)*(x + -1)**2 + (-1)*2*(x + -1)*x*(x + 5)**3) / (x + -1)**2**2
-
class
pymbolic.mapper.distributor.
DistributeMapper
(collector=None, const_folder=None)¶ Example usage:
>>> import pymbolic.primitives as p >>> x = p.Variable("x") >>> expr = (x+1)**7 >>> from pymbolic.mapper.distributor import DistributeMapper as DM >>> print DM()(expr) 7*x**6 + 21*x**5 + 21*x**2 + 35*x**3 + 1 + 35*x**4 + 7*x + x**7
-
class
pymbolic.mapper.collector.
TermCollector
(parameters={})¶ A term collector that assumes that multiplication is commutative.
Allows specifying parameters (a set of
pymbolic.primitives.Variable
instances) that are viewed as being coefficients and are not used for term collection.
-
class
pymbolic.mapper.constant_folder.
ConstantFoldingMapper
¶
-
class
pymbolic.mapper.constant_folder.
CommutativeConstantFoldingMapper
¶
Finding expression properties¶
-
class
pymbolic.mapper.dependency.
DependencyMapper
(include_subscripts=True, include_lookups=True, include_calls=True, include_cses=False, composite_leaves=None)¶ Maps an expression to the
set
of expressions it is based on. Theinclude_*
arguments to the constructor determine which types of objects occur in this output set. If all are False, onlypymbolic.primitives.Variable
instances are included.
-
class
pymbolic.mapper.flop_counter.
FlopCounter
¶
-
class
pymbolic.mapper.flop_counter.
CSEAwareFlopCounter
¶ A flop counter that only counts the contribution from common subexpressions once.
Warning
You must use a fresh mapper for each new evaluation operation for which reuse may take place.