Primitives (Basic Objects)¶
Typing helpers¶
- pymbolic.RealNumber¶
Supported real number types (integer and floating point).
Mainly distinguished from
Numberby having a total ordering, i.e. not including the complex numbers.
alias ofint|integer|float|floating
- pymbolic.Scalar¶
Supported scalar types.
alias ofint|integer|float|complex|inexact|bool|bool
- pymbolic.ArithmeticExpression¶
A narrower type alias than
Expressionthat is returned by arithmetic operators, to allow continue doing arithmetic with the result.
alias ofint|integer|float|complex|inexact|ExpressionNode
- pymbolic.typing.Expression¶
A union of types that are considered as part of an expression tree.
alias ofint|integer|float|complex|inexact|bool|bool|ExpressionNode|tuple[Expression, …]
Note
For backward compatibility, pymbolic.Expression will alias
pymbolic.primitives.ExpressionNode for now. Once its
deprecation period is up, it will be removed, and then, in the further
future, pymbolic.Expression may become this type alias.
- class pymbolic.typing.ArithmeticOrExpressionT¶
A
TypeVarthat can be either anArithmeticExpressionor anExpression.alias of TypeVar(‘ArithmeticOrExpressionT’, int | ~numpy.integer | float | complex | ~numpy.inexact | ExpressionNode, int | ~numpy.integer | float | complex | ~numpy.inexact | bool | ~numpy.bool | ExpressionNode | tuple[Expression, …])
- pymbolic.typing.ArithmeticExpressionContainer¶
A type alias for allowable expressions that can act as an operand in arithmetic. This includes numbers and expressions, but also
MultiVectorand (object)numpyarrays.
alias ofint|integer|float|complex|inexact|ExpressionNode|MultiVector[ArithmeticExpression]|ObjectArray[tuple[int],int|integer|float|complex|inexact|ExpressionNode] |ObjectArray[tuple[int,int],int|integer|float|complex|inexact|ExpressionNode] |ObjectArray[tuple[int, …],int|integer|float|complex|inexact|ExpressionNode]
- class pymbolic.typing.ArithmeticExpressionContainerTc¶
A
TypeVarconstrained to the types inArithmeticExpressionContainer. Note that this does not useArithmeticExpressionContaineras a bound.alias of TypeVar(‘ArithmeticExpressionContainerTc’, int | ~numpy.integer | float | complex | ~numpy.inexact | ExpressionNode, MultiVector[ArithmeticExpression], ~pytools.obj_array.ObjectArray[tuple[int], int | ~numpy.integer | float | complex | ~numpy.inexact | ExpressionNode], ~pytools.obj_array.ObjectArray[tuple[int, int], int | ~numpy.integer | float | complex | ~numpy.inexact | ExpressionNode], ~pytools.obj_array.ObjectArray[tuple[int, …], int | ~numpy.integer | float | complex | ~numpy.inexact | ExpressionNode])
Expression base class¶
- class pymbolic.ExpressionNode[source]¶
Superclass for parts of a mathematical expression. Overrides operators to implicitly construct
Sum,Productand other expressions.Expression objects are immutable.
Changed in version 2022.2: PEP 634-style pattern matching is now supported.
- property a: _AttributeLookupCreator¶
Provide a spelling
expr.a.namefor encoding attribute lookup.
- mapper_method: ClassVar[str]¶
The
pymbolic.mapper.Mappermethod called for objects of this type.
- __getitem__(subscript: EmptyOK) Subscript[source]¶
- __getitem__(subscript: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[Expression, ...]) Self | Subscript
Return an expression representing
self[subscript].
- make_stringifier(originating_stringifier=None)[source]¶
Return a
pymbolic.mapper.Mapperinstance 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) bool[source]¶
Provides equality testing with quick positive and negative paths based on
id()and__hash__().
- __str__() str[source]¶
Use the
make_stringifier()to return a human-readable string representation of self.
Logical operator constructors
- not_() LogicalNot[source]¶
Return self wrapped in a
LogicalNot.Added in version 2015.2.
- and_(other) LogicalAnd[source]¶
Return
LogicalAndbetween self and other.Added in version 2015.2.
Comparison constructors
- eq(other) Comparison[source]¶
Return a
Comparisoncomparing self to other.Added in version 2015.2.
- ne(other) Comparison[source]¶
Return a
Comparisoncomparing self to other.Added in version 2015.2.
- lt(other) Comparison[source]¶
Return a
Comparisoncomparing self to other.Added in version 2015.2.
- le(other) Comparison[source]¶
Return a
Comparisoncomparing self to other.Added in version 2015.2.
- gt(other) Comparison[source]¶
Return a
Comparisoncomparing self to other.Added in version 2015.2.
- ge(other) Comparison[source]¶
Return a
Comparisoncomparing self to other.Added in version 2015.2.
- pymbolic.expr_dataclass(init: bool = True, eq: bool = True, hash: bool = True) Callable[[type[T]], type[T]][source]¶
A class decorator that makes the class a
dataclass()while also adding functionality needed forExpressionNode. Specifically, it adds cached hashing, equality comparisons withself is othershortcuts as well as some methods/attributes for backward compatibility (e.g.__getinitargs__,init_arg_names).It also adds a
ExpressionNode.mapper_methodbased on the class name if not already present. Ifmapper_methodis inherited, it will be viewed as unset and replaced.Note that the class to which this decorator is applied need not be a subclass of
ExpressionNode.Added in version 2024.1.
Expressions¶
- class pymbolic.primitives.AlgebraicLeaf[source]¶
Bases:
ExpressionNodeAn expression that serves as a leaf for arithmetic evaluation. This may end up having child nodes still, but they’re not reached by ways of arithmetic.
- class pymbolic.primitives.Leaf[source]¶
Bases:
AlgebraicLeafAn expression that is irreducible, i.e. has no Expression-type parts whatsoever.
- class pymbolic.primitives.Wildcard[source]¶
Bases:
LeafA general wildcard that can be used to substitute expressions.
Function Calls¶
- class pymbolic.primitives.Call(function: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], parameters: tuple[int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], ...])[source]¶
Bases:
AlgebraicLeafA function invocation.
- function: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[Expression, ...]¶
Evaluates to the function to be called.
- class pymbolic.primitives.CallWithKwargs(function: Expression, parameters: tuple[Expression, ...], kw_parameters: Mapping[str, Expression])[source]¶
Bases:
AlgebraicLeafA function invocation with keyword arguments.
- function: Expression¶
Evaluates to the function to be called.
- parameters: tuple[Expression, ...]¶
A
tupleof positional parameters.
- kw_parameters: Mapping[str, Expression]¶
A dictionary mapping names to arguments.
- class pymbolic.primitives.Subscript(aggregate: int | ~numpy.integer | float | complex | ~numpy.inexact | bool | ~numpy.bool | ~pymbolic.primitives.ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], index: int | ~numpy.integer | float | complex | ~numpy.inexact | bool | ~numpy.bool | ~pymbolic.primitives.ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...] = <function ExpressionNode.index>)[source]¶
Bases:
AlgebraicLeafAn array subscript.
Sums, products and such¶
- class pymbolic.primitives.Sum(children: tuple[TypeAliasForwardRef('ArithmeticExpression'), ...])[source]¶
Bases:
ExpressionNode- children: tuple[ArithmeticExpression, ...]¶
- class pymbolic.primitives.Product(children: tuple[TypeAliasForwardRef('ArithmeticExpression'), ...])[source]¶
Bases:
ExpressionNode- children: tuple[ArithmeticExpression, ...]¶
- class pymbolic.primitives.Min(children: tuple[int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], ...])[source]¶
Bases:
ExpressionNode
- class pymbolic.primitives.Max(children: tuple[int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], ...])[source]¶
Bases:
ExpressionNode
- class pymbolic.primitives.Quotient(numerator: ArithmeticExpression, denominator: ArithmeticExpression)[source]¶
Bases:
ExpressionNode- numerator: ArithmeticExpression¶
- denominator: ArithmeticExpression¶
- class pymbolic.primitives.FloorDiv(numerator: ArithmeticExpression, denominator: ArithmeticExpression)[source]¶
Bases:
ExpressionNode- numerator: ArithmeticExpression¶
- denominator: ArithmeticExpression¶
- class pymbolic.primitives.Remainder(numerator: ArithmeticExpression, denominator: ArithmeticExpression)[source]¶
Bases:
ExpressionNode- numerator: ArithmeticExpression¶
- denominator: ArithmeticExpression¶
- class pymbolic.primitives.Power(base: ArithmeticExpression, exponent: ArithmeticExpression)[source]¶
Bases:
ExpressionNode- base: ArithmeticExpression¶
- exponent: ArithmeticExpression¶
Shift operators¶
- class pymbolic.primitives.LeftShift(shiftee: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], shift: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...])[source]¶
Bases:
ExpressionNode.- shiftee: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[Expression, ...]¶
- class pymbolic.primitives.RightShift(shiftee: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], shift: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...])[source]¶
Bases:
ExpressionNode.- shiftee: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[Expression, ...]¶
Bitwise operators¶
- class pymbolic.primitives.BitwiseNot(child: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...])[source]¶
Bases:
ExpressionNode
- class pymbolic.primitives.BitwiseOr(children: tuple[int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], ...])[source]¶
Bases:
ExpressionNode
- class pymbolic.primitives.BitwiseXor(children: tuple[int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], ...])[source]¶
Bases:
ExpressionNode
- class pymbolic.primitives.BitwiseAnd(children: tuple[int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], ...])[source]¶
Bases:
ExpressionNode
Comparisons and logic¶
- class pymbolic.primitives.Comparison(left: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], operator: str, right: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...])[source]¶
Bases:
ExpressionNode- left: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[Expression, ...]¶
- right: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[Expression, ...]¶
Note
Unlike other expressions, comparisons are not implicitly constructed by comparing
ExpressionNodeobjects. Seepymbolic.ExpressionNode.eq()and related.- operator_to_name: ClassVar[dict[str, str]] = {'!=': 'ne', '<': 'lt', '<=': 'le', '==': 'eq', '>': 'gt', '>=': 'ge'}¶
- class pymbolic.primitives.LogicalNot(child: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...])[source]¶
Bases:
ExpressionNode
- class pymbolic.primitives.LogicalAnd(children: tuple[int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], ...])[source]¶
Bases:
ExpressionNode
- class pymbolic.primitives.LogicalOr(children: tuple[int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], ...])[source]¶
Bases:
ExpressionNode
- class pymbolic.primitives.If(condition: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], then: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], else_: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...])[source]¶
Bases:
ExpressionNode- condition: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[Expression, ...]¶
- then: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[Expression, ...]¶
Slices¶
- class pymbolic.primitives.Slice(children: tuple[()] | tuple[int | integer | float | complex | inexact | ExpressionNode | None] | tuple[int | integer | float | complex | inexact | ExpressionNode | None, int | integer | float | complex | inexact | ExpressionNode | None] | tuple[int | integer | float | complex | inexact | ExpressionNode | None, int | integer | float | complex | inexact | ExpressionNode | None, int | integer | float | complex | inexact | ExpressionNode | None])[source]¶
Bases:
ExpressionNodeA slice expression as in a[1:7].
- children: tuple[()] | tuple[int | integer | float | complex | inexact | ExpressionNode | None] | tuple[int | integer | float | complex | inexact | ExpressionNode | None, int | integer | float | complex | inexact | ExpressionNode | None] | tuple[int | integer | float | complex | inexact | ExpressionNode | None, int | integer | float | complex | inexact | ExpressionNode | None, int | integer | float | complex | inexact | ExpressionNode | None]¶
- property start¶
- property stop¶
- property step¶
Code generation helpers¶
- class pymbolic.primitives.CommonSubexpression(child: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], prefix: str | None = None, scope: str = 'pymbolic_eval')[source]¶
Bases:
ExpressionNodeA 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
prefixshould be used.- child: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[Expression, ...]¶
See
pymbolic.mapper.c_code.CCodeMapperfor an example.
- class pymbolic.primitives.cse_scope[source]¶
Determines the lifetime for the saved value of a
CommonSubexpression.- EVALUATION: str¶
The evaluated result lives for the duration of the evaluation of the current expression and is discarded thereafter.
- pymbolic.primitives.make_common_subexpression(expr: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[Expression, ...], prefix: str | None = None, scope: str | None = None, *, wrap_vars: bool = True) CommonSubexpression[source]¶
- pymbolic.primitives.make_common_subexpression(expr: ObjectArray[ShapeT, int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[Expression, ...]], prefix: str | None = None, scope: str | None = None, *, wrap_vars: bool = True) ObjectArray[ShapeT, CommonSubexpression]
- pymbolic.primitives.make_common_subexpression(expr: MultiVector[ArithmeticExpression], prefix: str | None = None, scope: str | None = None, *, wrap_vars: bool = True) MultiVector[ArithmeticExpression]
Wrap expr in a
CommonSubexpressionwith prefix.If expr is a
numpyobject array, each individual entry is instead wrapped. If expr is apymbolic.geometric_algebra.MultiVector, each coefficient is individually wrapped. In general, the function tries to avoid re-wrapping existingCommonSubexpressionif the same scope is given.See
CommonSubexpressionfor the meaning of prefix and scope. The scope defaults tocse_scope.EVALUATION.- Parameters:
wrap_vars – If True, this also wraps a
Variableand its subscripts. Otherwise, these expressions are returned as is.
Symbolic derivatives and substitution¶
Inspired by similar functionality in sympy.
- class pymbolic.primitives.Substitution(child: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], variables: tuple[str, ...], values: tuple[int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], ...])[source]¶
Bases:
ExpressionNodeWork-alike of
Subs.- child: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[Expression, ...]¶
- class pymbolic.primitives.Derivative(child: int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...], variables: tuple[str, ...])[source]¶
Bases:
ExpressionNodeWork-alike of sympy’s
Derivative.
Helper functions¶
- pymbolic.primitives.is_constant(value: object) TypeIs[int | integer | float | complex | inexact | bool | bool][source]¶
- pymbolic.primitives.is_expression(value: object) TypeIs[int | integer | float | complex | inexact | bool | bool | ExpressionNode | tuple[TypeAliasForwardRef('Expression'), ...]][source]¶
Added in version 2024.2.3.
- pymbolic.primitives.is_arithmetic_expression(value: object) TypeIs[TypeAliasForwardRef('ArithmeticExpression')][source]¶
- pymbolic.primitives.flattened_sum(terms: Iterable[ArithmeticExpression]) ArithmeticExpression[source]¶
Recursively flattens all the top level
Sums in terms.
- pymbolic.primitives.flattened_product(terms: Iterable[ArithmeticExpression]) ArithmeticExpression[source]¶
Recursively flattens all the top level
Products in terms.This operation does not change the order of the terms in the products, so it does not require the product to be commutative.
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: str, components: int | list[int], var_factory: Callable[[str], ExpressionNode] = <class 'pymbolic.primitives.Variable'>) ObjectArray[ArithmeticExpression][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
Variablesubclass 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)
- pymbolic.primitives.make_sym_array(name: str, shape: ShapeT, var_factory: Callable[[str], Variable] = <class 'pymbolic.primitives.Variable'>) Variable | ObjectArray[ShapeT, ArithmeticExpression][source]¶
Constants¶
- class pymbolic.primitives.NaN(data_type: Callable[[float], Any] | None = None)[source]¶
Bases:
AlgebraicLeafAn expression node representing not-a-number as a floating point number. Unlike,
math.nan, all instances ofNaNcompare 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.nanis False, but(np.nan,) == (np.nan,)is True.- data_type: Callable[[float], Any] | None = None¶
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
floatcan be passed to obtain a NaN of the yield the desired type. It must also be suitable for use as the second argument ofisinstance().