TensorStructure
TensorStructure
TensorStructure()A tensor structure without abstract indices, defined purely by representations.
TensorStructure represents the shape and representation structure of tensors without specific index assignments. It’s used for defining tensor templates in libraries and for creating indexless tensor computations.
Examples
from symbolica.community.spenso import TensorStructure, Representation, TensorName
# Create from representations
rep = Representation.euc(3)
structure = TensorStructure(rep, rep) # 3x3 matrix structure
# With name for library registration
T = TensorName("T")
named_structure = TensorStructure(rep, rep, name=T)
# Use to create indexed tensor
indices = structure.index('mu', 'nu') # Assign specific indices
# Create symbolic expression
expr = structure.symbolic('a', 'b') # T(a, b)Methods
| Name | Description |
|---|---|
__call__ |
Convenience method for creating symbolic expressions |
__getitem__ |
Get expanded indices at the specified range of flattened indices. |
__len__ |
|
__new__ |
Construct a new TensorStructure with the given representations. |
__repr__ |
|
__str__ |
|
get_name |
|
index |
Create an indexed tensor (TensorIndices) from this structure |
set_name |
|
symbolic |
Create a symbolic expression representing this tensor structure |
__call__
TensorStructure.__call__(
*args: builtins.int | Expression | str,
extra_args: typing.Sequence[Expression | int | str | float | builtins.complex] = [],
) -> ExpressionConvenience method for creating symbolic expressions.
This is a shorthand for calling symbolic(*args, extra_args=extra_args). Creates a symbolic Expression representing this tensor structure.
Parameters
*args(int, str, Symbol, or Expression) Positional arguments (indices and additional args)extra_args(list of Expression, optional) Optional list of additional non-tensorial arguments
Returns
ExpressionA symbolic Expression representing the tensor
Examples
structure = TensorStructure(rep, rep, name="T")
expr = structure('mu', 'nu')__getitem__
__getitem__ has 3 variants:
__getitem__ with item: builtins.slice
TensorStructure.__getitem__(item: builtins.slice) -> builtins.list[Expression | builtins.complex | float]Get expanded indices at the specified range of flattened indices.
Parameters
item(slice) Slice object defining the range of indices
Returns
list of list of intList of expanded indices
__getitem__ with item: typing.Sequence[builtins.int]
TensorStructure.__getitem__(item: typing.Sequence[builtins.int]) -> Expression | builtins.complex | floatGet flattened index associated to this expanded index.
Parameters
item(list of int) Multi-dimensional index coordinates
Returns
intThe flat index
__getitem__ with item: builtins.int
TensorStructure.__getitem__(item: builtins.int) -> Expression | builtins.complex | floatGet expanded index associated to this flat index.
Parameters
item(int) Flat index into the tensor
Returns
list of intMulti-dimensional index coordinates
__len__
TensorStructure.__len__() -> builtins.int__new__
TensorStructure.__new__(
*reps_and_additional_args: TensorIndices | builtins.list[Slot],
name: TensorName | builtins.str | Expression | None = None,
) -> TensorStructureConstruct a new TensorStructure with the given representations.
Parameters
*reps_and_additional_args(Representation or Expression) Mixed arguments (Representation objects and Expressions for additional arguments)name(TensorName, optional) Optional tensor name to assign to the structure
Returns
TensorStructureA new TensorStructure object
Examples
from symbolica import S
from symbolica.community.spenso import TensorStructure, Representation, TensorName
rep = Representation.euc(3)
structure = TensorStructure(rep, rep)
x = S('x')
structure_with_args = TensorStructure(rep, rep, x)
T = TensorName("T")
named_structure = TensorStructure(rep, rep, name=T)__repr__
TensorStructure.__repr__() -> builtins.str__str__
TensorStructure.__str__() -> builtins.strget_name
TensorStructure.get_name() -> typing.Optional[TensorName]index
TensorStructure.index(
*args: builtins.int | Expression | str,
extra_args: typing.Sequence[Expression] = [],
cook_indices: builtins.bool = False,
) -> TensorIndicesCreate an indexed tensor (TensorIndices) from this structure.
Converts this structure template into a concrete indexed tensor by assigning specific abstract indices to each representation slot.
Parameters
*args(int, str, Symbol, Expression, or ‘;’) Positional arguments (indices and ‘;’ separator between additional args and indices)extra_args(list of Expression, optional) Optional list of additional non-tensorial argumentscook_indices(bool, optional) If True, attempt to convert expressions to valid indices
Returns
TensorIndicesA TensorIndices object with concrete index assignments
Examples
import symbolica as sp
from symbolica.community.spenso import TensorStructure, Representation, TensorName
rep = Representation.cof(3)
T = TensorName("T")
structure = TensorStructure([rep, rep], name=T)
indices = structure.index('mu', 'nu')
x = sp.S('x')
indices = structure.index(x, ';', 'mu', 'nu')set_name
TensorStructure.set_name(name: TensorName | builtins.str | Expression) -> Nonesymbolic
TensorStructure.symbolic(
*args: builtins.int | Expression | str,
extra_args: typing.Sequence[Expression | int | str | float | builtins.complex] = [],
) -> ExpressionCreate a symbolic expression representing this tensor structure.
Builds a symbolic tensor expression with the specified indices. Arguments can be separated using a semicolon (‘;’) to distinguish between additional arguments and tensor indices.
Parameters
*args(int, str, Symbol, Expression, or ‘;’) Positional arguments (int, str, Symbol, Expression for indices, ‘;’ for separator)extra_args(list of Expression, optional) Optional list of additional non-tensorial arguments
Returns
ExpressionA symbolic Expression representing the tensor with indices
Examples
import symbolica as sp
from symbolica.community.spenso import TensorStructure, Representation, TensorName
rep = Representation.euc(3)
T = TensorName("T")
structure = TensorStructure([rep, rep], name=T)
expr = structure.symbolic('mu', 'nu')
x = sp.S('x')
expr = structure.symbolic(x, ';', 'mu', 'nu')
expr = structure.symbolic('mu', 'nu', extra_args=[x])