Skip to content

star_trace_gram_energy

from tensorsig.offset_coded_signatures.graph_queries import star_trace_gram_energy

Return ST4's exact normalized chronological-log trace squared norm.

There may be any positive number of occurrences of the centre channel. Distinct active leaf channels commute and every leaf depends on the centre. Temporal letters within a channel remain free. Multiply by prod(factorial(n_offset_channel))**2 for the source-image trace norm. ResourceLimit signals refusal, never zero. Leafless boundary controls are also accepted by the same finite logarithm identity. max_entries counts query-live sparse states, retained leaf maps and skeleton weights, and simultaneous contraction input/output polynomials. It is not a bound on Python object bytes or immutable scalar caches.

Source code in src/tensorsig/offset_coded_signatures/_graph/star_gram.py
def star_trace_gram_energy(
    composition,
    occurrence_channels,
    graph,
    *,
    centre_channel,
    max_entries=100_000,
    max_terms=1_000_000,
):
    """Return ST4's exact normalized chronological-log trace squared norm.

    There may be any positive number of occurrences of the centre channel.
    Distinct active leaf channels commute and every leaf depends on the
    centre. Temporal letters within a channel remain free. Multiply by
    ``prod(factorial(n_offset_channel))**2`` for the source-image trace norm.
    ResourceLimit signals refusal, never zero. Leafless boundary controls
    are also accepted by the same finite logarithm identity.
    ``max_entries`` counts query-live sparse states, retained leaf maps and
    skeleton weights, and simultaneous contraction input/output polynomials.
    It is not a bound on Python object bytes or immutable scalar caches.
    """
    parts, channels = validate_cell(composition, occurrence_channels, graph)
    if type(centre_channel) is not int or centre_channel < 0:
        raise ValueError("centre_channel must be a non-negative integer.")
    work = ExactWork(max_entries, max_terms)
    active = set(channels)
    if centre_channel not in active:
        raise ValueError("the cell must contain at least one centre occurrence.")
    leaves = sorted(active - {centre_channel})
    if any(tuple(sorted((centre_channel, leaf))) not in graph.edges for leaf in leaves):
        raise ValueError("the centre must depend on every active leaf channel.")
    if any(edge in graph.edges for edge in combinations(leaves, 2)):
        raise ValueError("distinct active leaf channels must commute.")
    blocks = tuple(i for i, count in enumerate(parts) for _ in range(count))
    counts = {c: [0] * len(parts) for c in active}
    for block, channel in zip(blocks, channels, strict=True):
        counts[channel][block] += 1
    centre_counts = tuple(counts[centre_channel])
    p = sum(centre_counts)
    leaf_degree = len(channels) - p
    occupancies = tuple(tuple(counts[c]) for c in leaves)
    result = Fraction()
    for word in _multiset_words(centre_counts):
        # Only equal centre words can produce equal traces. Discard each word's
        # cache after contraction; the channel word spaces are never multiplied.
        rows = []
        for skeleton in _skeletons(word, len(parts), leaf_degree, work):
            work.acquire()  # Retained skeleton weight; maps own their entries.
            local = tuple(_leaf_map(skeleton, occ, p, work) for occ in occupancies)
            rows.append((skeleton, local))
        for left_index, (left, left_maps) in enumerate(rows):
            for right_index in range(left_index, len(rows)):
                work.step()
                right, right_maps = rows[right_index]
                polynomial = {}
                _add(
                    polynomial, (0,) * (left.symbols + right.symbols), Fraction(1), work
                )
                for a, b in zip(left_maps, right_maps, strict=True):
                    matched = _match(a, b, work)
                    multiplied = _multiply(polynomial, matched, work)
                    work.discard(polynomial)
                    work.discard(matched)
                    polynomial = multiplied
                    if not polynomial:
                        break
                paired = sum(
                    (
                        coefficient
                        * _simplex_moment(left, powers[: left.symbols])
                        * _simplex_moment(right, powers[left.symbols :])
                        for powers, coefficient in polynomial.items()
                    ),
                    Fraction(),
                )
                multiplicity = 1 if left_index == right_index else 2
                result += multiplicity * left.weight * right.weight * paired
                work.discard(polynomial)
        for _, local in rows:
            work.release(1)  # Skeleton weight.
            for row in local:
                for polynomial in row.values():
                    work.discard(polynomial)
        rows.clear()
        del left, right, skeleton, _
    if result < 0:
        raise ArithmeticError("an exact trace Gram norm cannot be negative.")
    return result