#!/usr/bin/env python3 """Streaming exact-rational certificate at d = 19..27 (no matrix materialisation). For each configuration (k, eps, dvec, tau): 1. dvec Ritz vector via the blocked arb Cholesky whitening (existing pipeline); 2. rationalise it (limit_denominator 1e9); 3. stream the exact quadratic form Q_tau(c) = c^T (M2 - tau M1) c over the upper triangle of the exact rational Gram pair -- no dense matrix, only O(n^2) entry evaluations -- at degree dvec and at d = 27 (the d = 27 pass is the "exact Gram assembly": it evaluates every entry of the d = 27 pair); 4. persist the exact value, timing and the rationalised vector. Q_tau(c) > 0 certifies M_{k,eps} > tau at that degree; by basis nesting the zero-padded vector gives the identical value at every higher degree, so the d = 27 column is valid without a d = 27 eigensolve. """ from __future__ import annotations import hashlib import json import os import sys import time from fractions import Fraction as Fr HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) from even_engine import EvenEngine # noqa: E402 from certificate import ritz_vector, A_SRC # noqa: E402 def stream_Q(ef: EvenEngine, c, tau: Fr, progress=False): """Exact c^T (M2 - tau M1) c, upper-triangle, symmetric.""" n = ef.n diag = Fr(0) off = Fr(0) t0 = time.time() for i in range(n): if progress and i % 250 == 0: print(f" row {i}/{n} {time.time()-t0:.0f}s", flush=True) ci = c[i] if ci == 0: continue for j in range(i, n): v = ef.M2(i, j) - tau * ef.M1(i, j) if i == j: diag += ci * ci * v else: off += 2 * ci * c[j] * v return diag + off, time.time() - t0 def one(k, eps, dvec, tau, denom=10 ** 9, prec=1024, out=None, jd27=True, progress=True): t0 = time.time() print(f"[{k} {eps} dvec={dvec} tau={tau}] ritz ...", flush=True) ef0, c_f, lam = ritz_vector(k, eps, dvec, prec=prec) tritz = time.time() - t0 c = [Fr(float(x)).limit_denominator(denom) for x in c_f] h = hashlib.sha256(",".join(str(x) for x in c).encode()).hexdigest() res = {"k": k, "eps": str(eps), "dvec": dvec, "tau": str(tau), "tau_float": float(tau), "n_vec": ef0.n, "prec": prec, "eig_hp": lam, "ritz_s": round(tritz, 1), "vec_sha256": h, "vec": [str(x) for x in c], "streams": {}} print(f"[{k} {eps}] ritz {tritz:.0f}s eig={lam:.9f}", flush=True) e0 = EvenEngine(k, Fr(eps), dvec, "exact") q0, t0s = stream_Q(e0, c, tau, progress=progress) res["streams"][str(dvec)] = {"n": e0.n, "Q": str(q0), "float": float(q0), "positive": q0 > 0, "s": round(t0s, 1)} print(f"[{k} {eps}] d={dvec} n={e0.n} Q={float(q0):+.6e} " f"pos={q0>0} {t0s:.0f}s", flush=True) if jd27 and dvec < 27: e1 = EvenEngine(k, Fr(eps), 27, "exact") c1 = c + [Fr(0)] * (e1.n - e0.n) q1, t1s = stream_Q(e1, c1, tau, progress=progress) res["streams"]["27"] = {"n": e1.n, "Q": str(q1), "float": float(q1), "positive": q1 > 0, "s": round(t1s, 1), "lifted": True} print(f"[{k} {eps}] d=27 n={e1.n} Q(lifted)={float(q1):+.6e} " f"pos={q1>0} {t1s:.0f}s identical_to_dvec={q1==q0}", flush=True) if out: with open(out, "w") as f: json.dump(res, f, indent=2) print("wrote", out, flush=True) return res if __name__ == "__main__": OUT = os.path.join(HERE, "..", "out") one(46, Fr(25, 861), 21, 1 / A_SRC, out=os.path.join(OUT, "cert_stream_k46_eps25_861_d27.json")) one(46, Fr(79, 1250), 21, 1 / A_SRC, out=os.path.join(OUT, "cert_stream_k46_eps79_1250_d27.json")) one(50, Fr(1, 25), 25, Fr(4), out=os.path.join(OUT, "cert_stream_k50_eps1_25_d27.json"))