Primitives (Basic Objects)¶
Expression base class¶
- class pymbolic.primitives.Expression[source]¶
Superclass for parts of a mathematical expression. Overrides operators to implicitly construct
Sum
,Product
and other expressions.Expression objects are immutable.
Changed in version 2022.2: PEP 634-style pattern matching is now supported when Pymbolic is used under Python 3.10.
- a¶
- mapper_method¶
The
pymbolic.mapper.Mapper
method called for objects of this type.
- make_stringifier(originating_stringifier=None)[source]¶
Return a
pymbolic.mapper.Mapper
instance that can be used to generate a human-readable representation of self. Usually a subclass ofpymbolic.mapper.stringifier.StringifyMapper
.- Parameters:
originating_stringifier – If provided, the newly created stringifier should carry forward attributes and settings of originating_stringifier.
- __eq__(other)[source]¶
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__()[source]¶
Provides caching for hash values.
Subclasses should generally not override this method, but instead provide an implementation of
get_hash()
.
- __str__()[source]¶
Use the
make_stringifier()
to return a human-readable string representation of self.
- __repr__()[source]¶
Provides a default
repr()
based on the Python pickling interface__getinitargs__()
.
Logical operator constructors
- not_()[source]¶
Return self wrapped in a
LogicalNot
.Added in version 2015.2.
- and_(other)[source]¶
Return
LogicalAnd
between self and other.Added in version 2015.2.
Comparison constructors
- eq(other)[source]¶
Return a
Comparison
comparing self to other.Added in version 2015.2.
- ne(other)[source]¶
Return a
Comparison
comparing self to other.Added in version 2015.2.
- lt(other)[source]¶
Return a
Comparison
comparing self to other.Added in version 2015.2.
- le(other)[source]¶
Return a
Comparison
comparing self to other.Added in version 2015.2.
- gt(other)[source]¶
Return a
Comparison
comparing self to other.Added in version 2015.2.
- ge(other)[source]¶
Return a
Comparison
comparing self to other.Added in version 2015.2.
Sums, products and such¶
- class pymbolic.primitives.Call(function, parameters)[source]¶
A function invocation.
- function¶
A
Expression
that evaluates to a function.
- parameters¶
A
tuple
of positional parameters, each element of which is aExpression
or a constant.
- mapper_method = 'map_call'¶
- class pymbolic.primitives.CallWithKwargs(function, parameters, kw_parameters)[source]¶
A function invocation with keyword arguments.
- function¶
A
Expression
that evaluates to a function.
- parameters¶
A
tuple
of positional parameters, each element of which is aExpression
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 thedict
constructor.
- mapper_method = 'map_call_with_kwargs'¶
- class pymbolic.primitives.Subscript(aggregate, index)[source]¶
An array subscript.
- aggregate¶
- mapper_method = 'map_subscript'¶
- class pymbolic.primitives.Lookup(aggregate, name)[source]¶
Access to an attribute of an aggregate, such as an attribute of a class.
- mapper_method = 'map_lookup'¶
- class pymbolic.primitives.Quotient(numerator, denominator=1)[source]¶
- numerator¶
- denominator¶
- mapper_method = 'map_quotient'¶
- class pymbolic.primitives.FloorDiv(numerator, denominator=1)[source]¶
- numerator¶
- denominator¶
- mapper_method = 'map_floor_div'¶
Shift operators¶
Bitwise operators¶
Comparisons and logic¶
- class pymbolic.primitives.Comparison(left, operator, right)[source]¶
- left¶
- operator¶
One of
[">", ">=", "==", "!=", "<", "<="]
.
- right¶
Note
Unlike other expressions, comparisons are not implicitly constructed by comparing
Expression
objects. SeeExpression.eq()
.- mapper_method = 'map_comparison'¶
Code generation helpers¶
- class pymbolic.primitives.CommonSubexpression(child, prefix=None, scope=None)[source]¶
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¶
See
pymbolic.mapper.c_code.CCodeMapper
for an example.- mapper_method = 'map_common_subexpression'¶
- class pymbolic.primitives.cse_scope[source]¶
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)[source]¶
Wrap field in a
CommonSubexpression
with prefix. If field is anumpy
object array, each individual entry is instead wrapped. If field is apymbolic.geometric_algebra.MultiVector
, each coefficient is individually wrapped.See
CommonSubexpression
for the meaning of prefix and scope.
Helper functions¶
- pymbolic.primitives.flattened_sum(terms)[source]¶
Recursively flattens all the top level
Sum
s in terms.
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, var_factory=<class 'pymbolic.primitives.Variable'>)[source]¶
Return an object array of components subscripted
Variable
(or subclass) instances.- Parameters:
components – Either a list of indices, or an integer representing the number of indices.
var_factory – The
Variable
subclass to use for instantiating the scalar variables.
For example, this creates a vector with three components:
>>> make_sym_vector("vec", 3) array([Subscript(Variable('vec'), 0), Subscript(Variable('vec'), 1), Subscript(Variable('vec'), 2)], dtype=object)
Constants¶
- class pymbolic.primitives.NaN(data_type=None)[source]¶
An expression node representing not-a-number as a floating point number. Unlike,
math.nan
, all instances ofNaN
compare equal, as one might reasonably expect for program representation. (If this weren’t so, programs containing NaNs would effectively be unhashable, because they don’t compare equal to themselves.)Note that, in Python, this equality comparison is made even more complex by this issue, due to which
np.nan == np.nan
is False, but(np.nan,) == (np.nan,)
is True.- data_type¶
The data type used for the actual realization of the constant. Defaults to None. If given, This must be a callable to which a NaN
float
can be passed to obtain a NaN of the yield the desired type. It must also be suitable for use as the second argument ofisinstance()
.
- mapper_method = 'map_nan'¶