Mappers¶
Basic dispatch¶
- class pymbolic.mapper.Mapper[source]¶
A visitor for trees of
pymbolic.Expression
subclasses. Each expression-derived object is dispatched to the method named by thepymbolic.Expression.mapper_method
attribute and if not found, the methods named by the class attribute mapper_method in the method resolution order of the object.- __call__(expr, *args, **kwargs)[source]¶
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)[source]¶
Identical to
__call__()
, but intended for use in recursive dispatch in mapper methods.
- handle_unsupported_expression(expr, *args, **kwargs)[source]¶
Mapper method that is invoked for
pymbolic.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.Expression
.These are abstract methods for foreign objects that should be overridden in subclasses:
- map_constant(expr, *args, **kwargs)[source]¶
Mapper method for constants. See
pymbolic.primitives.register_constant_class()
.
Base classes for new mappers¶
- class pymbolic.mapper.CombineMapper[source]¶
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)[source]¶
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[source]¶
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.
Added in version 2014.3.
- class pymbolic.mapper.IdentityMapper[source]¶
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[source]¶
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 children.
- class pymbolic.mapper.CSECachingMapperMixin[source]¶
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:This method deliberately does not support extra arguments in mapper dispatch, to avoid spurious dependencies of the cache on these arguments.
Base classes for mappers with memoization support¶
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_IF¶
- pymbolic.mapper.stringifier.PREC_NONE¶
Mappers¶
- class pymbolic.mapper.stringifier.StringifyMapper[source]¶
A mapper to turn an expression tree into a string.
pymbolic.Expression.__str__
is often implemented using this mapper.When it encounters an unsupported
pymbolic.Expression
subclass, it calls itspymbolic.Expression.make_stringifier()
method to get aStringifyMapper
that potentially does.- __call__(expr, prec=0, *args, **kwargs)[source]¶
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[source]¶
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.c_code.CCodeMapper(reverse=True, cse_prefix='_cse', complex_constant_base_type='double', cse_name_list=None)[source]¶
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=None)[source]¶
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
- pymbolic.mapper.evaluator.evaluate(expression, context=None, mapper_cls=<class 'pymbolic.mapper.evaluator.CachedEvaluationMapper'>) Any [source]¶
- Parameters:
mapper_cls – A
type
of the evaluation mapper whose instance performs the evaluation.
- pymbolic.mapper.evaluator.evaluate_kw(expression, mapper_cls=<class 'pymbolic.mapper.evaluator.CachedEvaluationMapper'>, **context) Any [source]¶
- Parameters:
mapper_cls – A
type
of the evaluation mapper whose instance performs the evaluation.
- pymbolic.mapper.evaluator.evaluate_to_float(expression, context=None, mapper_cls=<class 'pymbolic.mapper.evaluator.CachedFloatEvaluationMapper'>) Any [source]¶
- Parameters:
mapper_cls – A
type
of the evaluation mapper whose instance performs the evaluation.
- class pymbolic.mapper.differentiator.DifferentiationMapper(variable, func_map=<function map_math_functions_by_name>, allowed_nonsmoothness=None)[source]¶
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)[source]¶
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=None)[source]¶
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.
Finding expression properties¶
- class pymbolic.mapper.dependency.DependencyMapper(include_subscripts=True, include_lookups=True, include_calls=True, include_cses=False, composite_leaves=None)[source]¶
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.
Analysis tools¶
- class pymbolic.mapper.analysis.NodeCountMapper[source]¶
Counts the number of nodes in an expression tree. Nodes that occur repeatedly as well as
CommonSubexpression
nodes are only counted once.- count¶
The number of nodes.
- pymbolic.mapper.analysis.get_num_nodes(expr) int [source]¶
Returns the number of nodes in expr. Nodes that occur repeatedly as well as
CommonSubexpression
nodes are only counted once.