#!/usr/bin/env python3 """Unit tests for the Polymath8b Lemma 7.2 even-signature engine. Run: python3 tests/test_even_engine.py """ from __future__ import annotations import itertools import os import sys from fractions import Fraction as Fr from math import factorial HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "..", "src")) from even_engine import (EvenEngine, K_prodF, Tval, eig_float, # noqa: E402 partitions, perm_count) def brute_T(m, lam, mu): """T_m by explicit enumeration of permutations of the padded signatures.""" lp = list(lam) + [0] * (m - len(lam)) mp = list(mu) + [0] * (m - len(mu)) tot = 0 for a in set(itertools.permutations(lp)): for b in set(itertools.permutations(mp)): v = 1 for x, y in zip(a, b): v *= factorial(x + y) tot += v return tot def test_Tval(): cases = [(2, (2,), (2,)), (2, (2,), ()), (3, (2, 2), (2,)), (3, (4,), (2,)), (4, (2, 2), (2, 2)), (3, (2,), (2, 2)), (4, (), ())] for m, lam, mu in cases: assert len(lam) <= m and len(mu) <= m b = brute_T(m, lam, mu) g = Tval(m, lam, mu) assert b == g, (m, lam, mu, b, g) assert Tval(m, mu, lam) == g print(" Tval: %d cases OK" % len(cases)) def test_perm_count(): for m, lam in [(5, ()), (5, (2,)), (5, (2, 2)), (6, (2, 4))]: pads = list(lam) + [0] * (m - len(lam)) n = len(set(itertools.permutations(pads))) assert n == perm_count(m, lam), (m, lam, n, perm_count(m, lam)) print(" perm_count OK") def test_K_sympy(): """Independent sympy integration of (1-P1)^e m_lam m_mu over R_k.""" import sympy as sp x = sp.symbols("x0:4") tests = [ (3, 1, (2,), (2,)), (3, 2, (2,), ()), (3, 0, (2, 2), (2,)), (3, 1, (4,), (2,)), (2, 1, (2,), (2,)), ] for k, e, lam, mu in tests: xs = x[:k] P1 = sum(xs) def mono(lam_): """m_lam as an explicit sum of distinct monomials.""" pads = list(lam_) + [0] * (k - len(lam_)) terms = set(itertools.permutations(pads)) return sum(sp.prod([xs[i] ** t[i] for i in range(k)]) for t in terms) expr = (1 - P1) ** e * mono(lam) * mono(mu) # integrate over the simplex by nested integration val = expr for i in range(k - 1, -1, -1): lim = 1 - sum(xs[:i]) val = sp.integrate(val, (xs[i], 0, lim)) val = sp.simplify(val) got = K_prodF(k, e, lam, mu) assert sp.Rational(got.numerator, got.denominator) == sp.nsimplify(val), \ (k, e, lam, mu, got, val) print(" K_prod vs sympy: %d cases OK" % len(tests)) def test_plain_M_krylov_table(): """Polymath8b Table (Krylov lower bounds) vs this engine (even basis). Both are lower bounds for the same M_k; the even-signature monomial basis is weaker at a fixed low degree, so we only require agreement to a few 1e-3 and strict domination by the published ceiling (k/(k-1)) log k. """ table = {5: (2.00714, 7), 10: (2.54547, 11), 20: (3.12756, 13)} for k, (pub, d) in table.items(): e = EvenEngine(k, Fr(0), d, "float") A1, A2 = e.matrices() val = eig_float(A1, A2) import math ub = k / (k - 1) * math.log(k) assert val <= ub + 1e-9, (k, val, ub) assert pub - 2e-2 < val < pub + 2e-2, (k, val, pub) print(" plain M_k vs Krylov table OK") def test_eps_reference(): """High-precision k=50, eps=1/25, d=11 reference value.""" e = EvenEngine(50, Fr(1, 25), 11, "float") A1, A2 = e.matrices() val = eig_float(A1, A2) assert abs(val - 3.8519565733) < 1e-6, val print(" eps reference d=11 OK (%.10f)" % val) def test_exact_vs_float(): for k, eps, d in [(5, Fr(0), 5), (4, Fr(168, 1000), 5), (6, Fr(1, 25), 5)]: ee = EvenEngine(k, eps, d, "exact") A1e, A2e = ee.matrices() ef = EvenEngine(k, eps, d, "float") A1f, A2f = ef.matrices() for i in range(ee.n): for j in range(ee.n): assert abs(float(A1e[i][j]) - A1f[i, j]) <= 1e-12 * max( 1.0, abs(A1f[i, j])), (k, eps, d, i, j, "M1") assert abs(float(A2e[i][j]) - A2f[i, j]) <= 1e-12 * max( 1.0, abs(A2f[i, j])), (k, eps, d, i, j, "M2") print(" exact vs float entries OK") def test_partitions(): assert list(partitions(4)) == [(4,), (3, 1), (2, 2), (2, 1, 1), (1, 1, 1, 1)] print(" partitions OK") def main(): test_partitions() test_perm_count() test_Tval() test_K_sympy() test_exact_vs_float() test_plain_M_krylov_table() test_eps_reference() print("ALL TESTS PASSED") if __name__ == "__main__": main()