Primitives (Basic Objects)¶
Typing helpers¶
- pymbolic.ArithmeticExpressionT¶
A narrower type alias than
ExpressionT
that is returned by arithmetic operators, to allow continue doing arithmetic with the result of arithmetic.> alias of
int
|integer
|float
|complex
|inexact
|Expression
- pymbolic.ExpressionT¶
alias of
int
|integer
|float
|complex
|inexact
|bool
|bool
|Expression
|Tuple
[ExpressionT
, …]
Expression base class¶
- class pymbolic.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.
- property a: _AttributeLookupCreator¶
Provide a spelling
expr.a.name
for encoding attribute lookup.
- mapper_method: ClassVar[str]¶
The
pymbolic.mapper.Mapper
method called for objects of this type.
- __getitem__(subscript: ExpressionT | EmptyOK) Expression [source]¶
Return an expression representing
self[subscript]
.
- 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) 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
LogicalAnd
between self and other.Added in version 2015.2.
Comparison constructors
- eq(other) Comparison [source]¶
Return a
Comparison
comparing self to other.Added in version 2015.2.
- ne(other) Comparison [source]¶
Return a
Comparison
comparing self to other.Added in version 2015.2.
- lt(other) Comparison [source]¶
Return a
Comparison
comparing self to other.Added in version 2015.2.
- le(other) Comparison [source]¶
Return a
Comparison
comparing self to other.Added in version 2015.2.
- gt(other) Comparison [source]¶
Return a
Comparison
comparing self to other.Added in version 2015.2.
- ge(other) Comparison [source]¶
Return a
Comparison
comparing self to other.Added in version 2015.2.
- pymbolic.expr_dataclass(init: 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 forExpression
nodes. Specifically, it adds cached hashing, equality comparisons withself is other
shortcuts as well as some methods/attributes for backward compatibility (e.g.__getinitargs__
,init_arg_names
)It also adds a
Expression.mapper_method
based on the class name if not already present. Ifmapper_method
is inherited, it will be viewed as unset and replaced.Added in version 2024.1.
Expressions¶
- class pymbolic.primitives.AlgebraicLeaf[source]¶
Bases:
Expression
An 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:
AlgebraicLeaf
An expression that is irreducible, i.e. has no Expression-type parts whatsoever.
- class pymbolic.primitives.Wildcard[source]¶
Bases:
Leaf
A general wildcard that can be used to substitute expressions.
Function Calls¶
- class pymbolic.primitives.Call(function: ExpressionT, parameters: tuple[ExpressionT, ...])[source]¶
Bases:
AlgebraicLeaf
A function invocation.
- function: ExpressionT¶
A
Expression
that evaluates to a function.
- parameters: tuple[ExpressionT, ...]¶
A
tuple
of positional parameters, each element of which is aExpression
or a constant.
- class pymbolic.primitives.CallWithKwargs(function: ExpressionT, parameters: tuple[ExpressionT, ...], kw_parameters: Mapping[str, ExpressionT])[source]¶
Bases:
AlgebraicLeaf
A function invocation with keyword arguments.
- function: ExpressionT¶
An
Expression
that evaluates to a function.
- parameters: tuple[ExpressionT, ...]¶
A
tuple
of positional parameters, each element of which is aExpression
or a constant.
- kw_parameters: Mapping[str, ExpressionT]¶
A dictionary mapping names to arguments, each of which is a
Expression
or a constant.
- class pymbolic.primitives.Subscript(aggregate: ExpressionT, index: ExpressionT = <function Expression.index>)[source]¶
Bases:
AlgebraicLeaf
An array subscript.
- class pymbolic.primitives.Lookup(aggregate: ExpressionT, name: str)[source]¶
Bases:
AlgebraicLeaf
Access to an attribute of an aggregate, such as an attribute of a class.
Sums, products and such¶
- class pymbolic.primitives.Sum(children: tuple[ExpressionT, ...])[source]¶
Bases:
Expression
- children: tuple[ExpressionT, ...]¶
- class pymbolic.primitives.Product(children: tuple[ExpressionT, ...])[source]¶
Bases:
Expression
- children: tuple[ExpressionT, ...]¶
- class pymbolic.primitives.Min(children: tuple[ExpressionT, ...])[source]¶
Bases:
Expression
- children: tuple[ExpressionT, ...]¶
- class pymbolic.primitives.Max(children: tuple[ExpressionT, ...])[source]¶
Bases:
Expression
- children: tuple[ExpressionT, ...]¶
- class pymbolic.primitives.Quotient(numerator: ExpressionT, denominator: ExpressionT)[source]¶
Bases:
Expression
- numerator: ExpressionT¶
- denominator: ExpressionT¶
- class pymbolic.primitives.FloorDiv(numerator: ExpressionT, denominator: ExpressionT)[source]¶
Bases:
Expression
- numerator: ExpressionT¶
- denominator: ExpressionT¶
- class pymbolic.primitives.Remainder(numerator: ExpressionT, denominator: ExpressionT)[source]¶
Bases:
Expression
- numerator: ExpressionT¶
- denominator: ExpressionT¶
- class pymbolic.primitives.Power(base: ExpressionT, exponent: ExpressionT)[source]¶
Bases:
Expression
- base: ExpressionT¶
- exponent: ExpressionT¶
Shift operators¶
- class pymbolic.primitives.LeftShift(shiftee: ExpressionT, shift: ExpressionT)[source]¶
Bases:
Expression
.- shiftee: ExpressionT¶
- shift: ExpressionT¶
- class pymbolic.primitives.RightShift(shiftee: ExpressionT, shift: ExpressionT)[source]¶
Bases:
Expression
.- shiftee: ExpressionT¶
- shift: ExpressionT¶
Bitwise operators¶
- class pymbolic.primitives.BitwiseNot(child: ExpressionT)[source]¶
Bases:
Expression
- child: ExpressionT¶
- class pymbolic.primitives.BitwiseOr(children: tuple[ExpressionT, ...])[source]¶
Bases:
Expression
- children: tuple[ExpressionT, ...]¶
- class pymbolic.primitives.BitwiseXor(children: tuple[ExpressionT, ...])[source]¶
Bases:
Expression
- children: tuple[ExpressionT, ...]¶
- class pymbolic.primitives.BitwiseAnd(children: tuple[ExpressionT, ...])[source]¶
Bases:
Expression
- children: tuple[ExpressionT, ...]¶
Comparisons and logic¶
- class pymbolic.primitives.Comparison(left: ExpressionT, operator: str, right: ExpressionT)[source]¶
Bases:
Expression
- left: ExpressionT¶
- right: ExpressionT¶
Note
Unlike other expressions, comparisons are not implicitly constructed by comparing
Expression
objects. Seepymbolic.Expression.eq()
.- operator_to_name: ClassVar[dict[str, str]] = {'!=': 'ne', '<': 'lt', '<=': 'le', '==': 'eq', '>': 'gt', '>=': 'ge'}¶
- class pymbolic.primitives.LogicalNot(child: ExpressionT)[source]¶
Bases:
Expression
- child: ExpressionT¶
- class pymbolic.primitives.LogicalAnd(children: tuple[ExpressionT, ...])[source]¶
Bases:
Expression
- children: tuple[ExpressionT, ...]¶
- class pymbolic.primitives.LogicalOr(children: tuple[ExpressionT, ...])[source]¶
Bases:
Expression
- children: tuple[ExpressionT, ...]¶
- class pymbolic.primitives.If(condition: ExpressionT, then: ExpressionT, else_: ExpressionT)[source]¶
Bases:
Expression
- condition: ExpressionT¶
- then: ExpressionT¶
- else_: ExpressionT¶
Slices¶
- class pymbolic.primitives.Slice(children: tuple[()] | tuple[ExpressionT] | tuple[ExpressionT, ExpressionT] | tuple[ExpressionT, ExpressionT, ExpressionT])[source]¶
Bases:
Expression
A slice expression as in a[1:7].
- children: tuple[()] | tuple[ExpressionT] | tuple[ExpressionT, ExpressionT] | tuple[ExpressionT, ExpressionT, ExpressionT]¶
- property start¶
- property stop¶
- property step¶
Code generation helpers¶
- class pymbolic.primitives.CommonSubexpression(child: ExpressionT, prefix: str | None = None, scope: str = 'pymbolic_eval')[source]¶
Bases:
Expression
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: ExpressionT¶
See
pymbolic.mapper.c_code.CCodeMapper
for 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(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.
Symbolic derivatives and substitution¶
Inspired by similar functionality in sympy
.
- class pymbolic.primitives.Substitution(child: ExpressionT, variables: tuple[str, ...], values: tuple[ExpressionT, ...])[source]¶
Bases:
Expression
Work-alike of
Subs
.- child: ExpressionT¶
- values: tuple[ExpressionT, ...]¶
- class pymbolic.primitives.Derivative(child: ExpressionT, variables: tuple[str, ...])[source]¶
Bases:
Expression
Work-alike of sympy’s
Derivative
.- child: ExpressionT¶
Helper functions¶
- pymbolic.primitives.is_constant(value: object) TypeIs[int | integer | float | complex | inexact | bool | bool] [source]¶
- 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: Callable[[float], Any] | None = None)[source]¶
Bases:
Expression
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: 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
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()
.
Helper classes¶
- class pymbolic.primitives._AttributeLookupCreator(aggregate: ExpressionT)[source]¶
Helper used by
pymbolic.Expression.a
to create lookups.
References¶
- class pymbolic.primitives.DataclassInstance¶
An instance of a
dataclass()
.
- class pymbolic.primitives._T¶
A type variable.
- class numpy.bool_¶
Deprecated alias for
numpy.bool
.
- class typing_extensions.TypeIs¶
- class typing_extensions.Variable¶
See
pymbolic.Variable
.
- class typing_extensions.ExpressionT¶
See
pymbolic.ExpressionT
.
- class pymbolic.Comparison¶
- class pymbolic.LogicalNot¶
- class pymbolic.LogicalAnd¶
- class pymbolic.LogicalOr¶
- class pymbolic.Lookup¶