AELYRION-NOULITH-III / src /noulith /protocols.py
PureOne's picture
Add AELYRION-NOULITH III research v3.0.0 (hf.1)
d1c6c9a verified
Raw History Blame Contribute Delete
4.74 kB
"""Block protocols, their exact ideal generators, and valuation optimization."""
from __future__ import annotations
from functools import lru_cache
from math import inf
from typing import Sequence
import sympy as sp
@lru_cache(maxsize=None)
def partitions(mask: int, budget: int) -> tuple[tuple[int, ...], ...]:
"""Unordered set partitions into at most budget blocks, canonically ordered.
The first block contains the least element. Different block orders multiply
isolated rows by analytic units, so they generate the same local ideals.
"""
if not isinstance(mask, int) or not isinstance(budget, int) or mask < 0 or budget < 0:
raise ValueError("mask and budget must be nonnegative integers")
if mask == 0:
return ((),)
if budget == 0:
return ()
pivot = mask & -mask
remainder = mask ^ pivot
out: list[tuple[int, ...]] = []
extra = remainder
while True:
first = pivot | extra
for tail in partitions(mask ^ first, budget - 1):
out.append((first,) + tail)
if extra == 0:
break
extra = (extra - 1) & remainder
return tuple(out)
def ideal_generators(shuffles: Sequence[sp.Expr], mask: int, budget: int) -> list[sp.Expr]:
"""Normalized generators; normalization divides only by braid units."""
if not 0 <= mask < len(shuffles):
raise ValueError("mask exceeds shuffle table")
return [sp.expand(sp.prod(shuffles[b] for b in part)) for part in partitions(mask, budget)]
def valuation(expr: sp.Expr, variable: sp.Symbol) -> int | float:
"""Order at zero of a rational expression with exact coefficients.
Supports rational functions, not arbitrary analytic black boxes. The caller
is responsible for exact coefficient simplification in extension fields.
"""
expr = sp.cancel(expr)
if expr == 0:
return inf
numerator, denominator = sp.fraction(expr)
p, q = sp.Poly(numerator, variable), sp.Poly(denominator, variable)
vp = min(term[0][0] for term in p.terms())
vq = min(term[0][0] for term in q.terms())
return int(vp - vq)
def partition_valuations(weights: Sequence[int | float], budget: int) -> tuple[list[int | float], list[tuple[int, ...] | None]]:
"""Minimum sum over partitions, with certificates, in O(r 3^m) arithmetic."""
n = len(weights)
if n == 0 or n & (n - 1):
raise ValueError("weights must have length 2^m")
if not isinstance(budget, int) or isinstance(budget, bool) or budget < 0:
raise ValueError("budget must be a nonnegative integer")
if any(w < 0 for w in weights):
raise ValueError("weights must be nonnegative or infinity")
previous = [inf] * n
previous[0] = 0
witnesses: list[tuple[int, ...] | None] = [None] * n
witnesses[0] = ()
for _ in range(budget):
current = previous.copy()
current_witness = witnesses.copy()
for mask in range(1, n):
pivot = mask & -mask
rest = mask ^ pivot
extra = rest
while True:
block = pivot | extra
remainder = mask ^ block
candidate = weights[block] + previous[remainder]
if candidate < current[mask]:
current[mask] = candidate
tail = witnesses[remainder]
assert tail is not None
current_witness[mask] = (block,) + tail
if extra == 0:
break
extra = (extra - 1) & rest
previous, witnesses = current, current_witness
return previous, witnesses
def root_profile(degree: int, blocks: int, order: int) -> int | float:
"""Exact exponent for q(t)=zeta*(1+t), zeta primitive of order ell>=2."""
if any(not isinstance(v, int) or isinstance(v, bool) for v in (degree, blocks, order)):
raise ValueError("degree, blocks, and order must be integers")
if degree < 0 or blocks < 0 or order < 2:
raise ValueError("degree>=0, blocks>=0, and order>=2 are required")
if degree == 0:
return 0
if blocks == 0:
return inf
numerator = degree - blocks * (order - 1)
return max(0, (numerator + order - 1) // order)
def normalized_measurement_matrix(shuffles: Sequence[sp.Expr], budget: int) -> sp.Matrix:
"""Finite coefficient/isolation measurement frame, with one supported row."""
n = len(shuffles)
full = n - 1
rows = []
for mask in range(n):
for coefficient in ideal_generators(shuffles, mask, budget):
row = [sp.S.Zero] * n
row[full ^ mask] = coefficient
rows.append(row)
return sp.Matrix(rows) if rows else sp.zeros(0, n)