#!/usr/bin/env python3 """`capped_gram` must reproduce the direct contraction, bit-for-bit. At small `k` this checks, with exact rationals: 1. the recorded numerator functionals reproduce `correction_compact` for the witness whose signature slots were used (C, D and E), and for a *second* random witness; 2. the recorded denominator functional reproduces `violation_F2`; 3. the assembled Gram matrix agrees with direct per-basis and per-pair contraction evaluations. The same code path is then used at k = 46, where only step 1's identity on the stored witness is affordable. """ from __future__ import annotations import os import random import sys from fractions import Fraction as Fr HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "..", "src")) sys.path.insert(0, os.path.join(HERE, "..", "..", "..", "lib", "maynard")) from capped_gram import (assemble_gram, denominator_functionals, # noqa denominator_value, numerator_functionals, numerator_value, signature_parts) from capped_numerator import correction, violation_F2 # noqa from compact_contract import correction_compact # noqa from even_engine import EvenEngine # noqa A_SRC = Fr(2583, 10000) def setup(k, dvec): eps = Fr(25, 861) U, ell = 1 + eps, 1 - eps d = Fr(3, 250) / A_SRC caps = [Fr(0)] + [Fr(3, 20) / A_SRC] * 2 + [Fr(4, 25) / A_SRC] * (k + 2) e = EvenEngine(k, eps, dvec, "exact") return e, eps, d, U, ell, caps def merge(dicts): out = {} for dd in dicts: out.update(dd) return out def main(): ok = True for k, dvec in ((4, 3), (5, 3), (6, 3)): e, eps, d, U, ell, caps = setup(k, dvec) random.seed(11) c = [Fr(random.randint(-5, 5), random.randint(1, 4)) for _ in range(e.n)] sG2, sGT, sT2, SF2 = signature_parts(e, c) pre = sum(correction_compact(K, k, d, U, ell, caps, sG2, sGT, sT2) for K in ("C", "D", "E")) occ = sum(correction(K, k, d, U, ell, caps, sG2, sGT, sT2) for K in ("C", "D", "E")) delta = violation_F2(k, d, U, caps, SF2) W = merge([numerator_functionals(K, k, d, U, ell, caps, sG2, sGT, sT2) for K in ("C", "D", "E")]) WD = denominator_functionals(k, d, U, caps, SF2) got = numerator_value(W, sG2, sGT, sT2) gotD = denominator_value(WD, SF2) good = (got == pre == occ) and (gotD == delta) ok &= good print(f"k={k}: E compact={float(pre):+.10e} occ={float(occ):+.10e} " f"W={float(got):+.10e} | Delta={float(delta):+.10e} " f"WD={float(gotD):+.10e} {'OK' if good else 'MISMATCH'}", flush=True) # second, independent witness: its slots must be inside the recorded set random.seed(99) c2 = [Fr(random.randint(-5, 5), random.randint(1, 4)) for _ in range(e.n)] sG2b, sGTb, sT2b, SF2b = signature_parts(e, c2) pre2 = sum(correction_compact(K, k, d, U, ell, caps, sG2b, sGTb, sT2b) for K in ("C", "D", "E")) delta2 = violation_F2(k, d, U, caps, SF2b) got2 = numerator_value(W, sG2b, sGTb, sT2b) gotD2 = denominator_value(WD, SF2b) good2 = (got2 == pre2) and (gotD2 == delta2) ok &= good2 print(f"k={k}: second witness E={float(pre2):+.10e} " f"W={float(got2):+.10e} | Delta={float(delta2):+.10e} " f"WD={float(gotD2):+.10e} {'OK' if good2 else 'MISMATCH'}", flush=True) # Gram assembly vs direct contraction (basis elements and pairs) Mn = assemble_gram(W, e, "num") Md = assemble_gram(WD, e, "den") quad = Fr(0) for i in range(e.n): for j in range(e.n): quad += c[i] * c[j] * Mn[i][j] good3 = (quad == pre) ok &= good3 print(f"k={k}: c^T E_sym c = {float(quad):+.10e} " f"{'OK' if good3 else 'MISMATCH'}", flush=True) # direct pair check on a few indices random.seed(7) idx = random.sample(range(e.n), min(6, e.n)) for i in idx: for j in idx: if j < i: continue ci = [Fr(0)] * e.n ci[i] = Fr(1) if j != i: ci[j] = Fr(1) g1, g2, g3, _ = signature_parts(e, ci) direct = sum(correction_compact(K, k, d, U, ell, caps, g1, g2, g3) for K in ("C", "D", "E")) pred = Mn[i][i] + (0 if j == i else 2 * Mn[i][j] + Mn[j][j]) if direct != pred: ok = False print(f" pair ({i},{j}) direct={float(direct):+.6e} " f"pred={float(pred):+.6e} MISMATCH", flush=True) quadD = Fr(0) for i in range(e.n): for j in range(e.n): quadD += c[i] * c[j] * Md[i][j] if quadD != delta: ok = False print(f" Delta gram mismatch {float(quadD):+.10e}", flush=True) print(f"k={k}: pair + Delta gram checks " f"{'OK' if ok else 'see above'}", flush=True) print("CAPPED-GRAM", "PASS" if ok else "FAIL") return 0 if ok else 1 if __name__ == "__main__": raise SystemExit(main())