On this page

TensorLibrary

symbolica.community.spenso Class

TensorLibrary()

A library for registering and managing tensor templates and structures.

The TensorLibrary provides a centralized registry for tensor definitions that can be reused across tensor networks and expressions. It manages tensor structures with their associated names and can resolve symbolic references to registered tensors.

import symbolica
from symbolica.community.spenso import TensorLibrary, LibraryTensor, TensorStructure, Representation

lib = TensorLibrary()
rep = Representation.euc(3)
name = symbolica.S("my_tensor")
structure = TensorStructure(rep, rep, name=name)
tensor = LibraryTensor.dense(structure, [1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0])
lib.register(tensor)
tensor_ref = lib[name]

Constructor

#

Create a new empty tensor library.

Initializes an empty library ready for registering tensor structures. The library automatically manages internal tensor IDs.

Returns

TensorLibrary

A new empty tensor library

Examples

from symbolica.community.spenso import TensorLibrary
lib = TensorLibrary()

Member details

construct

Static method
#
construct() -> TensorLibrary

Create a new empty tensor library (static method).

Initializes an empty library ready for registering tensor structures. The library automatically manages internal tensor IDs.

Returns

TensorLibrary

A new empty tensor library

Examples

from symbolica.community.spenso import TensorLibrary
lib = TensorLibrary.construct()

register

Method
#
register(tensor: Tensor | LibraryTensor) -> None

Register a tensor in the library.

Adds a tensor template to the library that can be referenced by name in tensor networks and symbolic expressions. The tensor must have a name set in its structure.

Examples

import symbolica
from symbolica.community.spenso import TensorLibrary, LibraryTensor, TensorStructure, Representation
lib = TensorLibrary()
rep = Representation.euc(3)
name = symbolica.S("my_tensor")
structure = TensorStructure(rep, rep, name=name)
tensor = LibraryTensor.dense(structure, [1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0])
lib.register(tensor)
tensor_ref = lib[name]

Parameters

NameTypeDefaultDescription
tensorTensor | LibraryTensor

The tensor to register - can be a LibraryTensor or regular Tensor

__getitem__

Method
#
__getitem__(key: Expression | int | str | float | complex | str) -> TensorStructure

Retrieve a registered tensor structure by name.

Looks up a previously registered tensor by its name and returns a reference structure that can be used to create new tensor instances.

Returns

TensorStructure

A TensorStructure representing the registered tensor template

Raises

RuntimeError

If the tensor name is not found in the library

Examples

structure = lib["T"]

Parameters

NameTypeDefaultDescription
keyExpression | int | str | float | complex | str

The tensor name - can be a string or symbolic expression

hep_lib

Static method
#
hep_lib() -> TensorLibrary

Create a library pre-loaded with High Energy Physics tensor definitions.

Returns a library containing standard HEP tensors such as gamma matrices, color generators, metric tensors, and other commonly used structures in particle physics calculations. They are floating point tensors with f64 precision.

Returns

TensorLibrary

A TensorLibrary pre-populated with HEP tensor definitions

Examples

import symbolica
from symbolica.community.spenso import TensorLibrary, TensorName
hep_lib = TensorLibrary.hep_lib()
gamma_structure = hep_lib[symbolica.S("spenso::gamma")]

hep_lib_atom

Static method
#
hep_lib_atom() -> TensorLibrary

Create a library pre-loaded with High Energy Physics tensor definitions.

Returns a library containing standard HEP tensors such as gamma matrices, color generators, metric tensors, and other commonly used structures in particle physics calculations. They are tensors with atom numeric entries.

Returns

TensorLibrary

A TensorLibrary pre-populated with HEP tensor definitions

Examples

import symbolica
from symbolica.community.spenso import TensorLibrary, TensorName
hep_lib = TensorLibrary.hep_lib_atom()
gamma_structure = hep_lib[symbolica.S("spenso::gamma")]