On this page

Tensor

symbolica.community.spenso Class

Tensor()

A tensor class that can be either dense or sparse with flexible data types.

The tensor can store data as floats, complex numbers, or symbolic expressions. Tensors have an associated structure that defines their shape and index properties.

Examples

from symbolica.community.spenso import Tensor, TensorIndices, Representation
structure = TensorIndices(Representation.euc(4)(1))
data = [1.0, 2.0, 3.0, 4.0]
tensor = Tensor.dense(structure, data)
sparse_tensor = Tensor.sparse(structure, float)

Member details

structure

Method
#
structure() -> TensorIndices

No description is available for this member.

sparse

Static method
#
sparse(structure: TensorIndices | list[Slot], type_info: type) -> Tensor

Create a new sparse empty tensor with the given structure and data type.

Returns

Tensor

A new sparse tensor with all elements initially zero

Examples

from symbolica.community.spenso import Tensor, TensorIndices, Representation as R
structure = TensorIndices(R.euc(3)(1), R.euc(3)(2))
sparse_float = Tensor.sparse(structure, float)
sparse_sym = Tensor.sparse(structure, symbolica.Expression)

Parameters

NameTypeDefaultDescription
structureTensorIndices | list[Slot]

The tensor structure defining shape and index properties

type_infotype

The data type - either float or Expression class

dense

Static method
#
dense(structure: TensorIndices | list[Slot], data: Sequence[Expression] | Sequence[float] | Sequence[complex]) -> Tensor

Create a new dense tensor with the given structure and data.

Returns

Tensor

A new dense tensor with the specified data

Examples

from symbolica import S
from symbolica.community.spenso import Tensor, TensorIndices, Representation as R
structure = TensorIndices(R.euc(2)(1), R.euc(2)(2))
data = [1.0, 2.0, 3.0, 4.0]
tensor = Tensor.dense(structure, data)
x, y = S("x", "y")
sym_data = [x, y, x * y, x + y]
sym_tensor = Tensor.dense(structure, sym_data)

Parameters

NameTypeDefaultDescription
structureTensorIndices | list[Slot]

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() -> Tensor

Create a scalar tensor with value 1.0.

Returns

Tensor

A scalar tensor containing the value 1.0

Examples

from symbolica.community.spenso import Tensor
one = Tensor.one()

zero

Static method
#
zero() -> Tensor

Create a scalar tensor with value 0.0.

Returns

Tensor

A scalar tensor containing the value 0.0

Examples

from symbolica.community.spenso import Tensor
zero = Tensor.zero()

to_dense

Method
#
to_dense() -> None

Convert this 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 Tensor, TensorIndices, Representation as R
structure = TensorIndices(R.euc(4)(2))
tensor = Tensor.sparse(structure, float)
tensor[0] = 1.0
tensor.to_dense()

to_sparse

Method
#
to_sparse() -> None

Convert this 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 Tensor, TensorIndices, Representation as R
structure = TensorIndices(R.euc(2)(2), R.euc(2)(1))
data = [1.0, 0.0, 0.0, 2.0]
tensor = Tensor.dense(structure, data)
tensor.to_sparse()

__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 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 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 tensor element(s) at the specified index or indices.

Examples
from symbolica.community.spenso import Tensor, TensorIndices, Representation as R
structure = TensorIndices(R.euc(2)(2), R.euc(2)(1))
tensor = Tensor.sparse(structure, float)
tensor[0] = 4.0
tensor[1, 1] = 1.0
Parameters
NameTypeDefaultDescription
itemAny

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

valueAny

The value to set

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

Set tensor element at the specified index.

Examples
from symbolica.community.spenso import Tensor, TensorIndices, Representation
rep = Representation.euc(2)
structure = TensorIndices(rep(1), rep(2))
tensor = Tensor.sparse(structure, float)
tensor[0] = 1.0
tensor[1, 1] = 2.0
Parameters
NameTypeDefaultDescription
itemint | Sequence[int]

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

valueExpression | complex | float

The value to set

evaluator

Method
#
evaluator(constants: Mapping[Expression, Expression], funs: Mapping[tuple[Expression, str, Sequence[Expression]], Expression], params: Sequence[Expression], iterations: int = 100, n_cores: int = 4, verbose: bool = False) -> TensorEvaluator

Create an optimized evaluator for symbolic tensor expressions.

Compiles the symbolic expressions in this tensor into an optimized evaluation tree that can efficiently compute numerical values for different parameter inputs.

Returns

TensorEvaluator

An optimized evaluator for efficient numerical evaluation

Examples

from symbolica import S
from symbolica.community.spenso import Tensor, TensorIndices, Representation as R
x, y = S("x", "y")
structure = TensorIndices(R.euc(2)(1))
tensor = Tensor.dense(structure, [x * y, x + y])
evaluator = tensor.evaluator(constants={}, funs={}, params=[x, y], iterations=50)
results = evaluator.evaluate_complex([[1.0, 2.0], [3.0, 4.0]])

Parameters

NameTypeDefaultDescription
constantsMapping[Expression, Expression]

Dict mapping symbolic expressions to their constant numerical values

funsMapping[tuple[Expression, str, Sequence[Expression]], Expression]

Dict mapping function signatures to their symbolic definitions

paramsSequence[Expression]

List of symbolic parameters that will be varied during evaluation

iterationsint100

Number of optimization iterations for Horner scheme (default: 100)

n_coresint4

Number of CPU cores to use for optimization (default: 4)

verboseboolFalse

Whether to print optimization progress (default: False)

scalar

Method
#
scalar() -> Expression

Extract the scalar value from a rank-0 (scalar) 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 Tensor
scalar_tensor = Tensor.one()
value = scalar_tensor.scalar()

__iter__

Method
#
__iter__() -> Iterator[Any]

Iterator