Skip to content

active_word_log_signature

from tensorsig.offset_coded_signatures.active_word_log import active_word_log_signature

Evaluate independent windows (..., R, C) in the quotient basis.

Source code in src/tensorsig/offset_coded_signatures/active_word_log.py
def active_word_log_signature(
    windows: Tensor, plan: LieQuotientPlan, offset_code: Tensor | None = None
) -> Tensor:
    """Evaluate independent windows ``(..., R, C)`` in the quotient basis."""
    if windows.ndim < 2 or not windows.dtype.is_floating_point:
        raise ValueError("windows must be floating point with shape (..., R, C).")
    if not isinstance(plan, LieQuotientPlan):
        raise TypeError("plan must be a LieQuotientPlan.")
    if windows.shape[-1] != plan.alphabet.in_channels:
        raise ValueError("Input channels disagree with the quotient alphabet.")
    compiled = compile_active_word_log(plan).to(windows.device, windows.dtype)
    if isinstance(plan.alphabet, ProjectedAlphabet):
        if offset_code is None:
            raise ValueError("offset_code is required for a ProjectedAlphabet.")
        state = initialize_word_state(windows, offset_code, compiled.program)
    else:
        if offset_code is not None:
            raise ValueError("offset_code is not used with an OffsetAlphabet.")
        if (
            not isinstance(plan.alphabet, OffsetAlphabet)
            or windows.shape[-2] != plan.alphabet.kernel_size
        ):
            raise ValueError("Window size disagrees with the OffsetAlphabet.")
        zero = windows.flatten(-2)[..., :1] * 0
        state = torch.cat(
            (zero + 1, zero.expand(*zero.shape[:-1], compiled.state_count - 1)), -1
        )
        channels = windows.shape[-1]
        for offset in range(windows.shape[-2]):
            before = windows.new_zeros(*windows.shape[:-2], offset * channels)
            after = windows.new_zeros(
                *windows.shape[:-2], (windows.shape[-2] - offset - 1) * channels
            )
            segment = torch.cat((before, windows[..., offset, :], after), -1)
            state = append_word_state(state, segment, compiled.program)
    return compiled.readout(state)