On this page

TensorExpression

symbolica.community.spenso Class

TensorExpression()

A Symbolica expression with an ordered external tensor interface.

Predefined tensors are constructed by typed factories and indexed afterward in logical interface order. Dimensions are representation metadata, not scalar tensor arguments.

Examples

from symbolica.community.spenso import Representation, TensorExpression
mink = Representation.mink(4)
metric = TensorExpression.g(mink)("mu", "nu")
flat = TensorExpression.flat(mink)("mu", "nu")
gamma = TensorExpression.gamma(4)("i", "j", "mu")
gamma5 = TensorExpression.gamma5(4)("i", "j")
projm = TensorExpression.projm(4)("i", "j")
projp = TensorExpression.projp(4)("i", "j")
sigma = TensorExpression.sigma(4)("mu", "nu", "i", "j")
structure_constant = TensorExpression.f(8)("a", "b", "c")
generator = TensorExpression.t(8, 3)("a", "i", "j")

Member details

rank

Property · read-only
#
rank: int

Number of external tensor ports in the ordered interface.

is_scalar

Property · read-only
#
is_scalar: bool

Whether the expression has no external tensor ports.

interface

Property · read-only
#
interface: tuple

Ordered external interface as concrete Slot objects or unresolved Representations.

name

Property · read-only
#
name: Optional[TensorName]

The optional identity used when this expression describes stored data.

g

Static method
#
g(rep: Representation) -> TensorExpression

Create an unresolved metric tensor for rep.

Call the result with two indices to fill its ports in logical order.

Parameters

NameTypeDefaultDescription
repRepresentation——

flat

Static method
#
flat(rep: Representation) -> TensorExpression

Create an unresolved musical-isomorphism tensor for rep.

Call the result with two indices to fill its ports in logical order.

Parameters

NameTypeDefaultDescription
repRepresentation——

gamma

Static method
#
gamma(minkowski_dimension: int | Expression | str) -> TensorExpression

Create an unresolved gamma matrix with Minkowski dimension minkowski_dimension.

Its public ports use storage order: bispinor, bispinor, then Minkowski. The bispinor dimension is four. Call the result with (i, j, mu) to index it.

Parameters

NameTypeDefaultDescription
minkowski_dimensionint | Expression | str——

gamma5

Static method
#
gamma5(spinor_dimension: int | Expression | str) -> TensorExpression

Create an unresolved gamma-five matrix with two bispinor ports of spinor_dimension; call the result with (i, j).

Parameters

NameTypeDefaultDescription
spinor_dimensionint | Expression | str——

projm

Static method
#
projm(spinor_dimension: int | Expression | str) -> TensorExpression

Create an unresolved left-chiral projector with two bispinor ports of spinor_dimension; call the result with (i, j).

Parameters

NameTypeDefaultDescription
spinor_dimensionint | Expression | str——

projp

Static method
#
projp(spinor_dimension: int | Expression | str) -> TensorExpression

Create an unresolved right-chiral projector with two bispinor ports of spinor_dimension; call the result with (i, j).

Parameters

NameTypeDefaultDescription
spinor_dimensionint | Expression | str——

sigma

Static method
#
sigma(minkowski_dimension: int | Expression | str) -> TensorExpression

Create an unresolved sigma matrix with Minkowski dimension minkowski_dimension.

Its logical ports are two Minkowski ports followed by two four-dimensional bispinor ports. Call the result with (mu, nu, i, j) to index it.

Parameters

NameTypeDefaultDescription
minkowski_dimensionint | Expression | str——

f

Static method
#
f(adjoint_dimension: int | Expression | str) -> TensorExpression

Create an unresolved color structure constant.

All three logical ports are adjoint representations of adjoint_dimension; call the result with (a, b, c).

Parameters

NameTypeDefaultDescription
adjoint_dimensionint | Expression | str——

t

Static method
#
t(adjoint_dimension: int | Expression | str, fundamental_dimension: int | Expression | str) -> TensorExpression

Create an unresolved color generator.

The logical ports are adjoint, fundamental, and antifundamental. Dimensions belong to those representations and are not tensor key arguments.

Examples

from symbolica.community.spenso import TensorExpression
generator = TensorExpression.t(8, 3)
indexed = generator("a", "i", "j")

Parameters

