#!/usr/bin/env python3 """Exact capped numerator J_cap for the source's restricted support. J_cap = k * int_{s(X) <= ell} ( int_0^{h(X)} F(X,t) dt )^2 dX , with the deleted-fiber limit h(X) of eprint 2026/1893 Lemma 4.20, computed via the Appendix-B correction decomposition J_cap = J_0 + E_C + E_D + E_E , E_K = sum_r sum_j (-1)^j int_{P^{K,r,j}} P_K^{r,j} , P_{C,r} = T(X, b-R_r)^2 - 2 G T(X, b-R_r), P_{D,r} = T(X, d)^2 - 2 G T(X, d), P_{E,r} = -G(X)^2, G the full marginal (Eq. 91), T(X,c) = int_c^H F (Eq. 92), and n = k-1 the base dimension. The candidate needs the base cutoff imposed on C_r and D_r; see `derivation.md`. All arithmetic is exact. """ from __future__ import annotations from collections import defaultdict from fractions import Fraction as Fr from math import comb, factorial from capped_moment import (_distribute, expanded_rough, shifted_small, prod_fact, mult, emb) from capped_forms import mul_m # --------------------------------------------------------------------------- # polynomials in (Y, Z, j) -> {(i,k,l): Fraction} # --------------------------------------------------------------------------- def p_add(a: dict, b: dict, sc: Fr = Fr(1)) -> dict: out = dict(a) for kk, v in b.items(): nv = out.get(kk, Fr(0)) + sc * v if nv: out[kk] = nv elif kk in out: del out[kk] return out def p_mul(a: dict, b: dict) -> dict: out: dict = {} for ka, va in a.items(): for kb, vb in b.items(): kk = (ka[0] + kb[0], ka[1] + kb[1], ka[2] + kb[2]) out[kk] = out.get(kk, Fr(0)) + va * vb return {k: v for k, v in out.items() if v} def p_const(c, nvar=3) -> dict: return {(0,) * nvar: Fr(c)} def p_pow(a: dict, e: int) -> dict: r = p_const(1) for _ in range(e): r = p_mul(r, a) return r def p_subst_j(a: dict, j: int) -> dict: out: dict = {} for (i, kk, l), v in a.items(): out[(i, kk)] = out.get((i, kk), Fr(0)) + v * (j ** l) return {k: v for k, v in out.items() if v} def p_scalar(a: dict, s: Fr) -> dict: return {k: v * s for k, v in a.items() if v * s} # --------------------------------------------------------------------------- # marginals # --------------------------------------------------------------------------- def signature_G(F_terms): L = defaultdict(Fr) for (cc, a, lam) in F_terms: lam = tuple(lam) L[(lam, a + 1)] += cc * Fr(1, a + 1) for q in sorted(set(lam)): mu = list(lam) mu.remove(q) L[(tuple(sorted(mu, reverse=True)), a + q + 1)] += \ cc * Fr(factorial(q) * factorial(a), factorial(a + q + 1)) return {k: v for k, v in L.items() if v} def signature_T(F_terms): L = defaultdict(Fr) for (cc, a, lam) in F_terms: lam = tuple(lam) for q in sorted({0} | set(lam)): if q == 0: mu = lam else: lst = list(lam) lst.remove(q) mu = tuple(sorted(lst, reverse=True)) for h in range(q + 1): L[(mu, q - h, a + h + 1)] += \ cc * Fr(comb(q, h) * factorial(a) * factorial(h), factorial(a + h + 1)) return {k: v for k, v in L.items() if v} _mul_cache: dict = {} def _mul(m1, m2): key = (m1, m2) if m1 <= m2 else (m2, m1) if key not in _mul_cache: _mul_cache[key] = mul_m(key[0], key[1]) return _mul_cache[key] def sig_square(LG): keys = list(LG.items()) out = defaultdict(Fr) for i in range(len(keys)): (m1, p1), c1 = keys[i] for j in range(i, len(keys)): (m2, p2), c2 = keys[j] w = c1 * c2 * (1 if i == j else 2) if w == 0: continue for nu, cc in _mul(m1, m2).items(): out[(nu, p1 + p2)] += w * cc return {k: v for k, v in out.items() if v} def sig_GT(LG, LT): out = defaultdict(Fr) for (m1, p), c1 in LG.items(): for (m2, cp, hc), c2 in LT.items(): w = c1 * c2 if w == 0: continue for nu, cc in _mul(m1, m2).items(): out[(nu, p, cp, hc)] += w * cc return {k: v for k, v in out.items() if v} def sig_T2(LT): keys = list(LT.items()) out = defaultdict(Fr) for i in range(len(keys)): (m1, cp1, hc1), c1 = keys[i] for j in range(i, len(keys)): (m2, cp2, hc2), c2 = keys[j] w = c1 * c2 * (1 if i == j else 2) if w == 0: continue for nu, cc in _mul(m1, m2).items(): out[(nu, cp1 + cp2, hc1 + hc2)] += w * cc return {k: v for k, v in out.items() if v} # --------------------------------------------------------------------------- # shift expansion # --------------------------------------------------------------------------- _exp_cache: dict = {} def nu_expansion(r, s, d, nu): """[(gp, hp, j, coeff)] with coeff = count * expansion weights * density.""" key = (r, s, d, tuple(nu)) if key in _exp_cache: return _exp_cache[key] n = r + s num = mult(nu) ell = sum(num.values()) if ell > n: _exp_cache[key] = [] return [] embn = emb(n, num) out = [] for u_dist, dcoef in _distribute(num): u = sum(u_dist.values()) if u > r: continue g_mult = {q: uq for q, uq in u_dist.items() if uq > 0} h_mult = {q: num[q] - u_dist[q] for q in num if num[q] - u_dist[q] > 0} prof = embn * comb(n - ell, r - u) * dcoef if prof == 0: continue Gr = expanded_rough(g_mult, d) if r >= 1 else {(): Fr(1)} Gs = shifted_small(h_mult, s, d) for gp, cg in Gr.items(): for (jj, hp), ch in Gs.items(): if ch == 0: continue if r >= 1: dens = Fr(prod_fact(gp), factorial(r - 1 + sum(gp))) else: dens = Fr(1) if s >= 1: dens *= Fr(prod_fact(hp), factorial(s - 1 + sum(hp))) out.append((gp, hp, jj, prof * cg * ch * dens)) _exp_cache[key] = out return out # --------------------------------------------------------------------------- # polygons # --------------------------------------------------------------------------- def yline_integrate(Aj: dict, a: int, planes): """Integral over Z=0 of A(Y,0) Y^a dY, Y from the polygon's Y-interval. Used only when the small block is empty (s = 0).""" lo, hi = None, None for (A, B, C) in planes: if A == 0: if C < 0: return Fr(0) continue v = Fr(C, A) if A > 0: hi = v if hi is None else min(hi, v) else: lo = v if lo is None else max(lo, v) if lo is None: lo = Fr(0) if hi is None or lo >= hi: return Fr(0) tot = Fr(0) for (i, kk), co in Aj.items(): if kk != 0: continue e = i + a tot += co * (hi ** (e + 1) - lo ** (e + 1)) / (e + 1) return tot def polygon_vertices(planes): n = len(planes) pts = [] for i in range(n): a1, b1, c1 = planes[i] for j in range(i + 1, n): a2, b2, c2 = planes[j] det = a1 * b2 - a2 * b1 if det == 0: continue y = Fr(c1 * b2 - c2 * b1, det) z = Fr(a1 * c2 - a2 * c1, det) if all(a3 * y + b3 * z <= c3 for (a3, b3, c3) in planes): pts.append((y, z)) pts = sorted(set(pts)) if len(pts) < 3: return [] def cross(o, a, b): return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]) lower = [] for p in pts: while len(lower) >= 2 and cross(lower[-2], lower[-1], p) <= 0: lower.pop() lower.append(p) upper = [] for p in reversed(pts): while len(upper) >= 2 and cross(upper[-2], upper[-1], p) <= 0: upper.pop() upper.append(p) hull = lower[:-1] + upper[:-1] return hull if len(hull) >= 3 else [] def polygon_integrate(poly2: dict, planes): V = polygon_vertices(planes) if len(V) < 3: return Fr(0) zs = sorted({v[1] for v in V}) edges = [(V[i], V[(i + 1) % len(V)]) for i in range(len(V))] def line_at(p, q, z): if q[1] == p[1]: return p[0] t = (z - p[1]) / (q[1] - p[1]) return p[0] + t * (q[0] - p[0]) def lin(e): p, q = e if q[1] == p[1]: return (Fr(0), p[0]) alpha = (q[0] - p[0]) / (q[1] - p[1]) return (alpha, p[0] - alpha * p[1]) total = Fr(0) for a in range(len(zs) - 1): z0, z1 = zs[a], zs[a + 1] if z0 == z1: continue zm = (z0 + z1) / 2 ys = [] for (p, q) in edges: if (p[1] - zm) * (q[1] - zm) <= 0: ys.append(line_at(p, q, zm)) if len(ys) < 2: continue ylo, yhi = min(ys), max(ys) lo_e = hi_e = None for e in edges: v = line_at(e[0], e[1], zm) if v == ylo: lo_e = e if v == yhi: hi_e = e if lo_e is None or hi_e is None: continue al, be = lin(lo_e) ah, bh = lin(hi_e) for (i, kk), co in poly2.items(): m = i + 1 for sign, (aa, bb) in ((Fr(1), (ah, bh)), (Fr(-1), (al, be))): for t in range(m + 1): cf = Fr(comb(m, t)) * (aa ** t) * (bb ** (m - t)) if cf == 0: continue e = t + kk val = (z1 ** (e + 1) - z0 ** (e + 1)) / (e + 1) total += sign * co * cf * val / (i + 1) return total def seg_integrate(Aj: dict, b: int, planes): """Integral over Y=0 of A(0,Z) Z^b dZ, Z from the polygon's Z-interval.""" lo, hi = None, None for (A, B, C) in planes: # substitute Y = 0: A*0 + B*Z <= C if B == 0: if C < 0: return Fr(0) continue v = Fr(C, B) if B > 0: hi = v if hi is None else min(hi, v) else: lo = v if lo is None else max(lo, v) if lo is None: lo = Fr(0) if hi is None or lo >= hi: return Fr(0) tot = Fr(0) for (i, kk), co in Aj.items(): if i != 0: continue e = kk + b tot += co * (hi ** (e + 1) - lo ** (e + 1)) / (e + 1) return tot # --------------------------------------------------------------------------- # region geometry # --------------------------------------------------------------------------- def poly_H(r, d, U): return {(0, 0, 0): U - r * d, (1, 0, 0): Fr(-1), (0, 1, 0): Fr(-1), (0, 0, 1): -d} def poly_c(r, d, b): return {(0, 0, 0): b - r * d, (1, 0, 0): Fr(-1)} def poly_Hc_C(r, d, U, b): return {(0, 0, 0): U - b, (0, 1, 0): Fr(-1), (0, 0, 1): -d} def poly_Hc_D(r, d, U): return {(0, 0, 0): U - (r + 1) * d, (1, 0, 0): Fr(-1), (0, 1, 0): Fr(-1), (0, 0, 1): -d} def region_planes(K, r, j, d, U, ell, c): P = [(Fr(-1), Fr(0), Fr(0)), (Fr(0), Fr(-1), Fr(0))] if K != "E": P.append((Fr(1), Fr(0), c[r] - r * d)) P.append((Fr(1), Fr(1), ell - (r + j) * d)) if K == "C": P.append((Fr(1), Fr(0), c[r + 1] - d - r * d)) P.append((Fr(0), Fr(1), U - c[r + 1] - j * d)) elif K == "D": P.append((Fr(-1), Fr(0), -(c[r + 1] - d - r * d))) P.append((Fr(1), Fr(1), U - d - (r + j) * d)) elif K == "E": P.append((Fr(-1), Fr(0), -(c[r] - r * d))) return P # --------------------------------------------------------------------------- # assembly # --------------------------------------------------------------------------- def correction(region, k, d, U, ell, c, sigG2, sigGT, sigT2, verbose=False): n = k - 1 total = Fr(0) for r in range(0, n + 1): s = n - r if region == "E": if r * d > ell: # s <= ell and s >= r d break if r == 0: continue # E_0 = {0 > c_0 = 0} is empty elif r * d > c[r]: # legal regions need r d <= c_r break Aby: dict = defaultdict(dict) if region == "E": for (nu, p), co in sigG2.items(): Aby[nu] = p_add(Aby[nu], p_pow(poly_H(r, d, U), p), -co) else: for (nu, p, cp, hc), co in sigGT.items(): A = p_mul(p_pow(poly_H(r, d, U), p), p_mul(p_pow(poly_c(r, d, c[r + 1]), cp) if region == "C" else p_pow(p_const(d), cp), p_pow(poly_Hc_C(r, d, U, c[r + 1]) if region == "C" else poly_Hc_D(r, d, U), hc))) Aby[nu] = p_add(Aby[nu], A, -2 * co) for (nu, cp, hc), co in sigT2.items(): A = p_mul(p_pow(poly_c(r, d, c[r + 1]), cp) if region == "C" else p_pow(p_const(d), cp), p_pow(poly_Hc_C(r, d, U, c[r + 1]) if region == "C" else poly_Hc_D(r, d, U), hc)) Aby[nu] = p_add(Aby[nu], A, co) for nu, A3 in Aby.items(): if not A3: continue byj: dict = defaultdict(list) for gp, hp, jj, coefE in nu_expansion(r, s, d, nu): byj[jj].append((gp, hp, coefE)) for jj, lst in byj.items(): planes = region_planes(region, r, jj, d, U, ell, c) Aj = p_subst_j(A3, jj) if r == 0: for gp, hp, coefE in lst: b = (s - 1 + sum(hp)) if s >= 1 else 0 total += coefE * seg_integrate(Aj, b, planes) elif s == 0: for gp, hp, coefE in lst: a = r - 1 + sum(gp) total += coefE * yline_integrate(Aj, a, planes) else: poly2: dict = defaultdict(Fr) for gp, hp, coefE in lst: a = r - 1 + sum(gp) b = (s - 1 + sum(hp)) if s >= 1 else 0 for (i, kk), val in Aj.items(): poly2[(i + a, kk + b)] += coefE * val total += polygon_integrate(dict(poly2), planes) if verbose: print(f" [{region}] r={r} partial={float(total):+.6e}", flush=True) return total def J0_machinery(k, d, U, ell, sigG2, verbose=False): """k * int_{s(X)<=ell} G^2 dX : the cap-free numerator, same stratification.""" n = k - 1 total = Fr(0) for r in range(0, n + 1): s = n - r Aby: dict = defaultdict(dict) for (nu, p), co in sigG2.items(): Aby[nu] = p_add(Aby[nu], p_pow(poly_H(r, d, U), p), co) for nu, A3 in Aby.items(): if not A3: continue byj: dict = defaultdict(list) for gp, hp, jj, coefE in nu_expansion(r, s, d, nu): byj[jj].append((gp, hp, coefE)) for jj, lst in byj.items(): planes = [(Fr(-1), Fr(0), Fr(0)), (Fr(0), Fr(-1), Fr(0)), (Fr(1), Fr(1), ell - (r + jj) * d)] Aj = p_subst_j(A3, jj) if r == 0: for gp, hp, coefE in lst: b = (s - 1 + sum(hp)) if s >= 1 else 0 total += coefE * seg_integrate(Aj, b, planes) elif s == 0: for gp, hp, coefE in lst: a = r - 1 + sum(gp) total += coefE * yline_integrate(Aj, a, planes) else: poly2: dict = defaultdict(Fr) for gp, hp, coefE in lst: a = r - 1 + sum(gp) b = (s - 1 + sum(hp)) if s >= 1 else 0 for (i, kk), val in Aj.items(): poly2[(i + a, kk + b)] += coefE * val total += polygon_integrate(dict(poly2), planes) if verbose: print(f" [J0] r={r} partial={float(total):+.6e}", flush=True) return k * total # --------------------------------------------------------------------------- # PERFORMANCE NOTE (2026-09-24) # # The occurrence-wise shift expansion (`capped_moment.shifted_small`) is exact # and validated, but its size explodes in the ambient dimension. Measured at # k = 46, d = 17, dcap = 40/861, s = 45 - r: # # nu = (32,) deg 32, 1 part : 1486 entries, 0.03 s (r = 0) # nu = (28 with 5 parts) 45402 entries, 0.77 s (r = 0) # 394961 entries, 4.2 s (r = 1) # 2011810 entries, 17.7 s (r = 3) # # There are 915 distinct nu in the d = 17 signature set, so this route is # infeasible as written. The fix is to replace the occurrence-wise expansion by # the radial/Laplace transform of m_nu(X_shifted): # # T(m_nu)(Y,Z) = inverse Laplace of # [prod_q z_q^{m_q}] (1 + sum_q R_q z_q)^r # (1 + sum_q S_q z_q)^j # (1 + sum_q U_q z_q)^{s-j}, # R_q(s) = sum_k C(q,k) d^{q-k} k! s^{-(k+1)} (rough, shift d) # S_q(t) = same with t (small shifted, shift d) # U_q(t) = q! t^{-(q+1)} (small unshifted) # # because int_{block} P(x) g(sum x) dx = int T(P)(X) g(X) dX. The block # products are univariate series in 1/s and 1/t, so the per-nu cost is a # coefficient extraction over the distinct part values instead of a product of # per-occurrence choices. All small-k controls above already validate the # functionals (G, T, J_0, the regions and the polygon integration); only this # transform needs replacing. # --------------------------------------------------------------------------- def _z_interval(planes): lo = hi = None for (A, B, C) in planes: if B == 0: if C < 0: return None continue v = Fr(C, B) if B > 0: hi = v if hi is None else min(hi, v) else: lo = v if lo is None else max(lo, v) if lo is None: lo = Fr(0) if hi is None or lo >= hi: return None return lo, hi def correction_laplace(region, k, d, U, ell, c, sigG2, sigGT, sigT2, verbose=False): """Same correction as `correction`, using the radial/Laplace transform.""" from radial_transform import (radial_T, polygon_moments, integrate_A_T) n = k - 1 total = Fr(0) for r in range(0, n + 1): s = n - r if region == "E": if r * d > ell: break if r == 0: continue elif r * d > c[r]: break Aby: dict = defaultdict(dict) if region == "E": for (nu, p), co in sigG2.items(): Aby[nu] = p_add(Aby[nu], p_pow(poly_H(r, d, U), p), -co) else: for (nu, p, cp, hc), co in sigGT.items(): A = p_mul(p_pow(poly_H(r, d, U), p), p_mul(p_pow(poly_c(r, d, c[r + 1]), cp) if region == "C" else p_pow(p_const(d), cp), p_pow(poly_Hc_C(r, d, U, c[r + 1]) if region == "C" else poly_Hc_D(r, d, U), hc))) Aby[nu] = p_add(Aby[nu], A, -2 * co) for (nu, cp, hc), co in sigT2.items(): A = p_mul(p_pow(poly_c(r, d, c[r + 1]), cp) if region == "C" else p_pow(p_const(d), cp), p_pow(poly_Hc_C(r, d, U, c[r + 1]) if region == "C" else poly_Hc_D(r, d, U), hc)) Aby[nu] = p_add(Aby[nu], A, co) if not Aby: continue jmax = s for j in range(0, jmax + 1): if (r + j) * d > ell: break planes = region_planes(region, r, j, d, U, ell, c) if r == 0: iv = _z_interval(planes) if iv is None: continue zlo, zhi = iv sign = Fr(comb(n, r) * (-1) ** j * comb(s, j)) for nu, A3 in Aby.items(): Aj = p_subst_j(A3, j) T2 = radial_T(r, j, s, d, nu) if not T2: continue # A(0,Z)*T(Z) with a possible Z^k from A for (i, kk), ca in Aj.items(): if i != 0: continue for (p, q), ct in T2.items(): if p != 0: continue e = kk + q total += sign * ca * ct * (zhi ** (e + 1) - zlo ** (e + 1)) / (e + 1) continue V = polygon_vertices(planes) if not V: continue sign = Fr(comb(n, r) * (-1) ** j * comb(s, j)) prepared = [] maxA = maxT = 0 for nu, A3 in Aby.items(): Aj = p_subst_j(A3, j) T2 = radial_T(r, j, s, d, nu) if not Aj or not T2: continue prepared.append((Aj, T2)) maxA = max(maxA, max((i + kk for (i, kk) in Aj), default=0)) maxT = max(maxT, max((i + kk for (i, kk) in T2), default=0)) if not prepared: continue M = polygon_moments(planes, maxA + maxT) if M is None: continue from radial_transform import int_scale_M, integrate_A_T_scaled Mint, dM = int_scale_M(M) dmax = len(M) - 1 for Aj, T2 in prepared: total += sign * integrate_A_T_scaled(Aj, T2, Mint, dM, dmax) if verbose: print(f" [{region}L] r={r} partial={float(total):+.6e}", flush=True) return total # --------------------------------------------------------------------------- # fast (compact) A construction: build A once per nu, not once per signature key # --------------------------------------------------------------------------- _exp_yz_cache: dict = {} _exp_y_cache: dict = {} _exp_yz2_cache: dict = {} def _expand_YplusZ(base, e): """(base - Y - Z)^e -> {(i,k): coef}.""" key = (base, e) hit = _exp_yz_cache.get(key) if hit is not None: return hit out: dict = defaultdict(Fr) for t in range(e + 1): c0 = Fr(comb(e, t)) * (base ** (e - t)) * ((-1) ** t) if c0 == 0: continue for i in range(t + 1): out[(i, t - i)] += c0 * Fr(comb(t, i)) res = {k: v for k, v in out.items() if v} _exp_yz_cache[key] = res return res def _expand_Y(base, e): return {(i, 0): Fr(comb(e, i)) * (base ** (e - i)) * ((-1) ** i) for i in range(e + 1)} def _expand_YZ(alpha, beta, a, b): key = (alpha, beta, a, b) hit = _exp_yz2_cache.get(key) if hit is not None: return hit out: dict = defaultdict(Fr) for i in range(a + 1): ca = Fr(comb(a, i)) * (alpha ** (a - i)) * ((-1) ** i) if ca == 0: continue for k in range(b + 1): out[(i, k)] += ca * Fr(comb(b, k)) * (beta ** (b - k)) * ((-1) ** k) res = {k: v for k, v in out.items() if v} _exp_yz2_cache[key] = res return res def build_A_compact(region, r, d, U, c, sigG2, sigGT, sigT2): """A_ν in a compact form: E: {p: coef} means sum coef H^p D: {e: coef} means sum coef (H-d)^e C: {(a,b): coef} means sum coef c^a (H-c)^b """ S: dict = defaultdict(lambda: defaultdict(Fr)) if region == "E": for (nu, p), co in sigG2.items(): S[nu][p] += -co elif region == "D": b = c[r + 1] for (nu, p, cp, hc), co in sigGT.items(): for j in range(p + 1): S[nu][j + hc] += -2 * co * Fr(comb(p, j)) \ * (d ** (p - j + cp)) for (nu, cp, hc), co in sigT2.items(): S[nu][hc] += co * (d ** cp) elif region == "C": for (nu, p, cp, hc), co in sigGT.items(): for j in range(p + 1): S[nu][(cp + j, hc + p - j)] += -2 * co * Fr(comb(p, j)) for (nu, cp, hc), co in sigT2.items(): S[nu][(cp, hc)] += co return {nu: {k: v for k, v in d2.items() if v} for nu, d2 in S.items()} def correction_laplace_fast(region, k, d, U, ell, c, sigG2, sigGT, sigT2, verbose=False): from radial_transform import (radial_T, polygon_moments, integrate_A_T) n = k - 1 total = Fr(0) for r in range(0, n + 1): s = n - r if region == "E": if r * d > ell: break if r == 0: continue elif r * d > c[r]: break S = build_A_compact(region, r, d, U, c, sigG2, sigGT, sigT2) if not S: continue for j in range(0, s + 1): if (r + j) * d > ell: break planes = region_planes(region, r, j, d, U, ell, c) if r == 0: iv = _z_interval(planes) if iv is None: continue zlo, zhi = iv sign = Fr(comb(n, r) * (-1) ** j * comb(s, j)) for nu, comp in S.items(): T2 = radial_T(r, j, s, d, nu) if not T2: continue if region == "C": Aj = {} for (a, b), co in comp.items(): Aj = p_add(Aj, _expand_YZ(c[r + 1] - r * d, U - c[r + 1] - j * d, a, b), co) elif region == "D": Aj = {} for e, co in comp.items(): Aj = p_add(Aj, _expand_YplusZ( U - (r + 1) * d - j * d, e), co) else: Aj = {} for p, co in comp.items(): Aj = p_add(Aj, _expand_YplusZ(U - r * d - j * d, p), co) for (i, kk), ca in Aj.items(): if i != 0: continue for (pp, q), ct in T2.items(): if pp != 0: continue e = kk + q total += sign * ca * ct * (zhi ** (e + 1) - zlo ** (e + 1)) / (e + 1) continue V = polygon_vertices(planes) if not V: continue sign = Fr(comb(n, r) * (-1) ** j * comb(s, j)) prepared = [] maxA = maxT = 0 for nu, comp in S.items(): T2 = radial_T(r, j, s, d, nu) if not T2: continue if region == "C": Aj = {} for (a, b), co in comp.items(): Aj = p_add(Aj, _expand_YZ(c[r + 1] - r * d, U - c[r + 1] - j * d, a, b), co) elif region == "D": Aj = {} for e, co in comp.items(): Aj = p_add(Aj, _expand_YplusZ( U - (r + 1) * d - j * d, e), co) else: Aj = {} for p, co in comp.items(): Aj = p_add(Aj, _expand_YplusZ(U - r * d - j * d, p), co) if not Aj: continue prepared.append((Aj, T2)) maxA = max(maxA, max((i + kk for (i, kk) in Aj), default=0)) maxT = max(maxT, max((i + kk for (i, kk) in T2), default=0)) if not prepared: continue M = polygon_moments(planes, maxA + maxT) if M is None: continue from radial_transform import int_scale_M, integrate_A_T_scaled Mint, dM = int_scale_M(M) dmax = len(M) - 1 for Aj, T2 in prepared: total += sign * integrate_A_T_scaled(Aj, T2, Mint, dM, dmax) if verbose: print(f" [{region}F] r={r} partial={float(total):+.6e}", flush=True) return total def _S_g2(region, r, d, c, sigG2): """Compact A for the integrand G^2 on `region` (H = c + hc).""" S: dict = defaultdict(lambda: defaultdict(Fr)) if region == "E": for (nu, p), co in sigG2.items(): S[nu][p] += co elif region == "D": for (nu, p), co in sigG2.items(): for j in range(p + 1): S[nu][j] += co * Fr(comb(p, j)) * (d ** (p - j)) elif region == "C": for (nu, p), co in sigG2.items(): for j in range(p + 1): S[nu][(j, p - j)] += co * Fr(comb(p, j)) return {nu: {k: v for k, v in d2.items() if v} for nu, d2 in S.items()} def region_int_g2(region, k, d, U, ell, c, sigG2, verbose=False): """Exact int_{region} G^2 dYdZ (region in {C,D,E}).""" from radial_transform import (radial_T, polygon_moments, integrate_A_T) n = k - 1 total = Fr(0) for r in range(0, n + 1): s = n - r if region == "E": if r * d > ell: break if r == 0: continue elif r * d > c[r]: break S = _S_g2(region, r, d, c, sigG2) if not S: continue for j in range(0, s + 1): if (r + j) * d > ell: break planes = region_planes(region, r, j, d, U, ell, c) if r == 0: iv = _z_interval(planes) if iv is None: continue zlo, zhi = iv sign = Fr(comb(n, r) * (-1) ** j * comb(s, j)) for nu, comp in S.items(): T2 = radial_T(r, j, s, d, nu) if not T2: continue Aj = {} for (a, b), co in comp.items(): Aj = p_add(Aj, _expand_YZ(c[r + 1] - r * d, U - c[r + 1] - j * d, a, b), co) for (i, kk), ca in Aj.items(): if i != 0: continue for (pp, q), ct in T2.items(): if pp != 0: continue e = kk + q total += sign * ca * ct * (zhi ** (e + 1) - zlo ** (e + 1)) / (e + 1) continue if not polygon_vertices(planes): continue sign = Fr(comb(n, r) * (-1) ** j * comb(s, j)) prepared = [] maxA = maxT = 0 for nu, comp in S.items(): T2 = radial_T(r, j, s, d, nu) if not T2: continue Aj = {} if region == "C": for (a, b), co in comp.items(): Aj = p_add(Aj, _expand_YZ(c[r + 1] - r * d, U - c[r + 1] - j * d, a, b), co) elif region == "D": for e, co in comp.items(): Aj = p_add(Aj, _expand_YplusZ(U - (r + 1) * d - j * d, e), co) else: for p, co in comp.items(): Aj = p_add(Aj, _expand_YplusZ(U - r * d - j * d, p), co) if not Aj: continue prepared.append((Aj, T2)) maxA = max(maxA, max((i + kk for (i, kk) in Aj), default=0)) maxT = max(maxT, max((i + kk for (i, kk) in T2), default=0)) if not prepared: continue M = polygon_moments(planes, maxA + maxT) if M is None: continue from radial_transform import int_scale_M, integrate_A_T_scaled Mint, dM = int_scale_M(M) dmax = len(M) - 1 for Aj, T2 in prepared: total += sign * integrate_A_T_scaled(Aj, T2, Mint, dM, dmax) if verbose: print(f" [G2 {region}] r={r} partial={float(total):+.6e}", flush=True) return total # --------------------------------------------------------------------------- # capped denominator: I_cap = I_0 - Delta, Delta = int_{C u D u E} (full-cap) # of F^2 over the fiber # --------------------------------------------------------------------------- def signature_F2(F_terms): """F^2 = sum coef (H-t)^e m_nu(x,t) -> {(nu, e): coef}.""" L = defaultdict(Fr) n = len(F_terms) for i in range(n): (c1, a1, l1) = F_terms[i] for j in range(i, n): (c2, a2, l2) = F_terms[j] w = c1 * c2 * (1 if i == j else 2) if w == 0: continue for nu, cc in _mul(tuple(l1), tuple(l2)).items(): L[(nu, a1 + a2)] += w * cc return {k: v for k, v in L.items() if v} def _P0_terms(SF2): """int_0^H F^2 dt -> {(mu, p): coef} meaning coef H^p m_mu.""" L = defaultdict(Fr) for (nu, e), co in SF2.items(): L[(tuple(nu), e + 1)] += co * Fr(1, e + 1) for q in sorted(set(nu)): lst = list(nu) lst.remove(q) mu = tuple(sorted(lst, reverse=True)) L[(mu, e + q + 1)] += co * Fr(factorial(q) * factorial(e), factorial(e + q + 1)) return {k: v for k, v in L.items() if v} def _Pdel_terms(SF2): """int_c^H F^2 dt -> {(mu, cpow, hc): coef} cos c^cpow (H-c)^hc m_mu.""" L = defaultdict(Fr) for (nu, e), co in SF2.items(): L[(tuple(nu), 0, e + 1)] += co * Fr(1, e + 1) for q in sorted(set(nu)): lst = list(nu) lst.remove(q) mu = tuple(sorted(lst, reverse=True)) for h in range(q + 1): L[(mu, q - h, e + h + 1)] += \ co * Fr(comb(q, h) * factorial(e) * factorial(h), factorial(e + h + 1)) return {k: v for k, v in L.items() if v} def build_A_denom(region, r, d, c, P0, Pdel): """Compact A for the denominator correction on `region`.""" S: dict = defaultdict(lambda: defaultdict(Fr)) if region == "E": for (mu, p), co in P0.items(): # full fiber removed S[mu][p] += co elif region == "D": for (mu, cp, hc), co in Pdel.items(): # deleted [d, H] S[mu][hc] += co * (d ** cp) elif region == "C": for (mu, cp, hc), co in Pdel.items(): # deleted [b-R_r, H] S[mu][(cp, hc)] += co return {mu: {k: v for k, v in d2.items() if v} for mu, d2 in S.items()} def denom_correction(region, k, d, U, ell, c, P0, Pdel, verbose=False): """Exact int over `region` of (full fiber - capped fiber) int F^2.""" from radial_transform import (radial_T, polygon_moments, int_scale_M, integrate_A_T_scaled) n = k - 1 total = Fr(0) for r in range(0, n + 1): s = n - r if region == "E": if r * d > ell: break if r == 0: continue elif r * d > c[r]: break S = build_A_denom(region, r, d, c, P0, Pdel) if not S: continue for j in range(0, s + 1): if (r + j) * d > ell: break planes = region_planes(region, r, j, d, U, ell, c) if r == 0: iv = _z_interval(planes) if iv is None: continue zlo, zhi = iv sign = Fr(comb(n, r) * (-1) ** j * comb(s, j)) for nu, comp in S.items(): T2 = radial_T(r, j, s, d, nu) if not T2: continue Aj = {} for (a, b), co in comp.items(): Aj = p_add(Aj, _expand_YZ(c[r + 1] - r * d, U - c[r + 1] - j * d, a, b), co) for (i, kk), ca in Aj.items(): if i != 0: continue for (pp, q), ct in T2.items(): if pp != 0: continue e = kk + q total += sign * ca * ct * (zhi ** (e + 1) - zlo ** (e + 1)) / (e + 1) continue if not polygon_vertices(planes): continue sign = Fr(comb(n, r) * (-1) ** j * comb(s, j)) prepared = [] maxA = maxT = 0 for nu, comp in S.items(): T2 = radial_T(r, j, s, d, nu) if not T2: continue Aj = {} if region == "C": for (a, b), co in comp.items(): Aj = p_add(Aj, _expand_YZ(c[r + 1] - r * d, U - c[r + 1] - j * d, a, b), co) elif region == "D": for e, co in comp.items(): Aj = p_add(Aj, _expand_YplusZ(U - (r + 1) * d - j * d, e), co) else: for p, co in comp.items(): Aj = p_add(Aj, _expand_YplusZ(U - r * d - j * d, p), co) if not Aj: continue prepared.append((Aj, T2)) maxA = max(maxA, max((i + kk for (i, kk) in Aj), default=0)) maxT = max(maxT, max((i + kk for (i, kk) in T2), default=0)) if not prepared: continue M = polygon_moments(planes, maxA + maxT) if M is None: continue Mint, dM = int_scale_M(M) dmax = len(M) - 1 for Aj, T2 in prepared: total += sign * integrate_A_T_scaled(Aj, T2, Mint, dM, dmax) if verbose: print(f" [D {region}] r={r} partial={float(total):+.6e}", flush=True) return total def violation_F2(k, d, U, c, SF2, verbose=False): """Delta = int_{s c_r} F^2 dX over the full k-dimensional support. This is the capped-denominator violation; unlike the numerator there is no marginal base cutoff, so the region is the single over-cap stratum {Y >= c_r - r d, Y + Z <= U - (r+j) d}.""" from radial_transform import (radial_T, polygon_moments, int_scale_M, integrate_A_T_scaled) total = Fr(0) S: dict = defaultdict(lambda: defaultdict(Fr)) for (nu, e), co in SF2.items(): S[nu][e] += co S = {nu: dict(v) for nu, v in S.items()} for r in range(1, k + 1): s = k - r if r * d > U: break if r >= len(c): break for j in range(0, s + 1): if (r + j) * d > U: break planes = [(Fr(-1), Fr(0), Fr(0)), (Fr(0), Fr(-1), Fr(0)), (Fr(-1), Fr(0), -(c[r] - r * d)), (Fr(1), Fr(1), U - (r + j) * d)] V = polygon_vertices(planes) if not V: continue sign = Fr(comb(k, r) * (-1) ** j * comb(s, j)) prepared = [] maxA = maxT = 0 for nu, comp in S.items(): T2 = radial_T(r, j, s, d, nu) if not T2: continue Aj = {} for e, co in comp.items(): Aj = p_add(Aj, _expand_YplusZ(U - r * d - j * d, e), co) if not Aj: continue prepared.append((Aj, T2)) maxA = max(maxA, max((i + kk for (i, kk) in Aj), default=0)) maxT = max(maxT, max((i + kk for (i, kk) in T2), default=0)) if not prepared: continue M = polygon_moments(planes, maxA + maxT) if M is None: continue Mint, dM = int_scale_M(M) dmax = len(M) - 1 for Aj, T2 in prepared: total += sign * integrate_A_T_scaled(Aj, T2, Mint, dM, dmax) if verbose: print(f" [violF2] r={r} partial={float(total):+.6e}", flush=True) return total