TensorLibrary
TensorLibrary
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]Methods
| Name | Description |
|---|---|
__getitem__ |
Retrieve a registered tensor structure by name |
__new__ |
Create a new empty tensor library |
construct |
Create a new empty tensor library (static method) |
hep_lib |
Create a library pre-loaded with High Energy Physics tensor definitions |
hep_lib_atom |
Create a library pre-loaded with High Energy Physics tensor definitions |
register |
Register a tensor in the library |
__getitem__
TensorLibrary.__getitem__(key: Expression | int | str | float | builtins.complex | builtins.str) -> TensorStructureRetrieve 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.
Parameters
key(str or Expression) The tensor name - can be a string or symbolic expression
Returns
TensorStructureA TensorStructure representing the registered tensor template
Raises
RuntimeError: If the tensor name is not found in the library
Examples
structure = lib["T"]__new__
TensorLibrary.__new__() -> TensorLibraryCreate a new empty tensor library.
Initializes an empty library ready for registering tensor structures. The library automatically manages internal tensor IDs.
Returns
TensorLibraryA new empty tensor library
Examples
from symbolica.community.spenso import TensorLibrary
lib = TensorLibrary()construct
TensorLibrary.construct() -> TensorLibraryCreate a new empty tensor library (static method).
Initializes an empty library ready for registering tensor structures. The library automatically manages internal tensor IDs.
Returns
TensorLibraryA new empty tensor library
Examples
from symbolica.community.spenso import TensorLibrary
lib = TensorLibrary.construct()hep_lib
TensorLibrary.hep_lib() -> TensorLibraryCreate 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
TensorLibraryA 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
TensorLibrary.hep_lib_atom() -> TensorLibraryCreate 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
TensorLibraryA 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")]register
TensorLibrary.register(tensor: Tensor | LibraryTensor) -> NoneRegister 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.
Parameters
tensor(LibraryTensor or Tensor) The tensor to register - can be a LibraryTensor or regular Tensor
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]