NameTypeDefaultDescription
adjoint_dimensionint | Expression | str——
fundamental_dimensionint | Expression | str——

with_name

Method
#
with_name(name: TensorName | str | Expression | TensorExpression) -> TensorExpression

Return a named descriptor without changing the underlying expression.

Parameters

NameTypeDefaultDescription
nameTensorName | str | Expression | TensorExpression——

__len__

Method
#
__len__() -> int

No description is available for this member.

to_expression

Method
#
to_expression() -> Expression

Drop the structured interface and return an ordinary Symbolica Expression.

reinfer

Method
#
reinfer() -> TensorExpression

Re-parse the underlying symbolic expression and rebuild its ordered tensor interface.

expand

Method
#
expand(var: Optional[Expression | int | Float | int | float | str | decimal.Decimal | ComplexFloat | Float | int | float | str | decimal.Decimal | complex | tuple[Float | int | float | str | decimal.Decimal, Float | int | float | str | decimal.Decimal]] = None, via_poly: Optional[bool] = None) -> TensorExpression

Expand scalar algebra while preserving and validating the tensor interface.

Parameters

NameTypeDefaultDescription
varOptional[Expression | int | Float | int | float | str | decimal.Decimal | ComplexFloat | Float | int | float | str | decimal.Decimal | complex | tuple[Float | int | float | str | decimal.Decimal, Float | int | float | str | decimal.Decimal]]None—
via_polyOptional[bool]None—

simplify_gamma

Method
#
simplify_gamma(settings: Optional[GammaSimplifySettings] = None) -> TensorExpression

Apply Idenso's gamma-algebra simplifier and re-infer the tensor interface.

Parameters

NameTypeDefaultDescription
settingsOptional[GammaSimplifySettings]None—

collect_gamma_chains

Method
#
collect_gamma_chains() -> TensorExpression

Convert bispinor tensors into chain/trace shorthands and join adjacent gamma chains.

simplify_gamma0

Method
#
simplify_gamma0() -> TensorExpression

Simplify products and linear combinations involving the time-like gamma matrix gamma0.

simplify_gamma_conjugate

Method
#
simplify_gamma_conjugate() -> TensorExpression

Rewrite conjugated gamma matrices as gamma0-sandwiched matrices.

simplify_epsilon

Method
#
simplify_epsilon() -> TensorExpression

Simplify Levi-Civita/metric contractions and pairs of Levi-Civita tensors.

simplify_metrics

Method
#
simplify_metrics() -> TensorExpression

Contract metric and identity tensors and re-infer the external interface.

simplify_color

Method
#
simplify_color(settings: Optional[ColorSimplifySettings] = None) -> TensorExpression

Apply Idenso's SU(N) color-algebra simplifier and re-infer the interface.

Parameters

NameTypeDefaultDescription
settingsOptional[ColorSimplifySettings]None—

collect_color

Method
#
collect_color() -> TensorExpression

Factor around tensors carrying fundamental, antifundamental, or adjoint color.

collect_color_constants

Method
#
collect_color_constants() -> TensorExpression

Factor around recognized scalar color invariants such as Casimirs and indices.

to_color_casimir

Method
#
to_color_casimir(*, fundamental: Representation, adjoint: Representation, settings: Optional[ColorCasimirSettings] = None) -> TensorExpression

Rewrite supplied color dimensions and invariants into a representation-aware Casimir basis.

Parameters

NameTypeDefaultDescription
fundamentalRepresentation——
adjointRepresentation——
settingsOptional[ColorCasimirSettings]None—

to_cof_dimension_invariants

Method
#
to_cof_dimension_invariants() -> TensorExpression

Replace supported cof(N) invariants by explicit dimension formulas.

wrap_color

Method
#
wrap_color(symbol: Expression) -> TensorExpression

Expand around color structures and wrap each scalar coefficient with symbol.

Parameters

NameTypeDefaultDescription
symbolExpression——

expand_mink

Method
#
expand_mink() -> list[tuple[Expression, Expression]]

Selectively expand around Minkowski structures into (structure, coefficient) pairs.

expand_bis

Method
#
expand_bis() -> list[tuple[Expression, Expression]]

Selectively expand around bispinor structures into (structure, coefficient) pairs.

expand_mink_bis

Method
#
expand_mink_bis() -> list[tuple[Expression, Expression]]

