Spenso · latest
On this page

TensorLibrary

symbolica.community.tensor Class

TensorLibrary()

Reusable component definitions indexed by tensor name and signature.

Register a named Tensor, then use its symbolic expression in calculations with this library. A full signature includes the name, scalar arguments, and representations. A name alone is a shortcut when it identifies one entry.

Lookup returns independent component data. Enumeration lists stored entries; dimension-dependent factories, such as metrics, are resolved on demand.

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])
from symbolica.community.tensor import TensorLibrary
library = TensorLibrary()
library.register(tensor)
library[A.name][1, 0]

Constructor

#

Create an empty tensor component library.

Examples

from symbolica.community import tensor as sp
library = sp.TensorLibrary()

Returns

TensorLibrary

Library ready to accept named tensors through register().

Examples

from symbolica.community.tensor import TensorLibrary
library = TensorLibrary()
len(library)

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

to_html

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

Display stored tensors with on-demand component and construction details.

Returns

str

HTML fragment for display in a notebook or page.

Notes

Stored entries can be selected to inspect the same component explorer as a standalone Tensor. On-demand factories are listed 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])
from symbolica.community.tensor import TensorLibrary
library = TensorLibrary()
library.register(tensor)
value = library
html = value.to_html()

Parameters

NameTypeDefaultDescription
settingsOptional[DisplaySettings]None

Index and tensor presentation choices; defaults to DisplaySettings().

_repr_html_

Method#
_repr_html_() -> Optional[str]

No description is available for this member.

construct

Static method#
construct() -> TensorLibrary

Create an empty tensor component library.

Examples

from symbolica.community import tensor as sp
library = sp.TensorLibrary.construct()

Returns

TensorLibrary

Library ready to accept named tensors through register().

Examples

from symbolica.community.tensor import TensorLibrary
library = TensorLibrary.construct()
len(library)

register

Method#
register(tensor: Tensor) -> None

Store an independent copy of a named tensor's components.

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])
library = sp.TensorLibrary()
library.register(tensor)
stored = library[A]

Returns

None

Adds or replaces the entry for this exact signature.

Notes

Changing the original tensor afterward does not change this library. Register a modified tensor again to update its stored definition.

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])
from symbolica.community.tensor import TensorLibrary
library = TensorLibrary()
library.register(tensor)
len(library)

Parameters

NameTypeDefaultDescription
tensorTensor—

Tensor with a name, scalar key arguments, concrete dimensions, and fully unresolved external axes (no explicit index labels). Use tensor.with_name(...) if the tensor is unnamed.

__getitem__

Method#
__getitem__(key: TensorExpression | TensorName | Expression | str) -> Tensor

Retrieve a tensor by exact signature or unambiguous name.

Returns

Tensor

An independent copy in the requested logical axis order. Call its expression() method for a symbolic reference.

Raises

KeyError

The key is missing or the name matches more than one stored signature.

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])
from symbolica.community.tensor import TensorLibrary
library = TensorLibrary()
library.register(tensor)
stored = library[A]
stored[0, 1]

Parameters

NameTypeDefaultDescription
keyTensorExpression | TensorName | Expression | str—

A fully unresolved tensor expression selects an exact signature. A TensorName, symbol, or string selects a name only when exactly one stored signature has that name. Exact signatures also select factories. Strings without a namespace use "spenso_python"; prefer TensorName or a fully qualified string when the symbol uses another namespace.

__contains__

Method#
__contains__(key: TensorExpression | TensorName | Expression | str) -> bool

Check whether a stored signature or factory can resolve a key.

Returns

bool

False for a missing key. Factories are checked without constructing their component data.

Notes

An ambiguous name or invalid key raises the same error as lookup.

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])
from symbolica.community.tensor import TensorLibrary
library = TensorLibrary()
library.register(tensor)
A in library

Parameters

NameTypeDefaultDescription
keyTensorExpression | TensorName | Expression | str—

A fully unresolved tensor expression selects an exact signature. A TensorName, symbol, or string selects a name only when exactly one stored signature has that name. Exact signatures also select factories. Strings without a namespace use "spenso_python"; prefer TensorName or a fully qualified string when the symbol uses another namespace.

get

Method#

Overloads

Overload 1 #
get(key: TensorExpression | TensorName | Expression | str, default: None = None) -> Tensor | None

Retrieve a tensor, returning a default when its signature is absent.

Returns

Tensor or object

Independent tensor snapshot, or the supplied default.

Notes

Ambiguous names and invalid keys still raise; they do not select the default.

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])
from symbolica.community.tensor import TensorLibrary
library = TensorLibrary()
library.register(tensor)
library.get("missing_tensor") is None
Parameters
NameTypeDefaultDescription
keyTensorExpression | TensorName | Expression | str—

