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
Methodsparse
Static methodsparse(structure: TensorIndices | list[Slot], type_info: type) -> TensorCreate 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
| Name | Type | Default | Description |
|---|---|---|---|
structure | TensorIndices | list[Slot] | — | The tensor structure defining shape and index properties |
type_info | type | — | The data type - either |
dense
Static methoddense(structure: TensorIndices | list[Slot], data: Sequence[Expression] | Sequence[float] | Sequence[complex]) -> TensorCreate 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
| Name | Type | Default | Description |
|---|---|---|---|
structure | TensorIndices | list[Slot] | — | 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() -> TensorCreate 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 methodzero() -> TensorCreate 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
Methodto_dense() -> NoneConvert 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
Methodto_sparse() -> NoneConvert 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__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 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 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 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.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: int | Sequence[int], value: Expression | complex | float) -> NoneSet 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.0Parameters
| Name | Type | Default | Description |
|---|---|---|---|
item | int | Sequence[int] | — | Index specification (int for flat index, list of int for coordinates) |
value | Expression | complex | float | — | The value to set |
evaluator
Methodevaluator(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) -> TensorEvaluatorCreate 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
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
| Name | Type | Default | Description |
|---|---|---|---|
constants | Mapping[Expression, Expression] | — | Dict mapping symbolic expressions to their constant numerical values |
funs | Mapping[tuple[Expression, str, Sequence[Expression]], Expression] | — | Dict mapping function signatures to their symbolic definitions |
params | Sequence[Expression] | — | List of symbolic parameters that will be varied during evaluation |
iterations | int | 100 | Number of optimization iterations for Horner scheme (default: 100) |
n_cores | int | 4 | Number of CPU cores to use for optimization (default: 4) |
verbose | bool | False | Whether to print optimization progress (default: False) |
scalar
Methodscalar() -> ExpressionExtract the scalar value from a rank-0 (scalar) tensor.
Returns
Expression
The scalar expression contained in this tensor
Raises
RuntimeErrorIf the tensor is not a scalar
Examples
from symbolica.community.spenso import Tensor
scalar_tensor = Tensor.one()
value = scalar_tensor.scalar()__iter__
MethodView generated signature source: docs/api/python/spynso3.pyi:594