Idenso · latest
On this page

Tensor

symbolica.community.tensor Class

Tensor()

Tensor components together with their symbolic identity and ordered axes.

Use dense(), sparse(), or from_numpy() to construct one. Components can be real floats, complex floats, or Symbolica expressions. Square brackets access components; calling the tensor assigns abstract index labels and returns a lazy TensorNetwork. Arithmetic also builds networks, retaining component data.

The default notebook view is an interactive component explorer with a matrix toggle. to_typst() provides static mathematical source.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor[1, 0]

Member details

dtype

Property · read-only#
dtype: type[float] | type[complex] | type[Expression]

Python type used for the stored components.

Returns

type

One of float, complex, or Expression.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor.dtype is float

storage

Property · read-only#
storage: Literal['dense', 'sparse']

How components are stored.

Returns

str

"dense" for all entries, or "sparse" for populated entries.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor.storage

rank

Property · read-only#
rank: int

Number of external tensor axes.

Returns

int

Zero for a scalar.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor.rank

is_scalar

Property · read-only#
is_scalar: bool

Whether the tensor has no external axes.

Returns

bool

True exactly when rank is zero.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor.is_scalar

shape

Property · read-only#
shape: tuple[int, ...]

Dimensions in logical axis order.

Returns

tuple of int

Axis sizes in the order of axes, the current component view.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor.shape

axes

Property · read-only#
axes: tuple[Slot | Representation, ...]

External axes in the current component and port-position order.

Returns

tuple of Slot or Representation

Indexed or unresolved axes, respectively. Component coordinates and methods taking axis positions use this order. It follows construction and explicit permute_axes() calls; structure.axes instead gives the canonical signature.

Examples

from symbolica.community.tensor import Representation, TensorName
r = Representation.euc(3)
A = TensorName("A")(r("j"), r("i"))
A.axes == (r("j"), r("i"))
A.permute_axes([1, 0]).axes == A.axes[::-1]

structure

Property · read-only#
structure: TensorStructure

Canonical external signature of the whole tensor.

Returns

TensorStructure

Free axes in canonical order, independent of names and component layout.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor.structure.rank

copy

Method#
copy() -> Tensor

Copy this tensor's components and metadata.

Returns

Tensor

An independent copy. Changes to it do not alter the original.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
copied = tensor.copy()
copied.shape == tensor.shape

map_components

Method#
map_components(callback: Callable[[Expression | float | complex], Expression | float | complex], *, dtype: type[float] | type[complex] | type[Expression] | None = None) -> Tensor

Apply a scalar function to component values.

Returns

Tensor

An independent tensor with the same axes and mapped components.

Notes

Sparse storage remains sparse when the implicit zero maps to zero. If it maps to a nonzero value, the result becomes dense. The implicit zero is mapped once; avoid relying on callback invocation counts.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor.map_components(lambda value: value * 2)[1, 0]

Parameters

NameTypeDefaultDescription
callbackCallable[[Expression | float | complex], Expression | float | complex]—

Function receiving one component and returning its replacement. The traversal order is unspecified.

dtypetype[float] | type[complex] | type[Expression] | NoneNone

Output component type: float, complex, or Expression. Defaults to the current type; callback results must convert to the selected type.

to_numpy

Method#
to_numpy() -> numpy.NDArray[numpy.float64] | numpy.NDArray[numpy.complex128]

Copy numerical components into a NumPy array.

Numeric Expression literals, such as exact results of a contraction, are rounded to float64. The array uses complex128 if a converted value has a nonzero imaginary part.

Returns

numpy.ndarray

Array with the tensor's logical shape and dtype float64 or complex128.

Raises

TypeError

An Expression component is not a numeric literal. Evaluate it first.

ValueError

An Expression component converts to a nonfinite value.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor.to_numpy().tolist()

from_numpy

Static method#
from_numpy(structure: TensorExpression, array: numpy.ArrayLike) -> Tensor

Copy a numerical array into a named tensor.

Returns

Tensor

Dense tensor with float or complex components.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.from_numpy(A, [[1.0, 2.0], [3.0, 4.0]])
tensor[1, 0]

