Skip to content

unflatten_signature_levels

from tensorsig.tensor_ops.series import unflatten_signature_levels

Reshape flattened tensor-series blocks into homogeneous levels.

Parameters:

Name Type Description Default
sig Tensor

Tensor with arbitrary leading dimensions and trailing dimension width + width**2 + ... + width**depth.

required
width int

Tensor-algebra width.

required
depth int

Maximum series degree.

required

Returns:

Type Description
list[Tensor]

Level m - 1 has shape (*sig.shape[:-1], *([width] * m)).

Source code in src/tensorsig/tensor_ops/series.py
def unflatten_signature_levels(sig: Tensor, width: int, depth: int) -> list[Tensor]:
    """Reshape flattened tensor-series blocks into homogeneous levels.

    Parameters
    ----------
    sig : Tensor
        Tensor with arbitrary leading dimensions and trailing dimension
        ``width + width**2 + ... + width**depth``.
    width : int
        Tensor-algebra width.
    depth : int
        Maximum series degree.

    Returns
    -------
    list[Tensor]
        Level ``m - 1`` has shape
        ``(*sig.shape[:-1], *([width] * m))``.
    """
    width = _positive_integer(width, "width")
    depth = _positive_integer(depth, "depth")
    if sig.ndim < 1:
        raise ValueError("Signature tensor must have a trailing feature dimension.")

    expected = sum(_signature_level_sizes(width, depth))
    if sig.shape[-1] != expected:
        raise ValueError(
            f"Expected trailing dimension {expected}, got {sig.shape[-1]}."
        )

    leading_shape = sig.shape[:-1]
    levels: list[Tensor] = []
    offset = 0
    for current_depth in range(1, depth + 1):
        size = width**current_depth
        chunk = sig[..., offset : offset + size]
        levels.append(chunk.reshape(*leading_shape, *([width] * current_depth)))
        offset += size
    return levels