A fully unresolved tensor expression selects an exact signature. A TensorName, symbol, or string selects a name only when exactly one stored signature has that name. Exact signatures also select factories. Strings without a namespace use "spenso_python"; prefer TensorName or a fully qualified string when the symbol uses another namespace.

defaultNoneNone

Value returned only when the signature is missing.

Overload 2 #
get(key: TensorExpression | TensorName | Expression | str, default: _LibraryDefault) -> Tensor | _LibraryDefault

Retrieve a tensor, returning a default when its signature is absent.

Returns

Tensor or object

Independent tensor snapshot, or the supplied default.

Notes

Ambiguous names and invalid keys still raise; they do not select the default.

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])
from symbolica.community.tensor import TensorLibrary
library = TensorLibrary()
library.register(tensor)
library.get("missing_tensor") is None
Parameters
NameTypeDefaultDescription
keyTensorExpression | TensorName | Expression | str—

A fully unresolved tensor expression selects an exact signature. A TensorName, symbol, or string selects a name only when exactly one stored signature has that name. Exact signatures also select factories. Strings without a namespace use "spenso_python"; prefer TensorName or a fully qualified string when the symbol uses another namespace.

default_LibraryDefault—

Value returned only when the signature is missing.

__len__

Method#
__len__() -> int

Count stored tensor definitions.

Returns

int

Number of stored entries; excludes on-demand factories.

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])
from symbolica.community.tensor import TensorLibrary
library = TensorLibrary()
library.register(tensor)
len(library)

keys

Method#
keys() -> list[TensorExpression]

List the full unresolved signatures of stored tensors.

Returns

list of TensorExpression

Snapshot in name/signature order, excluding on-demand factories.

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])
from symbolica.community.tensor import TensorLibrary
library = TensorLibrary()
library.register(tensor)
len(library.keys())

values

Method#
values() -> list[Tensor]

List independent copies of the stored tensors.

Returns

list of Tensor

Snapshot in the same order as keys(), excluding on-demand factories.

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])
from symbolica.community.tensor import TensorLibrary
library = TensorLibrary()
library.register(tensor)
len(library.values())

items

Method#
items() -> list[tuple[TensorExpression, Tensor]]

List stored tensor signatures together with their component data.

Returns

list of (TensorExpression, Tensor)

Independent snapshots in the same order as keys().

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])
from symbolica.community.tensor import TensorLibrary
library = TensorLibrary()
library.register(tensor)
len(library.items())

__iter__

Method#
__iter__() -> Iterator[TensorExpression]

Iterate over a snapshot of stored tensor signatures.

Returns

iterator of TensorExpression

Same entries and ordering as keys().

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])
from symbolica.community.tensor import TensorLibrary
library = TensorLibrary()
library.register(tensor)
len(list(library))

hep_lib

Static method#
hep_lib() -> TensorLibrary

Load standard Dirac and SU(3) tensors with floating-point components.

Examples

from symbolica.community import tensor as sp
library = sp.TensorLibrary.hep_lib()
gamma = sp.TensorExpression.dirac_gamma(4)
components = gamma.to_tensor(library)

Returns

TensorLibrary

Independent library with four-dimensional Dirac matrices in the Weyl basis, SU(3) color tensors, and dimension-dependent metric factories.

Notes

Values use double-precision real or complex numbers. This explicitly selects numerical components; omitted HEP libraries and hep_lib_atom() use exact Symbolica expressions.

Examples

from symbolica.community.tensor import TensorLibrary, TensorExpression
library = TensorLibrary.hep_lib()
library[TensorExpression.dirac_gamma(4)].shape

hep_lib_atom

Static method#
hep_lib_atom() -> TensorLibrary

Load standard Dirac and SU(3) tensors with exact symbolic components.

Examples

from symbolica.community import tensor as sp
library = sp.TensorLibrary.hep_lib_atom()
gamma = sp.TensorExpression.dirac_gamma(4)
components = gamma.to_tensor(library)

Returns

TensorLibrary

Independent library with four-dimensional Dirac matrices in the Weyl basis, SU(3) color tensors, and dimension-dependent metric factories.

Notes

Components are Symbolica Expressions, retaining exact rational and algebraic constants for symbolic component calculations. This is the same component convention used when tensor-network evaluation omits a library. Use hep_lib() to request floating-point HEP data explicitly.

Examples

from symbolica.community.tensor import TensorLibrary, TensorExpression
library = TensorLibrary.hep_lib_atom()
library[TensorExpression.dirac_gamma(4)].shape