Parameters

NameTypeDefaultDescription
structureTensorExpression—

Named descriptor with concrete dimensions matching the array shape.

arraynumpy.ArrayLike—

Real or complex array-like data. Axes follow the descriptor's current axes.

evaluator

Method#
evaluator(params: Sequence[Expression], functions: Sequence[FunctionDefinition] = [], iterations: int = 1, cpe_iterations: Optional[int] = None, n_cores: int = 4, verbose: bool = False, jit_compile: bool = True, direct_translation: bool = True, jit_direct_translation: bool = False, jit_optimization_level: int = 3, jit_options: Mapping[str, str] = {}, max_horner_scheme_variables: int = 500, max_common_pair_cache_entries: int = 1000000, max_common_pair_distance: int = 100, horner_use_cpe: bool = False) -> TensorEvaluator

Optimise all components together using Symbolica's evaluator.

Accepts the same parameters, FunctionDefinitions, optimisation controls, and JIT settings as Expression.evaluator. Fixed values can be substituted before construction. Evaluation returns one Tensor per input row, retaining the logical axes and data identity. JIT compilation occurs on first use. Set horner_use_cpe=True to include common-pair elimination when scoring Horner candidates. Final common-pair elimination remains enabled in both modes and is bounded by cpe_iterations.

from symbolica import S
from symbolica.community.tensor import Tensor, TensorName, Representation
x = S("x")
values = Tensor.dense(TensorName.vector("eval_v")(Representation.euc(2)), [x, x**2])
values.evaluator([x]).evaluate([[2.0]])[0][1]

Parameters

NameTypeDefaultDescription
paramsSequence[Expression]——
functionsSequence[FunctionDefinition][]—
iterationsint1—
cpe_iterationsOptional[int]None—
n_coresint4—
verboseboolFalse—
jit_compileboolTrue—
direct_translationboolTrue—
jit_direct_translationboolFalse—
jit_optimization_levelint3—
jit_optionsMapping[str, str]{}—
max_horner_scheme_variablesint500—
max_common_pair_cache_entriesint1000000—
max_common_pair_distanceint100—
horner_use_cpeboolFalse—

expression

Method#
expression() -> TensorExpression

Return the symbolic descriptor for these components.

Returns

TensorExpression

A symbolic tensor with the same external axes.

Notes

The descriptor identifies the data but does not embed its components. Register the tensor in a TensorLibrary to evaluate that symbolic reference.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor.expression().rank

with_name

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

Assign a data identity without rewriting the symbolic computation.

Returns

Tensor

A new value with the requested identity and the same axes and components.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
named = tensor.with_name("named_matrix")
named.rank

Parameters

NameTypeDefaultDescription
nameTensorName | str | Expression | TensorExpression—

New tensor name. A rank-zero atomic TensorExpression may also supply scalar key arguments, for example TensorName("B")(7).

sparse

Static method#
sparse(structure: TensorExpression, type_info: type) -> Tensor

Create an initially zero tensor using sparse component storage.

Returns

Tensor

A zero tensor storing only explicitly populated entries.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.sparse(A, float)
tensor[1, 0] = 3.0
tensor[0, 0]

Parameters

NameTypeDefaultDescription
structureTensorExpression—

Named descriptor with concrete dimensions in logical axis order.

type_infotype—

Component type: float or Expression. Expression storage preserves exact symbolic values and accepts integer assignments without rounding. Floating-point storage requires numerical assignments.

dense

Static method#
dense(structure: TensorExpression, data: Sequence[Expression | int | complex | float]) -> Tensor

Store every component of a named tensor.

Returns

Tensor

A dense component tensor; the input descriptor remains symbolic.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor.shape

Parameters

NameTypeDefaultDescription
structureTensorExpression—

Named descriptor with concrete dimensions and the desired logical axis order. Use with_name() to give a composite expression a name.

dataSequence[Expression | int | complex | float]—

Flat component data in logical row-major order. The length must equal the product of the dimensions. An explicit Expression selects symbolic storage for the sequence, preserving exact rational and algebraic values. Integer-only input is exact too. Float or complex sequences without Expressions retain numerical storage.