Selectively expand around Minkowski and bispinor structures into factorized pairs.

expand_metrics

Method
#
expand_metrics() -> list[tuple[Expression, Expression]]

Selectively expand around metric tensors into (structure, coefficient) pairs.

expand_color

Method
#
expand_color() -> list[tuple[Expression, Expression]]

Selectively expand around color structures into (structure, coefficient) pairs.

expand_in_patterns

Method
#
expand_in_patterns(patterns: Sequence[Expression]) -> list[tuple[Expression, Expression]]

Selectively expand around the supplied Symbolica patterns into factorized pairs.

Parameters

NameTypeDefaultDescription
patternsSequence[Expression]——

wrap_indices

Method
#
wrap_indices(header: Expression) -> Expression

Wrap every abstract-index payload with header and return an ordinary expression.

Wrapped payloads are not Spenso abstract indices until they are cooked, so the result intentionally has no TensorExpression interface.

Parameters

NameTypeDefaultDescription
headerExpression——

cook_indices

Method
#
cook_indices(settings: Optional[CookSettings] = None) -> TensorExpression

Flatten nested representation-index payloads using index cooking by default.

Parameters

NameTypeDefaultDescription
settingsOptional[CookSettings]None—

cook_function

Method
#
cook_function(settings: Optional[CookSettings] = None) -> Expression

Encode one function call as an ordinary Symbolica expression.

Parameters

NameTypeDefaultDescription
settingsOptional[CookSettings]None—

wrap_dummies

Method
#
wrap_dummies(header: Expression) -> TensorExpression

Wrap only contracted-index payloads with header and re-infer the interface.

Raises ValueError when the expression cannot be parsed as a tensor network.

Parameters

NameTypeDefaultDescription
headerExpression——

list_dangling

Method
#
list_dangling() -> list[Expression]

Return the ordered external, uncontracted indices as Symbolica expressions.

Raises ValueError when the expression cannot be parsed as a tensor network.

canonize

Method
#
canonize() -> TensorExpression

Canonically order tensor factors and deterministically rename contracted indices.

alias_subtensors

Method
#
alias_subtensors(tensor_name: str) -> tuple[Expression, list[tuple[Expression, Expression]]]

Replace nested tensor subexpressions by aliases and return the root plus alias mappings.

Parameters

NameTypeDefaultDescription
tensor_namestr——

spenso_conjugate

Method
#
spenso_conjugate() -> TensorExpression

Complex-conjugate this tensor expression and re-infer its interface.

conjugate_transpose

Method
#
conjugate_transpose(representation: Representation) -> TensorExpression

Complex-conjugate this expression and transpose slots in representation.

Parameters

NameTypeDefaultDescription
representationRepresentation——

dirac_adjoint

Method
#
dirac_adjoint() -> TensorExpression

Construct the physics-aware Dirac adjoint and re-infer the tensor interface.

cook

Method
#
cook(settings: Optional[CookSettings] = None) -> Expression

Encode this expression using Idenso's reversible cooking format.

Parameters

NameTypeDefaultDescription
settingsOptional[CookSettings]None—

uncook

Method
#
uncook(settings: Optional[CookSettings] = None) -> TensorExpression

Restore a reversibly cooked expression and re-infer its tensor interface.

Parameters

NameTypeDefaultDescription
settingsOptional[CookSettings]None—

schoonschip

Method
#
schoonschip(settings: Optional[SchoonschipSettings] = None) -> TensorExpression

Simplify tensor shorthands using the configured Schoonschip traversal.

Parameters

NameTypeDefaultDescription
settingsOptional[SchoonschipSettings]None—

schoonschip_net

Method
#
schoonschip_net(settings: Optional[SchoonschipSettings] = None, *, expand_contracted_sums: bool = False) -> TensorExpression

Parse and contract this expression as a symbolic tensor network.

Parameters

NameTypeDefaultDescription
settingsOptional[SchoonschipSettings]None—
expand_contracted_sumsboolFalse—

to_dots

Method
#
to_dots() -> TensorExpression

Convert contracted rank-one tensors into compact dot-product notation.

normalize_dots

Method
#
normalize_dots() -> TensorExpression

Canonicalize compact dot-product shorthands without expanding them.

expand_dots

Method
#
expand_dots() -> TensorExpression

Expand dot products into explicit metric and indexed-vector contractions.

