#!/usr/bin/env python3 """Tests for job #4332's pair_kernel.py (route 59, rescue). Run: ../../.venv/Scripts/python.exe -m unittest test_pair_kernel -v Pins: (a) exact regression against return #1075's published falsifier1676.json cells; (b) the exact Hermitian symmetry of the h-summed pair kernel; (c) the Rayleigh/operator-norm bound and the spectral identity, including a symbolic (sympy, exact rational) verification on a small matrix; (d) negative controls (a corrupted symmetry and a corrupted bound must be detected). """ import unittest import numpy as np import sympy as sp import pair_kernel as pk X1 = 10 ** 5 M1 = 1000 X2 = 10 ** 6 M2 = 10 ** 4 class TestMobius(unittest.TestCase): def test_matches_sympy(self): mu = pk.mobius_upto(500) for n in range(1, 501): self.assertEqual(int(mu[n]), int(sp.mobius(n)), f"mu({n})") class TestPrimePowers(unittest.TestCase): def test_lambda_values(self): got = dict(pk.prime_powers(4)) # q in [4, 8) = {4,5,6,7} self.assertIn(4, got) self.assertAlmostEqual(got[4], np.log(2), places=12) # 4 = 2^2, Lambda = log 2 self.assertAlmostEqual(got[7], np.log(7), places=12) # prime, Lambda = log 7 self.assertNotIn(6, got) # 6 = 2*3 is not a prime power class TestRegressionAgainst1075(unittest.TestCase): """return #1075's served falsifier1676.json, x=1e5, Q=2, sigma=+1.""" def test_q2_sigma_plus_z06(self): km = pk.kernel_matrices(X1, M1, 2, 4, 1, 0.6 * X1, 50) self.assertAlmostEqual(abs(pk.signed_sum(km)), 937.099303, places=4) self.assertAlmostEqual(pk.absolute_majorant(km), 58233.851515, places=4) self.assertEqual(km['nterms'], 35600) def test_q2_sigma_minus_z06(self): km = pk.kernel_matrices(X1, M1, 2, 4, -1, 0.6 * X1, 50) self.assertAlmostEqual(abs(pk.signed_sum(km)), 170.184367, places=4) self.assertAlmostEqual(pk.absolute_majorant(km), 58421.613068, places=4) def test_q4_sigma_plus_z06(self): km = pk.kernel_matrices(X1, M1, 4, 4, 1, 0.6 * X1, 25) self.assertAlmostEqual(abs(pk.signed_sum(km)), 341.121243, places=4) self.assertAlmostEqual(pk.absolute_majorant(km), 28173.541083, places=4) def test_x1e6_q2_sigma_plus_z09(self): km = pk.kernel_matrices(X2, M2, 2, 4, 1, 0.9 * X2, 50) # published falsifier1676.json, x=1e6, Q=2, sigma=+1, z=0.9x self.assertAlmostEqual(abs(pk.signed_sum(km)), 464.715759, places=4) self.assertAlmostEqual(pk.absolute_majorant(km), 159600.948926, places=3) class TestHermiticity(unittest.TestCase): def test_relative_defect_small(self): km = pk.kernel_matrices(X1, M1, 2, 4, 1, 0.6 * X1, 50) self.assertLess(pk.hermiticity_defect(km['N']), 1e-12) def test_exact_conjugation_of_phases(self): """The exact structural fact: R -> -R under (e1,h1)<->(e2,h2), c unchanged.""" from math import gcd for (e1, e2, h1, h2, q) in [(51, 53, 4, 5, 2), (51, 55, 6, 8, 3), (95, 97, 4, 8, 2)]: c = q * gcd(e1, e2) * (e1 // gcd(e1, e2)) * (e2 // gcd(e1, e2)) j = gcd(e1, e2) l1, l2 = e1 // j, e2 // j R = h1 * l2 - h2 * l1 Rswap = h2 * l1 - h1 * l2 self.assertEqual((R + Rswap) % c, 0) for m in (7, 11, 13): if gcd(m, c) != 1: continue a = (2 * R * pow(m, -1, c)) % c # sigma*theta = 2 b = (2 * Rswap * pow(m, -1, c)) % c self.assertEqual((a + b) % c, 0) def test_negative_control_detects_broken_symmetry(self): km = pk.kernel_matrices(X1, M1, 2, 4, 1, 0.6 * X1, 50) broken = [A_.copy() for A_ in km['N']] broken[0][0, 1] += 1.0 self.assertGreater(pk.hermiticity_defect(broken), 1e-6) class TestOperatorBound(unittest.TestCase): def test_bound_holds_for_random_signs(self): km = pk.kernel_matrices(X1, M1, 2, 4, 1, 0.6 * X1, 50) ob = pk.operator_bound(km) rng = np.random.default_rng(11) n = km['beta'].size for _ in range(200): b = rng.choice([-1.0, 1.0], size=n) val = abs(sum(l * (b @ (A_ @ b)) for l, A_ in zip(km['lams'], km['N']))) self.assertLessEqual(val, ob['A_op'] * (1 + 1e-9)) def test_true_signs_within_bound(self): km = pk.kernel_matrices(X1, M1, 2, 4, 1, 0.6 * X1, 50) ob = pk.operator_bound(km) self.assertLessEqual(abs(pk.signed_sum(km)), ob['A_op'] * (1 + 1e-9)) # the bound is nontrivial here: it beats the trivial majorant by > 8x self.assertGreater(pk.absolute_majorant(km) / ob['A_op'], 8.0) def test_spectral_identity(self): km = pk.kernel_matrices(X1, M1, 2, 4, 1, 0.6 * X1, 50) S = pk.signed_sum(km) Ssp, _ = pk.spectral_profile(km) self.assertLess(abs(Ssp - S) / abs(S), 1e-12) def test_sympy_exact_rayleigh(self): """|x^H A x| <= rho(A) ||x||^2, symbolically, on an exact rational Hermitian matrix.""" A = sp.Matrix([[sp.Rational(3), sp.Rational(1, 2), sp.I], [sp.Rational(1, 2), sp.Rational(-2), sp.Rational(1, 3)], [-sp.I, sp.Rational(1, 3), sp.Rational(5, 4)]]) self.assertEqual(A - A.H, sp.zeros(3, 3)) rho = max(abs(sp.re(e)) for e in A.eigenvals()) for v in ([1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [1, sp.Rational(-2), 3], [2, 1, -1]): x = sp.Matrix(v) q = sp.simplify((x.H * A * x)[0]) / (x.H * x)[0] self.assertLessEqual(abs(sp.nsimplify(sp.re(q))), sp.nsimplify(rho) + sp.Rational(1, 10 ** 9)) class TestShuffleShared(unittest.TestCase): def test_reproducible_and_shared(self): kms = {} for zf in (0.6, 0.9): kms[zf] = pk.kernel_matrices(X1, M1, 2, 4, 1, zf * X1, 50) a = pk.shuffle_shared(kms, nperm=300, seed=5) b = pk.shuffle_shared(kms, nperm=300, seed=5) self.assertEqual(a[0.6]['p_upper'], b[0.6]['p_upper']) self.assertEqual(a['joint']['p_upper'], b['joint']['p_upper']) self.assertIn('p_lower', a[0.6]) def test_rank_one_kernel_null_is_degenerate(self): """A rank-one kernel makes the shared-draw null take only two values: the test must still return a well-defined rank, and the true value must sit at the top (ratio 1).""" km = dict(qs=[2], lams=np.array([1.0]), support=np.array([1, 2]), beta=np.array([1.0, 1.0]), N=[np.ones((2, 2), complex)], Abs=[np.ones((2, 2))], nterms=0, E=2) r = pk.shuffle_shared({0.6: km}, nperm=200, seed=1) self.assertAlmostEqual(r[0.6]['true_ratio'], 1.0, places=12) self.assertEqual(r[0.6]['p_lower'], 1.0) def test_zero_kernel_is_flagged_degenerate(self): """A1 = 0 must not silently produce a finite rank.""" km = dict(qs=[2], lams=np.array([1.0]), support=np.array([1, 2]), beta=np.array([1.0, 1.0]), N=[np.zeros((2, 2), complex)], Abs=[np.zeros((2, 2))], nterms=0, E=2) r = pk.shuffle_shared({0.6: km}, nperm=20, seed=1) self.assertTrue(np.isnan(r[0.6]['true_ratio'])) if __name__ == '__main__': unittest.main()