to_dense

Method#
to_dense() -> Tensor

Copy the components into dense storage.

Returns

Tensor

Independent dense tensor with the same values and logical axes.

Notes

Dense storage allocates space for every component.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor.to_dense().storage

to_sparse

Method#
to_sparse() -> Tensor

Copy the components into sparse storage.

Returns

Tensor

Independent sparse tensor with the same values and logical axes.

Notes

Sparse storage retains nonzero entries and represents other entries by zero.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor.to_sparse().storage

__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)
tensor = sp.Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
text = repr(tensor)

__str__

Method#
__str__() -> str

Return a readable text representation.

Examples

from symbolica.community import tensor as sp
r = sp.Representation.euc(2)
A = sp.TensorName("docs::A")(r, r)
tensor = sp.Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
text = str(tensor)

format_tensor

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

Produce compact plain-text tensor notation.

Returns

str

Readable tensor text, suitable for logs or terminal output.

Notes

Only ports layout and default spacing are supported in this source format. Use to_html() or to_svg() for other layouts and custom gaps.

Components follow the current view's logical interface order (the order of axes), independently of the canonical structure signature. Large static displays show a bounded preview rather than every component.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
output = tensor.format_tensor()

Parameters

NameTypeDefaultDescription
show_dimensionsOptional[bool]None

Override settings.show_dimensions for this call. None retains the selected settings, whose default omits dimensions.

settingsOptional[DisplaySettings]None

Tensor notation and display choices. Defaults to DisplaySettings().

to_typst

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

Produce static Typst math source.

Returns

str

Typst source without an enclosing document; no compilation is performed.

Notes

Only ports layout and default spacing are supported in this source format. Use to_html() or to_svg() for other layouts and custom gaps.

Components follow the current view's logical interface order (the order of axes), independently of the canonical structure signature. Large static displays show a bounded preview rather than every component.

Static Typst output remains available independently of the HTML explorer.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
output = tensor.to_typst()

Parameters

NameTypeDefaultDescription
show_dimensionsOptional[bool]None

Override settings.show_dimensions for this call. None retains the selected settings, whose default omits dimensions.

settingsOptional[DisplaySettings]None

Tensor notation and display choices. Defaults to DisplaySettings().

to_typst_project

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

Return the source files of a complete Typst project without compiling it. Custom notation is source code for notation.typ in the returned mapping.

# Errors

Returns a Python exception if the tensor cannot be serialized. Registered Python printer exceptions are logged and decline the override, allowing native fallback. Exporting does not import or invoke a compiler.

Examples

from symbolica.community.tensor import Representation, Tensor, TensorName
tensor = Tensor.dense(TensorName("A")(Representation.euc(2)), [1., 2.])
project = tensor.to_typst_project()
sorted(project)

Parameters

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

formatted

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

Create a rich display value for a notebook.

Returns

FormattedOutput

Symbolica display wrapper with text and available rich representations.

Notes

Components follow logical axis order. Large static displays show a bounded preview rather than every component.

# Errors

Returns ValueError for Typst notation or incomplete MathML. Registered Python printer exceptions are logged and decline the override, allowing native fallback.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
output = tensor.formatted()

Parameters

NameTypeDefaultDescription
show_dimensionsOptional[bool]None

Override settings.show_dimensions for this call. None retains the selected settings, whose default omits dimensions.

settingsOptional[DisplaySettings]None

Tensor notation and display choices. Defaults to DisplaySettings().

notation_sourceOptional[str]None

Unsupported for HTML and formatted output; raises ValueError. Use to_typst_project() or to_svg() for custom Typst notation.

to_html

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

Render the tensor as an HTML fragment.

Returns

str

Self-contained output fragment for an HTML-capable notebook or page.

Notes

Mathematical rendering uses direct MathML without a Typst compiler. Pass the returned fragment to the notebook's HTML display facility.

Components follow logical axis order. Large static displays show a bounded preview rather than every component.

The default is a responsive component explorer with a Memory grid / Matrix toggle. Dark mode follows the surrounding page. The color scale measures stored component payload bytes, not total process memory. Use DisplaySettings(tensor_view="matrix") for static mathematical HTML.

