#!/usr/bin/env python3 """Job #4332 (route 59, rescue): the pair-kernel matrix and its spectral (sign-blind) axis. Reuses the kernel pinned by return #878 and used by #1075. The served producers are `falsifier1676.py` (sha256 fba579afec15522da7d14661962cbe65e5487504e65e3d106c1494a920085e67) and `shuffle1676.py` (sha256 8bfc3a547378f1105bb8c84b6502eaa0ff8be57307cbea61d64d38d8ef93dfea), both served with return #1075, not #1676 (the file name carries the job, not the return). Definitions (structured-dispersion-estimate.md sections 2 and 4 Step 2; grouped-divisor-moment.md section 1), verbatim from #1075: Y_q(m) = sum_{e~E} beta(e) sum_{h in H} c_h 1_{(m,eq)=1} e_{eq}(sigma theta h mbar) Phi_{eq,h}(m) Phi_{u,h}(m) = e(h z0'/(g m u)) - e(h z'/(g m u)), g = 1, theta = 2, sigma in {-1,+1} pair (e1,e2): j = gcd, e_i = j l_i, c = q j l1 l2, R = h1 l2 - h2 l1 K(e1,e2,h1,h2) = sum_{m in I_m, (m,c)=1} e_c(sigma theta R mbar) Phi_{qe1,h1}(m) conj(Phi_{qe2,h2}(m)) beta(e) = -mu(e), lambda(q) = Lambda(q), c_h = 1/A on H = [A,2A] ∩ Z, I_m = (M,2M] class: R != 0, j <= x^(7/300) (= 1 for x <= 1e6, i.e. coprime pairs) New object written here. For each prime power q the h-summed pair kernel is a matrix over the e-window, N_q[e1,e2] = c_h^2 * sum_{h1,h2 in H, R != 0} K(e1,e2,h1,h2), and the signed class sum is exactly the Hermitian form S = sum_q lambda(q) * beta^T N_q beta, beta = -mu restricted to the e-window. Because K(e2,e1,h2,h1) = conj(K(e1,e2,h1,h2)) and R is antisymmetric under the swap, N_q is Hermitian (`hermiticity_defect` measures this). Two immediate consequences are used here: * exact spectral evaluation: S = sum_q lambda(q) sum_k lam_k ||^2 with (lam_k, v_k) the eigenpairs of N_q; * the SIGN-BLIND operator-norm bound (Cauchy-Schwarz / Rayleigh): |S| <= ||beta||_2^2 * sum_q lambda(q) rho_q, rho_q = max_k |lam_k| = ||N_q||_op, and the trivial absolute majorant of #1075 is A1 = sum_q lambda(q) * 1^T |N_q| 1 (|beta| = 1 exactly on the squarefree support, 0 off it, so restricting N_q to that support loses nothing for either quantity). The bound uses no information about mu beyond `sum_{e~E} beta(e)^2 = #{squarefree e}`: it is available for every sign pattern on the same support. `A1 / A_op` is therefore the saving that is guaranteed *without* the Moebius signs, and the comparison of rho_q with ||N_q||_F and with the alignment of beta against the top eigenvectors says where the observed cancellation lives. Calibration: exact finite linear algebra on the pinned finite kernel. Numerically evaluated float64 kernels (the mod-c phases are exact: all products stay below 2^53, per the trusted review of #1075). No asymptotic statement, no bound on the twin prime gap, nothing about (R) as a theorem. """ import numpy as np from math import gcd, log THETA = 2 GG = 1 def mobius_upto(n): """mu(k) for k <= n, as int64 (identical to the served falsifier1676.mobius_upto).""" mu = np.ones(n + 1, dtype=np.int64) isp = np.ones(n + 1, bool) isp[:2] = False for p in range(2, n + 1): if isp[p]: isp[p * p::p] = False mu[p::p] *= -1 mu[p * p::p * p] = 0 return mu def prime_powers(Q): """[(q, Lambda(q))] for prime powers q in [Q, 2Q).""" out = [] for q in range(Q, 2 * Q): p = None for d in range(2, int(q ** 0.5) + 1): if q % d == 0: p = d break if p is None: out.append((q, log(q))) continue t = q while t % p == 0: t //= p if t == 1: out.append((q, log(p))) return out def _pair_kernel_matrix(x, M, A, sigma, z, q, lam, E, mu): """N_q and |N_q| summed over h, as (e-window, e-window) arrays, plus the nz-term count. Returns (Nq, Absq, nterms). Index 0 of the matrix corresponds to e = E+1. """ m = np.arange(M + 1, 2 * M + 1, dtype=np.int64) H = np.arange(A, 2 * A + 1) ch = 1.0 / A z0 = x / 2.0 Jmax = int(x ** (7 / 300)) n = E Nq = np.zeros((n, n), dtype=np.complex128) Absq = np.zeros((n, n), dtype=np.float64) nterms = 0 for e1 in range(E + 1, 2 * E + 1): if mu[e1] == 0: continue for e2 in range(E + 1, 2 * E + 1): if mu[e2] == 0: continue j = gcd(e1, e2) if j > Jmax: continue l1, l2 = e1 // j, e2 // j c = q * j * l1 * l2 keep = np.fromiter((gcd(int(mm), c) == 1 for mm in m), bool, m.size) mk = m[keep] inv = np.fromiter((pow(int(mm), -1, c) for mm in mk), np.float64, mk.size) u1, u2 = q * e1, q * e2 hh = H[:, None].astype(np.float64) mm = mk[None, :].astype(np.float64) Phi1 = np.exp(2j * np.pi * hh * z0 / (GG * mm * u1)) - np.exp(2j * np.pi * hh * z / (GG * mm * u1)) Phi2 = np.exp(2j * np.pi * hh * z0 / (GG * mm * u2)) - np.exp(2j * np.pi * hh * z / (GG * mm * u2)) ph1 = np.exp(2j * np.pi * ((sigma * THETA * hh * l2 * inv[None, :]) % c) / c) ph2 = np.exp(-2j * np.pi * ((sigma * THETA * hh * l1 * inv[None, :]) % c) / c) Kmat = (ph1 * Phi1) @ (ph2 * np.conj(Phi2)).T Rmat = H[:, None] * l2 - H[None, :] * l1 nz = Rmat != 0 Nq[e1 - E - 1, e2 - E - 1] = ch * ch * Kmat[nz].sum() Absq[e1 - E - 1, e2 - E - 1] = ch * ch * np.abs(Kmat[nz]).sum() nterms += int(nz.sum()) return Nq, Absq, nterms def kernel_matrices(x, M, Q, A, sigma, z, E): """Per-prime-power h-summed pair kernels, restricted to the squarefree e-support. Returns a dict with qs, lams : the prime powers and Lambda(q) support : the squarefree e values in the window, ascending N : list of (n,n) Hermitian matrices on that support Abs : list of the same matrices with every entry replaced by its modulus beta : the sign vector -mu on the support nterms : total number of (pair, h1, h2) terms with R != 0 """ mu = mobius_upto(2 * E + 1) pps = prime_powers(Q) support = np.array([e for e in range(E + 1, 2 * E + 1) if mu[e] != 0], dtype=np.int64) n = support.size N, Abs = [], [] nterms = 0 for q, lam in pps: full, fullabs, nt = _pair_kernel_matrix(x, M, A, sigma, z, q, lam, E, mu) # `full` is indexed by e - E - 1, not by position in `support`. sel = np.array([int(e) - E - 1 for e in support], dtype=np.int64) N.append(full[np.ix_(sel, sel)]) Abs.append(fullabs[np.ix_(sel, sel)]) nterms += nt beta = (-mu[support]).astype(np.float64) return dict(qs=[q for q, _ in pps], lams=np.array([l for _, l in pps]), support=support, N=N, Abs=Abs, beta=beta, nterms=nterms, E=E) def hermiticity_defect(N): """max |N - N^H| over all matrices in the list, relative to max |N|.""" worst = 0.0 scale = 0.0 for A_ in N: worst = max(worst, float(np.abs(A_ - A_.conj().T).max()) if A_.size else 0.0) scale = max(scale, float(np.abs(A_).max()) if A_.size else 0.0) return worst / (scale if scale else 1.0) def signed_sum(km): """S = sum_q lambda(q) beta^T N_q beta (complex; real for Hermitian N_q).""" tot = 0.0 + 0.0j for lam, A_, b in zip(km['lams'], km['N'], [km['beta']] * len(km['N'])): tot += lam * (b @ (A_ @ b)) return tot def absolute_majorant(km): """A1 of #1075 = sum_q lambda(q) * 1^T |N_q| 1 on the squarefree support.""" tot = 0.0 for lam, A_ in zip(km['lams'], km['Abs']): tot += lam * float(A_.sum()) return tot def operator_bound(km): """A_op = ||beta||_2^2 * sum_q lambda(q) * ||N_q||_op (sign-blind; >= |S|). Also returns the per-q top eigenvalue, the Frobenius norms and the effective ranks. """ n = km['beta'].size nn = float(km['beta'] @ km['beta']) rho, fro, effrank, lamtop = [], [], [], [] for A_ in km['N']: w = np.linalg.eigvalsh(A_) rho.append(float(np.abs(w).max()) if w.size else 0.0) lamtop.append(float(w.max()) if w.size else 0.0) fro.append(float(np.sqrt((np.abs(A_) ** 2).sum()))) s = np.abs(w) effrank.append(float(s.sum() ** 2 / (s ** 2).sum()) if (s ** 2).sum() > 0 else 0.0) Aop = nn * float(sum(l * r for l, r in zip(km['lams'], rho))) return dict(A_op=Aop, nn=nn, rho=rho, fro=fro, effrank=effrank, lamtop=lamtop) def spectral_profile(km): """Decompose S over the eigenpairs of each N_q and measure where beta sits in the spectrum. Returns the exact spectral S, the weight of ||^2 relative to a random sign vector (1/n), and the participation ratio n^2 / sum_k ||^4 (n = uniform, 1 = single mode). """ n = km['beta'].size S = 0.0 + 0.0j prof = [] for lam, A_ in zip(km['lams'], km['N']): w, v = np.linalg.eigh(A_) c = v.conj().T @ km['beta'] wgt = np.abs(c) ** 2 S += lam * float(w @ wgt) k = int(np.argmax(np.abs(w))) rho_q = float(np.abs(w).max()) if w.size else 1e-300 prof.append(dict( lam_min=float(w.min()), lam_max=float(w.max()), align_top=float(np.abs(c[k]) / np.sqrt(n)), participation=float(n * n / (wgt ** 2).sum()) if (wgt ** 2).sum() > 0 else float('nan'), top_share=float(abs(w[k]) * wgt[k] / abs(w @ wgt)) if abs(w @ wgt) > 0 else float('nan'), fro_over_op=float(np.sqrt((np.abs(A_) ** 2).sum()) / rho_q), )) return S, prof def shuffle_shared(km_by_z, nperm=2000, seed=4332): """Shared-draw sign shuffle, after the trusted review of #1075. One sign vector is drawn per replicate and used at every z' (the cells share one kernel family), so the joint statistic over the z' grid is legitimate. Returns, per z', the true |S|/A1, the null sample, and the one-sided p-values P(null >= true) and P(null <= true). """ rng = np.random.default_rng(seed) zs = list(km_by_z.keys()) n = km_by_z[zs[0]]['beta'].size true = np.array([abs(signed_sum(km_by_z[z])) / absolute_majorant(km_by_z[z]) for z in zs]) null = np.zeros((nperm, len(zs))) for it in range(nperm): eps = rng.choice([-1.0, 1.0], size=n) for j, z in enumerate(zs): km = km_by_z[z] tot = 0.0 + 0.0j for lam, A_ in zip(km['lams'], km['N']): tot += lam * (eps @ (A_ @ eps)) null[it, j] = abs(tot) / absolute_majorant(km) out = {} joint_true = float(np.log(true).sum()) joint_null = np.log(null).sum(axis=1) for j, z in enumerate(zs): col = null[:, j] out[z] = dict( true_ratio=float(true[j]), null_median=float(np.median(col)), null_p05=float(np.percentile(col, 5)), null_p95=float(np.percentile(col, 95)), p_upper=float((col >= true[j]).mean()), p_lower=float((col <= true[j]).mean()), ) out['joint'] = dict( true_log_sum=joint_true, p_upper=float((joint_null >= joint_true).mean()), p_lower=float((joint_null <= joint_true).mean()), null_median=float(np.median(joint_null)), nperm=nperm, ) return out