#!/usr/bin/env python3 """Unit tests for the exact capped quadratic forms (mul_m + capped integral). Control A: capped=False reproduces the engine's exact M1 (Gram) entries. Control B: capped=True agrees with a brute-force Monte-Carlo integration of the same polynomial over the capped support (small k). """ from __future__ import annotations import os import random import sys from fractions import Fraction as Fr from itertools import permutations from math import factorial import numpy as np 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_forms import capped_F2, mul_m # noqa: E402 from even_engine import EvenEngine # noqa: E402 def msym(points, nu): n = points.shape[1] seen = set() tot = np.zeros(points.shape[0]) lam = list(nu) + [0] * (n - len(nu)) for perm in set(permutations(lam)): if perm in seen: continue seen.add(perm) term = np.ones(points.shape[0]) for i, a in enumerate(perm): if a: term = term * points[:, i] ** a tot += term return tot def eval_F_brute(points, terms, U): s = points.sum(axis=1) out = np.zeros(points.shape[0]) for (c, a, lam) in terms: out = out + float(c) * (U - s) ** a * msym(points, lam) return out def mc_capped_F2(k, terms, U, delta, Bfun, N=400000, seed=3): rng = np.random.default_rng(seed) E = rng.exponential(1.0, size=(N, k + 1)) T = U * E[:, :k] / E.sum(axis=1, keepdims=True) s = T.sum(axis=1) big = T > delta R = (T * big).sum(axis=1) r = big.sum(axis=1) cap = np.array([Bfun(int(rr)) for rr in r], dtype=float) F = eval_F_brute(T, terms, U) vol = U ** k / factorial(k) return float((F * F * (cap >= R)).mean() * vol) def main(): ok = True print("control A: capped=False == engine M1") for (k, eps, d) in [(4, Fr(1, 5), 4), (5, Fr(1, 10), 4), (6, Fr(1, 7), 6)]: e = EvenEngine(k, eps, d, "exact") random.seed(k * 100 + d) c = [Fr(random.randint(-5, 5), random.randint(1, 4)) for _ in range(e.n)] terms = [(c[i], e.B[i][0], e.B[i][1]) for i in range(e.n)] I_exp = capped_F2(k, terms, Fr(1) + eps, Fr(0), lambda r: Fr(0), capped=False) I_eng = sum(c[i] * sum(e.M1(i, j) * c[j] for j in range(e.n)) for i in range(e.n)) good = (I_exp == I_eng) ok &= good print(f" k={k} eps={eps} d={d}: {'OK' if good else 'MISMATCH'}") print(" PASS" if ok else " FAIL") print("control B: capped=True vs Monte-Carlo (k=4, eps=1/5, d=3, " "delta=3/20)") k, eps, d = 4, Fr(1, 5), 3 U = Fr(1) + eps delta = Fr(3, 20) B = [Fr(0), Fr(3, 10), Fr(2, 5), Fr(2, 5), Fr(2, 5)] Bf = lambda r, B=B: B[min(r, len(B) - 1)] # noqa: E731 e = EvenEngine(k, eps, d, "exact") random.seed(11) c = [Fr(random.randint(-3, 3), random.randint(1, 3)) for _ in range(e.n)] terms = [(c[i], e.B[i][0], e.B[i][1]) for i in range(e.n)] ex = float(capped_F2(k, terms, U, delta, Bf, capped=True)) mc = mc_capped_F2(k, terms, float(U), float(delta), lambda r: float(Bf(r))) rel = abs(ex - mc) / abs(ex) if ex else abs(mc) good = rel < 0.03 ok &= good print(f" exact={ex:.8e} mc={mc:.8e} rel={rel:.4f} " f"{'OK' if good else 'MISMATCH'}") # mul_m structural check print("control C: mul_m identities") good = (mul_m((1,), (1,)) == {(2,): 1, (1, 1): 2} and mul_m((2,), (1,)) == {(3,): 1, (2, 1): 1} and mul_m((2,), (2,)) == {(4,): 1, (2, 2): 2}) ok &= good print(" PASS" if good else " FAIL") print("ALL PASS" if ok else "FAILURES PRESENT") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())