# Errors

Returns ValueError for Typst notation or incomplete MathML. Registered Python printer exceptions are logged and decline the override, allowing native fallback.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
output = tensor.to_html()

Parameters

NameTypeDefaultDescription
show_dimensionsOptional[bool]None

Override settings.show_dimensions for this call. None retains the selected settings, whose default omits dimensions.

settingsOptional[DisplaySettings]None

Tensor notation and display choices. Defaults to DisplaySettings().

notation_sourceOptional[str]None

Unsupported for HTML and formatted output; raises ValueError. Use to_typst_project() or to_svg() for custom Typst notation.

to_svg

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

Render static mathematical tensor notation to SVG.

Returns

str

SVG markup suitable for embedding or saving to a file.

Notes

SVG export uses the optional Python package typst (typst-py). Pass the returned markup to the notebook's SVG display facility.

Components follow logical axis order. Large static displays show a bounded preview rather than every component.

# Errors

Returns ImportError if optional typst-py is unavailable, or RuntimeError if compilation fails. Registered Python printer exceptions are logged and decline the override, allowing native fallback.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
output = tensor.to_svg()

Parameters

NameTypeDefaultDescription
show_dimensionsOptional[bool]None

Override settings.show_dimensions for this call. None retains the selected settings, whose default omits dimensions.

settingsOptional[DisplaySettings]None

Tensor notation and display choices. Defaults to DisplaySettings().

notation_sourceOptional[str]None

Custom Typst notation source used by the rich renderer. This is Typst code, not a filename; omit it for the supplied tensor notation.

_repr_html_

Method#
_repr_html_() -> Optional[str]

No description is available for this member.

_repr_latex_

