Skip to content

tree_moment

from tensorsig.offset_coded_signatures.graph_queries import tree_moment

Return one exact tree moment using the cheapest supported backend.

Universal-zero compositions return immediately. Terminal-singleton blocks use the exact Bernoulli hook product. Other small Young groups use the explicit factorial oracle; larger groups use the proved root-rank DP. metrics is populated only when the DP is selected.

Source code in src/tensorsig/offset_coded_signatures/_graph/adaptive.py
def adaptive_tree_moment(
    composition: Sequence[int],
    reference_covers: Iterable[tuple[int, int]],
    *,
    metrics: YoungDpMetrics | None = None,
) -> Fraction:
    """Return one exact tree moment using the cheapest supported backend.

    Universal-zero compositions return immediately. Terminal-singleton blocks
    use the exact Bernoulli hook product. Other small Young groups use the
    explicit factorial oracle; larger groups use the proved root-rank DP.
    ``metrics`` is populated only when the DP is selected.
    """
    parts = _validate_composition(composition)
    _validate_metrics(metrics)
    degree = sum(parts)
    tree = _prepare_tree(reference_covers, degree)
    backend = _tree_moment_backend(parts)
    if backend == "zero":
        return Fraction()
    if backend == "terminal_hook":
        return _terminal_hook_moment(tree, degree)
    if backend == "young_dp":
        derivative = _young_tree_derivative_dp(parts, tree, metrics)
    else:
        derivative = _young_averaged_derivative(parts, tree)
    return (1 << (degree - 1)) * derivative