Primitives (Basic Objects)

pymbolic.primitives.disable_subscript_by_getitem()

In prior versions of pymbolic, directly subscripting an Expression subclass generated a Subscript. For various reasons, this was a very bad idea. For example, the following code snippet would result in an infinite loop:

for el in expr:
    print(el)

numpy does similar things under the hodd, leading to hard-to-debug infinite loops. As a result, this behavior is being deprecated. In Pymbolic 2016.x, it will disappear entirely. It can also be disabled by this function. Once disabled, it cannot be reenabled.

See also Expression.index().

New in version 2014.3.

Expression base class

class pymbolic.primitives.Expression

Superclass for parts of a mathematical expression. Overrides operators to implicitly construct Sum, Product and other expressions.

Expression objects are immutable.

attr
mapper_method

The pymbolic.mapper.Mapper method called for objects of this type.

__getitem__()

Deprecated, see disable_subscript_by_getitem(). Use index() instead.

index(subscript)

Return an expression representing self[subscript].

New in version 2014.3.

stringifier()

Return a pymbolic.mapper.Mapper class used to yield a human-readable representation of self. Usually a subclass of pymbolic.mapper.stringifier.StringifyMapper.

__eq__(other)

Provides equality testing with quick positive and negative paths based on id() and __hash__().

Subclasses should generally not override this method, but instead provide an implementation of is_equal().

__hash__()

Provides caching for hash values.

Subclasses should generally not override this method, but instead provide an implementation of get_hash().

__str__()

Use the stringifier() to return a human-readable string representation of self.

__repr__()

Provides a default repr() based on the Python pickling interface __getinitargs__().

Sums, products and such

class pymbolic.primitives.Variable(name)
name
mapper_method = 'map_variable'
class pymbolic.primitives.Call(function, parameters)

A function invocation.

function

A Expression that evaluates to a function.

parameters

A tuple of positional paramters, each element of which is a Expression or a constant.

mapper_method = 'map_call'
class pymbolic.primitives.CallWithKwargs(function, parameters, kw_parameters)

A function invocation with keyword arguments.

function

A Expression that evaluates to a function.

parameters

A tuple of positional paramters, each element of which is a Expression or a constant.

kw_parameters

A dictionary mapping names to arguments, , each of which is a Expression or a constant, or an equivalent value accepted by the dict constructor.

mapper_method = 'map_call_with_kwargs'
class pymbolic.primitives.Subscript(aggregate, index)

An array subscript.

aggregate
index
mapper_method = 'map_subscript'
class pymbolic.primitives.Lookup(aggregate, name)

Access to an attribute of an aggregate, such as an attribute of a class.

mapper_method = 'map_lookup'
class pymbolic.primitives.Sum(children)
children

A tuple.

mapper_method = 'map_sum'
class pymbolic.primitives.Product(children)
children

A tuple.

mapper_method = 'map_product'
class pymbolic.primitives.Quotient(numerator, denominator=1)
numerator
denominator
mapper_method = 'map_quotient'
class pymbolic.primitives.FloorDiv(numerator, denominator=1)
numerator
denominator
mapper_method = 'map_floor_div'
class pymbolic.primitives.Remainder(numerator, denominator=1)
numerator
denominator
mapper_method = 'map_remainder'
class pymbolic.primitives.Power(base, exponent)
base
exponent
mapper_method = 'map_power'

Shift operators

class pymbolic.primitives.LeftShift
shiftee
shift
mapper_method = 'map_left_shift'
class pymbolic.primitives.RightShift
shiftee
shift
mapper_method = 'map_right_shift'

Bitwise operators

class pymbolic.primitives.BitwiseNot(child)
child
mapper_method = 'map_bitwise_not'
class pymbolic.primitives.BitwiseOr(children)
children

A tuple.

mapper_method = 'map_bitwise_or'
class pymbolic.primitives.BitwiseXor(children)
children

A tuple.

mapper_method = 'map_bitwise_xor'
class pymbolic.primitives.BitwiseAnd(children)
children

A tuple.

mapper_method = 'map_bitwise_and'

Comparisons and logic

class pymbolic.primitives.Comparison(left, operator, right)
left
operator
right

Note

Unlike other expressions, comparisons are not implicitly constructed by comparing Expression objects.

Parameters:operator – One of [">", ">=", "==", "!=", "<", "<="].
mapper_method = 'map_comparison'
class pymbolic.primitives.LogicalNot(child)
child
mapper_method = 'map_logical_not'
class pymbolic.primitives.LogicalAnd(children)
children

A tuple.

mapper_method = 'map_logical_and'
class pymbolic.primitives.LogicalOr(children)
children

A tuple.

mapper_method = 'map_logical_or'
class pymbolic.primitives.If(condition, then, else_)
condition
then
else_
mapper_method = 'map_if'

Code generation helpers

class pymbolic.primitives.CommonSubexpression(child, prefix=None, scope=None)

A helper for code generation and caching. Denotes a subexpression that should only be evaluated once. If, in code generation, it is assigned to a variable, a name starting with prefix should be used.

child
prefix
scope

One of the values in cse_scope. See there for meaning.

See pymbolic.mapper.c_code.CCodeMapper for an example.

Parameters:scope – Defaults to cse_scope.EVALUATION if given as None.
mapper_method = 'map_common_subexpression'
class pymbolic.primitives.cse_scope

Determines the lifetime for the saved value of a CommonSubexpression.

EVALUATION

The evaluated result lives for the duration of the evaluation of the current expression and is discarded thereafter.

EXPRESSION

The evaluated result lives for the lifetime of the current expression (across multiple evaluations with multiple parameters) and is discarded when the expression is.

GLOBAL

The evaluated result lives until the execution context dies.

pymbolic.primitives.make_common_subexpression(field, prefix=None, scope=None)

Wrap field in a CommonSubexpression with prefix. If field is a numpy object array, each individual entry is instead wrapped. If field is a pymbolic.geometric_algebra.MultiVector, each coefficient is individually wrapped.

See CommonSubexpression for the meaning of prefix and scope.

Helper functions

pymbolic.primitives.is_zero(value)
pymbolic.primitives.is_constant(value)
pymbolic.primitives.register_constant_class(class_)
pymbolic.primitives.unregister_constant_class(class_)
pymbolic.primitives.variables(s)

Return a list of variables for each (space-delimited) identifier in s.

Interaction with numpy arrays

numpy.ndarray instances are supported anywhere in an expression. In particular, numpy object arrays are useful for capturing vectors and matrices of pymbolic objects.

pymbolic.primitives.make_sym_vector(name, components)

Return an object array of components subscripted Variable instances.

Parameters:components – The number of components in the vector.