Method#
_repr_latex_() -> Optional[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——

__len__

Method#
__len__() -> int

Count all components in the tensor shape.

Returns

int

Product of the axis dimensions, including implicit zeros.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
len(tensor)

scalar

Method#
scalar() -> Expression

Extract the only component of a rank-zero tensor.

Examples

from symbolica.community import tensor as sp
scalar = sp.Tensor.dense(sp.TensorName("docs::scalar")(), [2.0])
value = scalar.scalar()

Returns

Expression

Scalar value converted to a Symbolica expression, including numeric data.

Raises

RuntimeError

The tensor has external axes.

Examples

from symbolica.community.tensor import TensorExpression
TensorExpression(3).to_tensor().scalar() == 3

index

Method#
index(*indices: _IndexInput, intern: Optional[Literal['indices', 'flattened']] = None) -> TensorNetwork

Assign labels to the unresolved external axes.

Returns

TensorNetwork

An indexed network retaining the component data; the original is unchanged.

Notes

Existing explicit labels are left in place. Use reindex to replace them.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
indexed = tensor.index("i", "j")
indexed.rank

Parameters

NameTypeDefaultDescription
indices_IndexInput——
internOptional[Literal['indices', 'flattened']]None

Encode unknown function-valued index labels before checking the tensor structure. "indices" retains reversible payloads; "flattened" creates readable names. Both preserve index tags and leave scalar arguments and tensor heads intact. Native index forms, including named, scoped, and open indices, remain unchanged. None accepts native index forms without encoding unknown function labels.

reindex

Method#
reindex(*indices: _IndexInput, intern: Optional[Literal['indices', 'flattened']] = None) -> TensorNetwork

Assign labels to all external axes.

Returns

TensorNetwork

An indexed network retaining the component data; the original is unchanged.

Notes

Existing labels are replaced as well as unresolved axes.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
indexed = tensor.reindex("i", "j")
indexed.rank

Parameters

NameTypeDefaultDescription
indices_IndexInput——
internOptional[Literal['indices', 'flattened']]None

Encode unknown function-valued index labels before checking the tensor structure. "indices" retains reversible payloads; "flattened" creates readable names. Both preserve index tags and leave scalar arguments and tensor heads intact. Native index forms, including named, scoped, and open indices, remain unchanged. None accepts native index forms without encoding unknown function labels.

rename_indices

Method#
rename_indices(mapping: dict[int | str | Expression | Slot, _IndexInput], *, intern: Optional[Literal['indices', 'flattened']] = None) -> Tensor

Rename selected external labels without changing the tensor rank.

Returns

Tensor

A new value with renamed axes; the original is unchanged.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
indexed = Tensor.dense(A("i", "j"), [1.0, 2.0, 3.0, 4.0])
indexed.rename_indices({"i": "k"}).rank

Parameters

NameTypeDefaultDescription
mappingdict[int | str | Expression | Slot, _IndexInput]—

Old label to new label. A Slot key also restricts the representation. Keys must identify existing external axes. Each axis may be renamed once; renaming must preserve the external interface.

internOptional[Literal['indices', 'flattened']]None

Encode unknown function-valued index labels before checking the tensor structure. "indices" retains reversible payloads; "flattened" creates readable names. Both preserve index tags and leave scalar arguments and tensor heads intact. Native index forms, including named, scoped, and open indices, remain unchanged. None accepts native index forms without encoding unknown function labels.

permute_axes

Method#
permute_axes(axes: Sequence[int]) -> Tensor

Reorder the external axes in logical component order.

Returns

Tensor

A new tensor view with the reordered interface and correspondingly reordered component access.

Notes

This permutes axes; it does not conjugate component values.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
transposed = tensor.permute_axes([1, 0])
transposed.shape

Parameters

NameTypeDefaultDescription
axesSequence[int]—

Each current axis position exactly once, in the desired new order. For a matrix, [1, 0] exchanges the two axes.

__copy__

Method#
__copy__() -> Tensor

Implement copy.copy using an independent Tensor copy.

Returns

Tensor

Copy of the component data and metadata.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
import copy
copied = copy.copy(tensor)

__call__

Method#
__call__(*indices: _IndexInput, intern: Optional[Literal['indices', 'flattened']] = None) -> TensorNetwork

Assign labels to the unresolved external axes.

Returns

TensorNetwork

An indexed network retaining the component data; the original is unchanged.

Notes

Equivalent to index(*indices).

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
indexed = tensor("i", "j")
indexed.rank

Parameters

NameTypeDefaultDescription
indices_IndexInput——
internOptional[Literal['indices', 'flattened']]None

Encode unknown function-valued index labels before checking the tensor structure. "indices" retains reversible payloads; "flattened" creates readable names. Both preserve index tags and leave scalar arguments and tensor heads intact. Native index forms, including named, scoped, and open indices, remain unchanged. None accepts native index forms without encoding unknown function labels.

__neg__

Method#
__neg__() -> TensorNetwork

Negate every tensor component.

Returns

TensorNetwork

Negated symbolic algebra retaining component data lazily.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
negated = -tensor

__add__

Method#
__add__(rhs: _ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]) -> TensorNetwork

Add tensors with compatible external interfaces.

Returns

TensorNetwork

New lazy calculation; operands are unchanged.

Notes

Scalar zero acts as the additive identity. Other scalars can be added only to rank-zero tensors.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
result = tensor + tensor

Parameters

NameTypeDefaultDescription
rhs_ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]—

Other operand. Component data are retained in a lazy network.

__radd__

Method#
__radd__(lhs: _ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]) -> TensorNetwork

Implement reflected addition.

Returns

TensorNetwork

New lazy calculation; operands are unchanged.

Notes

Scalar zero acts as the additive identity. Other scalars can be added only to rank-zero tensors.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
result = tensor + tensor

Parameters

NameTypeDefaultDescription
lhs_ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]—

Other operand. Component data are retained in a lazy network.

__sub__

Method#
__sub__(rhs: _ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]) -> TensorNetwork

Subtract tensors with compatible external interfaces.

Returns

TensorNetwork

New lazy calculation; operands are unchanged.

Notes

Subtraction requires matching tensor axes; a nonzero scalar cannot be subtracted from a tensor with external axes.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
result = tensor - tensor

Parameters

NameTypeDefaultDescription
rhs_ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]—

Other operand. Component data are retained in a lazy network.

__rsub__

Method#
__rsub__(lhs: _ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]) -> TensorNetwork

Implement reflected subtraction.

