LibraryTensor
symbolica.community.spenso Class
LibraryTensor()A library tensor class optimized for use in tensor libraries and networks.
Library tensors are similar to regular tensors but use explicit keys for efficient lookup and storage in tensor libraries. They can be either dense or sparse and store data as floats, complex numbers, or symbolic expressions.
LibraryTensors are designed for:
- Registration in TensorLibrary instances
- Use in tensor networks where structure reuse is important
- Efficient symbolic manipulation and pattern matching
Examples
from symbolica.community.spenso import LibraryTensor, TensorStructure, Representation
rep = Representation.euc(3)
structure = TensorStructure(rep, rep, name="T")
data = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]
tensor = LibraryTensor.dense(structure, data)
sparse_tensor = LibraryTensor.sparse(structure, float)Member details
structure
Methodsparse
Static methodsparse(structure: TensorStructure | list[Representation] | list[int], type_info: type) -> LibraryTensorCreate a new sparse empty library tensor with the given structure and data type.
Creates a sparse tensor that initially contains no non-zero elements. Elements can be set individually using indexing operations.
Returns
LibraryTensor
A new sparse library tensor with all elements initially zero
Examples
import symbolica as sp
from symbolica.community.spenso import LibraryTensor, TensorStructure, Representation
rep = Representation.euc(3)
structure = TensorStructure(rep, rep)
sparse_float = LibraryTensor.sparse(structure, float)
sparse_sym = LibraryTensor.sparse(structure, sp.Expression)
sparse_float[0, 0] = 1.0
sparse_float[1, 1] = 2.0Parameters
| Name | Type | Default | Description |
|---|---|---|---|
structure | TensorStructure | list[Representation] | list[int] | — | The tensor structure defining shape and index properties |
type_info | type | — | The data type - either |
dense
Static methoddense(structure: TensorStructure | list[Representation] | list[int], data: Sequence[Expression] | Sequence[float] | Sequence[complex]) -> LibraryTensorCreate a new dense library tensor with the given structure and data.
Dense tensors store all elements explicitly in row-major order. The structure defines the tensor's shape and indexing properties.
Returns
LibraryTensor
A new dense library tensor with the specified data
Examples
from symbolica import S
from symbolica.community.spenso import LibraryTensor, TensorStructure, Representation
rep = Representation.euc(2)
sigma = S("sigma")
structure = TensorStructure(rep, rep, name=sigma)
data = [0.0, 1.0, 1.0, 0.0]
tensor = LibraryTensor.dense(structure, data)
x, y = S("x", "y")
sym_data = [x, y, -y, x]
sym_tensor = LibraryTensor.dense(structure, sym_data)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
structure | TensorStructure | list[Representation] | list[int] | — | The tensor structure defining shape and index properties |
data | Sequence[Expression] | Sequence[float] | Sequence[complex] | — | The tensor data in row-major order |
one
Static methodone() -> LibraryTensorCreate a scalar library tensor with value 1.0.
Returns
LibraryTensor
A scalar library tensor containing the value 1.0
Examples
from symbolica.community.spenso import LibraryTensor
one = LibraryTensor.one()zero
Static methodzero() -> LibraryTensorCreate a scalar library tensor with value 0.0.
Returns
LibraryTensor
A scalar library tensor containing the value 0.0
Examples
from symbolica.community.spenso import LibraryTensor
zero = LibraryTensor.zero()to_dense
Methodto_dense() -> NoneConvert this library tensor to dense storage format.
Converts sparse tensors to dense format in-place. Dense tensors are unchanged. This allocates memory for all tensor elements.
Examples
from symbolica.community.spenso import LibraryTensor, TensorStructure, Representation
rep = Representation.cof(2)
structure = TensorStructure([rep, rep])
tensor = LibraryTensor.sparse(structure, float)
tensor[0, 0] = 1.0
tensor.to_dense() # Now stores all 4 elements explicitlyto_sparse
Methodto_sparse() -> NoneConvert this library tensor to sparse storage format.
Converts dense tensors to sparse format in-place, only storing non-zero elements. This can save memory for tensors with many zero elements.
Examples
from symbolica.community.spenso import LibraryTensor, TensorStructure, Representation
rep = Representation.euc(2)
structure = TensorStructure(rep, rep)
data = [1.0, 0.0, 0.0, 2.0]
tensor = LibraryTensor.dense(structure, data)
tensor.to_sparse() # Now only stores 2 non-zero elements__repr__
Method__str__
Method__len__
Method__getitem__
MethodOverloads
Overload 1 #
__getitem__(item: slice | int | list[int]) -> AnyNo additional description is available for this overload.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
item | slice | int | list[int] | — | — |
Overload 2 #
__getitem__(item: slice) -> list[Expression | complex | float]Get library tensor elements at the specified range of indices.
Returns
list of float, complex, or Expression
The tensor elements at the specified range
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
item | slice | — | Slice object defining the range of indices |
Overload 3 #
__getitem__(item: Sequence[int] | int) -> Expression | complex | floatGet library tensor element at the specified index or indices.
Returns
float, complex, or Expression
The tensor element at the specified index
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
item | Sequence[int] | int | — | Index specification (int for flat index, list of int for coordinates) |
__setitem__
MethodOverloads
Overload 1 #
__setitem__(item: Any, value: Any) -> NoneSet library tensor element(s) at the specified index or indices.
Examples
from symbolica.community.spenso import LibraryTensor, TensorStructure, Representation
rep = Representation.euc(2)
structure = TensorStructure(rep, rep)
tensor = LibraryTensor.sparse(structure, float)
tensor[0] = 1.0
tensor[1, 1] = 2.0Parameters
| Name | Type | Default | Description |
|---|---|---|---|
item | Any | — | Index specification (int for flat index, list of int for coordinates) |
value | Any | — | The value to set |
Overload 2 #
__setitem__(item: Sequence[int] | int, value: Expression | complex | float) -> NoneSet library tensor element(s) at the specified index or indices.
Examples
from symbolica.community.spenso import LibraryTensor, TensorStructure, Representation
rep = Representation.euc(2)
structure = TensorStructure(rep, rep)
tensor = LibraryTensor.sparse(structure, float)
tensor[0] = 1.0
tensor[1, 1] = 2.0Parameters
| Name | Type | Default | Description |
|---|---|---|---|
item | Sequence[int] | int | — | Index specification (int for flat index, list of int for coordinates) |
value | Expression | complex | float | — | The value to set |
scalar
Methodscalar() -> ExpressionExtract the scalar value from a rank-0 (scalar) library tensor.
Returns
Expression
The scalar expression contained in this tensor
Raises
RuntimeErrorIf the tensor is not a scalar
Examples
from symbolica.community.spenso import LibraryTensor
scalar_tensor = LibraryTensor.one()
value = scalar_tensor.scalar()View generated signature source: docs/api/python/spynso3.pyi:62