metric_shorthand_to_dot

Method
#
metric_shorthand_to_dot() -> TensorExpression

Replace metric shorthand such as g(p(rep), q(rep)) by a compact dot product.

undo_all

Method
#
undo_all() -> TensorExpression

Expand every Idenso tensor shorthand into explicit tensor syntax.

undo_schoonschip

Method
#
undo_schoonschip() -> TensorExpression

Expand Schoonschip shorthands while leaving dots, chains, and traces compact.

undo_dots

Method
#
undo_dots() -> TensorExpression

Expand dot-product shorthands while leaving other shorthands compact.

undo_chain

Method
#
undo_chain() -> TensorExpression

Expand open-chain shorthands while leaving other shorthands compact.

undo_trace

Method
#
undo_trace() -> TensorExpression

Expand trace shorthands while leaving other shorthands compact.

collect_chains

Method
#
collect_chains(representation: Representation) -> TensorExpression

Join adjacent open chains for representation and re-infer the interface.

Parameters

NameTypeDefaultDescription
representationRepresentation——

chainify

Method
#
chainify(representation: Representation) -> TensorExpression

Rewrite tensors with two representation slots as open-chain factors.

Parameters

NameTypeDefaultDescription
representationRepresentation——

normalize_chains

Method
#
normalize_chains() -> TensorExpression

Convert chains whose endpoints coincide into trace shorthands.

undo_single_length

Method
#
undo_single_length() -> TensorExpression

Replace one-factor chain shorthands by their underlying tensor factor.

to_network

Method
#
to_network(library: Optional[TensorLibrary] = None) -> TensorNetwork

Materialize unresolved ports and parse this expression as a tensor network.

When library is omitted, use the built-in four-dimensional HEP and SU(3) library.

Parameters

NameTypeDefaultDescription
libraryOptional[TensorLibrary]None—

index

Method
#
index(*indices: Any, cook_indices: bool = False) -> TensorExpression

Fill the unresolved external ports with indices in interface order.

Pass AUTO to leave a port unresolved. Set cook_indices=True to flatten nested symbolic index payloads before insertion. Repeated compatible indices contract their ports, in which case the result no longer carries the original stored-data identity.

Parameters

NameTypeDefaultDescription
indicesAny——
cook_indicesboolFalse—

__call__

Method
#
__call__(*indices: Any, cook_indices: bool = False) -> TensorExpression

Fill the unresolved external ports with indices in interface order.

Parameters

NameTypeDefaultDescription
indicesAny——
cook_indicesboolFalse—

__neg__

Method
#
__neg__() -> TensorExpression

No description is available for this member.

__add__

Method
#
__add__(rhs: Any) -> TensorExpression | TensorNetwork

No description is available for this member.

Parameters

NameTypeDefaultDescription
rhsAny——

__radd__

Method
#
__radd__(lhs: Any) -> TensorExpression | TensorNetwork

No description is available for this member.

Parameters

NameTypeDefaultDescription
lhsAny——

__sub__

Method
#
__sub__(rhs: Any) -> TensorExpression | TensorNetwork

No description is available for this member.

Parameters

NameTypeDefaultDescription
rhsAny——

__rsub__

Method
#
__rsub__(lhs: Any) -> TensorExpression | TensorNetwork

No description is available for this member.

Parameters

NameTypeDefaultDescription
lhsAny——

__mul__

Method
#
__mul__(rhs: Any) -> TensorExpression | TensorNetwork

No description is available for this member.

Parameters

NameTypeDefaultDescription
rhsAny——

__rmul__

Method
#
__rmul__(lhs: Any) -> TensorExpression | TensorNetwork

No description is available for this member.

Parameters

NameTypeDefaultDescription
lhsAny——

__truediv__

Method
#
__truediv__(rhs: Any) -> TensorExpression | TensorNetwork

No description is available for this member.

Parameters

NameTypeDefaultDescription
rhsAny——

__rtruediv__

Method
#
__rtruediv__(lhs: Any) -> TensorExpression | TensorNetwork

No description is available for this member.

Parameters

NameTypeDefaultDescription
lhsAny——

outer

Method
#
outer(rhs: Any) -> TensorExpression | TensorNetwork

Form an outer product without contracting compatible ports.

Parameters

NameTypeDefaultDescription
rhsAny——