Returns

TensorNetwork

New lazy calculation; operands are unchanged.

Notes

Subtraction requires matching tensor axes; a nonzero scalar cannot be subtracted from a tensor with external axes.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
result = tensor - tensor

Parameters

NameTypeDefaultDescription
lhs_ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]—

Other operand. Component data are retained in a lazy network.

__mul__

Method#
__mul__(rhs: _ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]) -> TensorNetwork

Multiply tensors, contracting unambiguous compatible axes.

Returns

TensorNetwork

New lazy calculation; operands are unchanged.

Notes

Matching explicit labels contract. Compatible unresolved axes are paired only when the choice is unambiguous. Use outer(), contract(), or compose() to make the intended pairing explicit.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
result = tensor * 2

Parameters

NameTypeDefaultDescription
rhs_ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]—

Other operand. Component data are retained in a lazy network.

__rmul__

Method#
__rmul__(lhs: _ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]) -> TensorNetwork

Implement reflected multiplication.

Returns

TensorNetwork

New lazy calculation; operands are unchanged.

Notes

Matching explicit labels contract. Compatible unresolved axes are paired only when the choice is unambiguous. Use outer(), contract(), or compose() to make the intended pairing explicit.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
result = 2 * tensor

Parameters

NameTypeDefaultDescription
lhs_ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]—

Other operand. Component data are retained in a lazy network.

__truediv__

Method#
__truediv__(rhs: _ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]) -> TensorNetwork

Divide tensor components by a scalar expression.

Returns

TensorNetwork

New lazy calculation; operands are unchanged.

Notes

The denominator must be scalar. For scalar divided by tensor, the tensor must also have rank zero.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
result = tensor / 2

Parameters

NameTypeDefaultDescription
rhs_ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]—

Other operand. Component data are retained in a lazy network.

__rtruediv__

Method#
__rtruediv__(lhs: _ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]) -> TensorNetwork

Implement reflected division.

Returns

TensorNetwork

New lazy calculation; operands are unchanged.

Notes

The denominator must be scalar. For scalar divided by tensor, the tensor must also have rank zero.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
result = 2 / TensorExpression(3).to_tensor()

Parameters

NameTypeDefaultDescription
lhs_ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]—

Other operand. Component data are retained in a lazy network.

outer

Method#
outer(rhs: _ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]) -> TensorNetwork

Form an outer product without implicit contractions between the operands.

Returns

TensorNetwork

A lazy outer product retaining the operands' component data.

Notes

Use this when compatible unresolved axes should remain independent. Choose explicit distinct labels if you need to refer to them separately.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
product = tensor.outer(tensor)
product.rank

Parameters

NameTypeDefaultDescription
rhs_ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]—

Tensor whose axes follow this tensor's axes.

contract_ports

Method#
contract_ports(rhs: _ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork], *, left: int, right: int) -> TensorNetwork

Contract one chosen pair of axes between two tensors.

Returns

TensorNetwork

A lazy contraction retaining component data.

Notes

Other axes remain external. Axis numbers refer to axes.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
contracted = tensor.contract_ports(tensor, left=1, right=0)
contracted.rank

Parameters

NameTypeDefaultDescription
rhs_ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]—

Tensor to contract with this tensor.

leftint—

Zero-based axis positions in this tensor and rhs, respectively. The representations must be compatible under contraction.

rightint—

Zero-based axis positions in this tensor and rhs, respectively. The representations must be compatible under contraction.

compose

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

Multiply two tensors along explicitly selected matrix channels.

Returns

TensorNetwork

A lazy ordered matrix product retaining component data.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
product = tensor.compose(tensor, left=(0, 1), right=(0, 1))
product.rank

Parameters

NameTypeDefaultDescription
rhs_ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]—

Next factor in the ordered matrix product.

lefttuple[int, int]—

(input_axis, output_axis) in this tensor and rhs, respectively. The left output contracts with the right input. Other axes are retained as spectator axes.

righttuple[int, int]—

(input_axis, output_axis) in this tensor and rhs, respectively. The left output contracts with the right input. Other axes are retained as spectator axes.

dot

Method#
dot(rhs: _ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]) -> TensorNetwork

