Spenso · latest
On this page

TensorRule

symbolica.community.tensor Class

TensorRule(pattern: TensorExpression | _ScalarInput | HeldExpression, rhs: TensorExpression | _ScalarInput | HeldExpression | Callable[[dict[Expression, Expression]], TensorExpression | _ScalarInput], *, cond: Optional[PatternRestriction | Condition] = None, rhs_cache_size: int = 100)

A reusable replacement rule that preserves tensor interfaces.

Use with TensorExpression.replace. A replacement must preserve the matched tensor's external indices, including their representations. Literal replacements are checked where possible at construction; wildcard-dependent replacements are checked when matched.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import TensorRule
indexed = A("i", "j")
rule = TensorRule(indexed, 2 * indexed)
indexed.replace(rule).rank

Constructor

#

Construct a checked whole-tensor replacement rule.

Returns

TensorRule

Reusable replacement, without applying it yet.

Notes

Right-hand-side wildcards must be bound by the pattern. Index multiplicity and external representations must remain valid after substitution.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import TensorRule
indexed = A("i", "j")
rule = TensorRule(indexed, 2 * indexed)
result = indexed.replace(rule)

Parameters

NameTypeDefaultDescription
patternTensorExpression | _ScalarInput | HeldExpression—

Tensor to match. TensorPattern can describe wildcard tensor syntax.

rhsTensorExpression | _ScalarInput | HeldExpression | Callable[[dict[Expression, Expression]], TensorExpression | _ScalarInput]—

Replacement with the same external interface. A callable receives a dict from wildcard Expressions to matched Expressions and returns the replacement. Zero retains the matched tensor's interface.

condOptional[PatternRestriction | Condition]None

Condition evaluated for each candidate match.

rhs_cache_sizeint100

Maximum number of cached replacement results per application. Use zero for a callback whose side effects must run at every match.

Member details

__repr__

Method#
__repr__() -> str

Return a readable object description for inspection.

Examples

from symbolica.community import tensor as sp
r = sp.Representation.euc(2)
A = sp.TensorName("docs::A")(r, r)
rule = sp.TensorRule(A("i", "j"), 2 * A("i", "j"))
text = repr(rule)