Skip to content

eulerian_project_columns

from tensorsig.algebra.eulerian import eulerian_project_columns

Apply the first Eulerian idempotent to tensor-word columns.

matrix has shape (width**depth, columns) in lexicographic word order. The columns are independent inputs, and the result has the same shape. Width and depth must be positive integers.

Source code in src/tensorsig/algebra/eulerian.py
def eulerian_project_columns(matrix: Tensor, width: int, depth: int) -> Tensor:
    """Apply the first Eulerian idempotent to tensor-word columns.

    ``matrix`` has shape ``(width**depth, columns)`` in lexicographic word
    order. The columns are independent inputs, and the result has the same
    shape. Width and depth must be positive integers.
    """
    for name, value in (("width", width), ("depth", depth)):
        if not isinstance(value, int) or isinstance(value, bool) or value < 1:
            raise ValueError(f"{name} must be a positive integer.")
    expected_rows = width**depth
    if matrix.ndim != 2 or matrix.shape[0] != expected_rows:
        raise ValueError(
            f"Expected matrix shape ({expected_rows}, columns), got {tuple(matrix.shape)}."
        )
    if not matrix.dtype.is_floating_point:
        raise ValueError("matrix must have a floating-point dtype.")
    tensor = matrix.reshape(*([width] * depth), matrix.shape[1])
    projected = torch.zeros_like(tensor)
    for permutation, coefficient in eulerian_permutations(depth):
        projected = projected + float(coefficient) * tensor.permute(*permutation, depth)
    return projected.reshape(expected_rows, matrix.shape[1])