Contract two rank-one tensors using their representation pairing.

Returns

TensorNetwork

A lazy scalar contraction, including the metric signs of the space.

Examples

from symbolica.community.tensor import TensorName, Tensor, TensorNetwork, Representation
vector = Tensor.dense(TensorName.vector("dot_v")(Representation.euc(2)), [1.0, 2.0])
vector.dot(vector).to_tensor().scalar() == 5

Parameters

NameTypeDefaultDescription
rhs_ScalarInput | TensorExpression | TensorNetwork | Tensor | FactorProjector[TensorExpression] | FactorProjector[TensorNetwork]—

Rank-one tensor in a compatible space.

trace

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

Close a pair of matrix axes on this tensor.

Returns

TensorNetwork

The traced tensor; spectator axes remain external.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor.trace(channel=(0, 1)).is_scalar

Parameters

NameTypeDefaultDescription
channelOptional[tuple[int, int]]None

(input_axis, output_axis) to contract. If omitted, the matrix channel must be uniquely determined by the representations. Specify it when more than one pairing is possible.

__iter__

Method#
__iter__() -> Iterator[Expression | float | complex]

Iterate over all components in logical row-major order.

Returns

iterator of Expression, float, or complex

Includes implicit zeros from sparse storage.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
list(tensor)

__getitem__

Method#

Overloads

Overload 1 #
__getitem__(item: slice) -> list[Expression | complex | float]

Read components in logical row-major order.

Returns

Expression, float, complex, or list

A scalar component, or lists for the sliced axes.

Notes

Coordinates follow axes, the current component view; structure.axes is a canonical signature. The canonical storage-axis order is not exposed by component indexing.

Examples
from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor[:, 1]
tensor[-1]
Parameters
NameTypeDefaultDescription
itemslice—

An integer is a flat position. A tuple gives one selector per axis. A flat slice returns a list; coordinate slices return nested lists. Negative indices count from the end.

Overload 2 #
__getitem__(item: Sequence[int] | int) -> Expression | complex | float

Read components in logical row-major order.

Returns

Expression, float, complex, or list

A scalar component, or lists for the sliced axes.

Notes

Coordinates follow axes, the current component view; structure.axes is a canonical signature. The canonical storage-axis order is not exposed by component indexing.

Examples
from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor[:, 1]
tensor[-1]
Parameters
NameTypeDefaultDescription
itemSequence[int] | int—

An integer is a flat position. A tuple gives one selector per axis. A flat slice returns a list; coordinate slices return nested lists. Negative indices count from the end.

Overload 3 #
__getitem__(item: tuple[int | slice, ...]) -> _Components

Read components in logical row-major order.

Returns

Expression, float, complex, or list

A scalar component, or lists for the sliced axes.

Notes

Coordinates follow axes, the current component view; structure.axes is a canonical signature. The canonical storage-axis order is not exposed by component indexing.

Examples
from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor[:, 1]
tensor[-1]
Parameters
NameTypeDefaultDescription
itemtuple[int | slice, ...]—

An integer is a flat position. A tuple gives one selector per axis. A flat slice returns a list; coordinate slices return nested lists. Negative indices count from the end.

__setitem__

Method#
__setitem__(item: int | Sequence[int], value: Expression | int | complex | float) -> None

Change one stored component in place.

Returns

None

The tensor is modified in place.

Notes

Coordinates follow axes, the current component view; structure.axes is a canonical signature. The canonical storage-axis order is not exposed by component indexing.

Examples

from symbolica.community.tensor import Representation, TensorName, TensorExpression
space = Representation.euc(2)
A = TensorName("M")(space, space)
from symbolica.community.tensor import Tensor
tensor = Tensor.dense(A, [1.0, 2.0, 3.0, 4.0])
tensor[1, 0] = 7.0
tensor[1, 0]

Parameters

NameTypeDefaultDescription
itemint | Sequence[int]—

Flat logical row-major position or one coordinate per logical axis. Negative indices count from the end.

valueExpression | int | complex | float—

Replacement matching the tensor's component type: float for real storage, complex for complex storage, or Expression for symbolic storage.