pymbolic.mapper.
Mapper
¶A visitor for trees of pymbolic.primitives.Expression
subclasses. Each expression-derived object is dispatched to the
method named by the pymbolic.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)¶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)¶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 call combine()
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 from
CombineMapper
.) The
pymbolic.mapper.dependency.DependencyMapper
is another example.
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.
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.
pymbolic.mapper.
WalkMapper
¶A mapper whose default mapper method implementations simply recurse
without propagating any result. Also calls visit()
for each
visited subexpression.
visit
(expr, *args)¶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.
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
¶pymbolic.mapper.stringifier.
StringifyMapper
(constant_mapper=<type 'str'>)¶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 its pymbolic.primitives.Expression.stringifier()
method to get a StringifyMapper
that potentially does.
Parameters: | constant_mapper – A function of a single expr argument being used to map constants into strings. |
---|
__call__
(expr, prec=0)¶Return a string corresponding to expr. If the enclosing precedence level prec is higher than prec (see Precedence constants), parenthesize the result.
pymbolic.mapper.stringifier.
CSESplittingStringifyMapperMixin
¶A mix-in for subclasses of
StringifyMapper
that collects
“variable assignments” for
pymbolic.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.
pymbolic.mapper.c_code.
CCodeMapper
(constant_mapper=<built-in function repr>, 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 the cse_*
attributes.
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
Parameters: | context – a mapping from variable names to values |
---|
pymbolic.mapper.differentiator.
DifferentiationMapper
(variable, func_map=<function map_math_functions_by_name>)¶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
Parameters: |
|
---|
pymbolic.mapper.distributor.
DistributeMapper
(collector=<pymbolic.mapper.collector.TermCollector object>)¶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
pymbolic.mapper.collector.
TermCollector
(parameters=set([]))¶A term collector that assumes that multiplication is commutative.
Allows specifying parameters (a set of
pymbolic.primitive.Variable
instances) that are viewed as being
coefficients and are not used for term collection.
pymbolic.mapper.constant_folder.
ConstantFoldingMapper
¶pymbolic.mapper.constant_folder.
CommutativeConstantFoldingMapper
¶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. The include_*
arguments to the constructor
determine which types of objects occur in this output set.
If all are False, only pymbolic.primitives.Variable
instances are included.
Parameters: | composite_leaves – Setting this is equivalent to setting
all preceding include_* flags. |
---|
pymbolic.mapper.flop_counter.
FlopCounter
¶