contract

Method
#
contract(rhs: Any, *, left: int, right: int) -> TensorExpression | TensorNetwork

Contract one selected pair of ordered interface positions.

Parameters

NameTypeDefaultDescription
rhsAny——
leftint——
rightint——

compose

Method
#
compose(rhs: Any, *, left: tuple[int, int], right: tuple[int, int]) -> TensorExpression | TensorNetwork

Compose two selected (input, output) matrix channels.

Parameters

NameTypeDefaultDescription
rhsAny——
lefttuple[int, int]——
righttuple[int, int]——

trace

Method
#
trace(*, channel: Optional[tuple[int, int]] = None) -> TensorExpression

Close channel, or the unique matrix channel when it is omitted.

Parameters

NameTypeDefaultDescription
channelOptional[tuple[int, int]]None—

format_tensor

Method
#
format_tensor(show_dimensions: Optional[bool] = None, *, settings: Optional[DisplaySettings] = None) -> str

Format this structured expression using compact Spenso notation.

Parameters

NameTypeDefaultDescription
show_dimensionsOptional[bool]None—
settingsOptional[DisplaySettings]None—

to_typst

Method
#
to_typst(show_dimensions: Optional[bool] = None, *, settings: Optional[DisplaySettings] = None) -> str

Format this structured expression as Typst math source.

Parameters

NameTypeDefaultDescription
show_dimensionsOptional[bool]None—
settingsOptional[DisplaySettings]None—

formatted

Method
#
formatted(show_dimensions: Optional[bool] = None, *, settings: Optional[DisplaySettings] = None, notation_source: Optional[str] = None) -> FormattedOutput

Build Symbolica's rich display value, including semantic HTML when the optional gammaloop[typst-display] renderer is installed.

notation_source is a trusted complete replacement for the bundled notation.typ module, not a style fragment. Typst executes it. When HTML cannot be rendered, the result retains its LaTeX and text forms.

Parameters

NameTypeDefaultDescription
show_dimensionsOptional[bool]None—
settingsOptional[DisplaySettings]None—
notation_sourceOptional[str]None—

to_html

Method
#
to_html(show_dimensions: Optional[bool] = None, *, settings: Optional[DisplaySettings] = None, notation_source: Optional[str] = None) -> str

Compile this expression to semantic HTML with the optional Typst renderer.

Install gammaloop[typst-display] to enable this method. notation_source, when supplied, is trusted Typst code replacing the complete bundled notation.typ module.

Parameters

NameTypeDefaultDescription
show_dimensionsOptional[bool]None—
settingsOptional[DisplaySettings]None—
notation_sourceOptional[str]None—

to_svg

Method
#
to_svg(show_dimensions: Optional[bool] = None, *, settings: Optional[DisplaySettings] = None, notation_source: Optional[str] = None) -> str

Compile this expression to SVG with the optional Typst renderer.

Install gammaloop[typst-display] to enable this method. notation_source, when supplied, is trusted Typst code replacing the complete bundled notation.typ module.

Parameters

NameTypeDefaultDescription
show_dimensionsOptional[bool]None—
settingsOptional[DisplaySettings]None—
notation_sourceOptional[str]None—

__repr__

Method
#
__repr__() -> str

No description is available for this member.

__str__

Method
#
__str__() -> str

No description is available for this member.

_repr_pretty_

Method
#
_repr_pretty_(pretty: Any, cycle: bool) -> None

No description is available for this member.

Parameters

NameTypeDefaultDescription
prettyAny——
cyclebool——

_repr_html_

Method
#
_repr_html_() -> Optional[str]

No description is available for this member.

_repr_latex_

Method
#
_repr_latex_() -> str

No description is available for this member.

__getitem__

Method
#

Overloads

Overload 1 #
__getitem__(item: int) -> list[int]

Convert a logical row-major flat index to tensor coordinates.

Parameters
NameTypeDefaultDescription
itemint——
Overload 2 #
__getitem__(item: Sequence[int]) -> int

Convert tensor coordinates to a logical row-major flat index.

Parameters
NameTypeDefaultDescription
itemSequence[int]——
Overload 3 #
__getitem__(item: slice) -> list[list[int]]

Expand a slice of logical row-major flat indices to tensor coordinates.

Parameters
NameTypeDefaultDescription
itemslice——