On this page

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

Method
#
structure() -> TensorStructure

No description is available for this member.

sparse

Static method
#
sparse(structure: TensorStructure | list[Representation] | list[int], type_info: type) -> LibraryTensor

Create 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.0

Parameters

NameTypeDefaultDescription
structureTensorStructure | list[Representation] | list[int]

The tensor structure defining shape and index properties

type_infotype

The data type - either float or Expression class

dense

Static method
#
dense(structure: TensorStructure | list[Representation] | list[int], data: Sequence[Expression] | Sequence[float] | Sequence[complex]) -> LibraryTensor

Create 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

NameTypeDefaultDescription
structureTensorStructure | list[Representation] | list[int]

The tensor structure defining shape and index properties

dataSequence[Expression] | Sequence[float] | Sequence[complex]

The tensor data in row-major order

one

Static method
#
one() -> LibraryTensor

Create 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 method
#
zero() -> LibraryTensor

Create 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

Method
#
to_dense() -> None

Convert 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 explicitly

to_sparse

Method
#
to_sparse() -> None

Convert 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
#
__repr__() -> str

No description is available for this member.

__str__

Method
#
__str__() -> str

No description is available for this member.

__len__

Method
#
__len__() -> int

No description is available for this member.

__getitem__

Method
#

Overloads

Overload 1 #
__getitem__(item: slice | int | list[int]) -> Any

No additional description is available for this overload.

Parameters
NameTypeDefaultDescription
itemslice | 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
NameTypeDefaultDescription
itemslice

Slice object defining the range of indices

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

Get library tensor element at the specified index or indices.

Returns

float, complex, or Expression

The tensor element at the specified index

Parameters
NameTypeDefaultDescription
itemSequence[int] | int

Index specification (int for flat index, list of int for coordinates)

__setitem__

Method
#

Overloads

Overload 1 #
__setitem__(item: Any, value: Any) -> None

Set 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.0
Parameters
NameTypeDefaultDescription
itemAny

Index specification (int for flat index, list of int for coordinates)

valueAny

The value to set

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

Set 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.0
Parameters
NameTypeDefaultDescription
itemSequence[int] | int

Index specification (int for flat index, list of int for coordinates)

valueExpression | complex | float

The value to set

scalar

Method
#
scalar() -> Expression

Extract the scalar value from a rank-0 (scalar) library tensor.

Returns

Expression

The scalar expression contained in this tensor

Raises

RuntimeError

If the tensor is not a scalar

Examples

from symbolica.community.spenso import LibraryTensor
scalar_tensor = LibraryTensor.one()
value = scalar_tensor.scalar()