#!/usr/bin/env python3 """Polymath8b Lemma 7.2 engine: the even-signature basis for M_{k,eps}. Basis (Polymath8b, Sec. 7 / arXiv:1407.4897) -------------------------------------------- A *signature* is a partition alpha = (alpha_1 >= alpha_2 >= ...); P_alpha is the monomial symmetric function m_alpha. The trial functions are b_{a,alpha}(t) = (1 + eps - P_(1))^a P_alpha(t), a >= 0, with alpha running over the *even* signatures (all parts even) and total degree a + |alpha| <= d. For eps = 0 this is the plain simplex problem M_k. Functionals (Polymath8b Thm 3.12) --------------------------------- I(F) = int_{(1+eps) R_k} F^2, J_{i,1-eps}(F) = int_{(1-eps) R_{k-1}} ( int_0^inf F dt_i )^2, M_{k,eps} = sup sum_i J_{i,1-eps}(F) / I(F). For F = sum a_i b_i this is the largest generalised eigenvalue of (M2, M1): M1[i][j] = int_{(1+eps)R_k} b_i b_j, M2[i][j] = k int_{(1-eps)R_{k-1}} (int_0^{1+eps-sigma} b_i dt_k) (int_0^{1+eps-sigma} b_j dt_k). Exact reduction (Lemma bfi / Beta identity) ------------------------------------------- K_m(e, P_lam) = int_{R_m} (1-P1)^e P_lam = e! (#perms) prod(lam!) / (|lam| + m + e)! K_m(e, P_lam P_mu) = e! T_m(lam,mu) / (|lam|+|mu|+m+e)! T_m(lam,mu) = sum_{alpha in orbit(lam), beta in orbit(mu)} prod_i (alpha_i+beta_i)! = sum over nonneg integer matrices n with row sums mult_lam, column sums mult_mu of (m!/prod n!) prod (p+q)!^n_{pq}. Marginal over t_k, one part value at a time: int_0^{1+eps-sigma} (1+eps-P1)^a P_lam dt_k = sum_{v in values(lam pad k)} [a! v!/(a+v+1)!] (1+eps-sigma)^{a+v+1} P_{lam minus one v}. """ from __future__ import annotations from collections import Counter from fractions import Fraction as Fr from functools import lru_cache from math import comb, factorial from typing import Dict, List, Tuple Partition = Tuple[int, ...] # -------------------------------------------------------------------------- # partitions # -------------------------------------------------------------------------- @lru_cache(maxsize=None) def partitions(n: int, maxpart: int | None = None) -> Tuple[Partition, ...]: if maxpart is None: maxpart = n if n == 0: return ((),) out: List[Partition] = [] for p in range(min(n, maxpart), 0, -1): for rest in partitions(n - p, p): out.append((p,) + rest) return tuple(out) def even_partitions_upto(d: int) -> List[Partition]: """All even partitions (every part even) of degree <= d, incl. the empty one.""" out: List[Partition] = [()] for s in range(1, d // 2 + 1): for lam in partitions(s): out.append(tuple(2 * x for x in lam)) return out @lru_cache(maxsize=None) def perm_count(m: int, lam: Partition) -> int: parts = list(lam) + [0] * (m - len(lam)) c = Counter(parts) r = factorial(m) for v in c.values(): r //= factorial(v) return r # -------------------------------------------------------------------------- # T_m(lam, mu) # -------------------------------------------------------------------------- _Tcache: Dict[Tuple[int, Partition, Partition], int] = {} def Tval(m: int, lam: Partition, mu: Partition) -> int: key = (m, lam, mu) hit = _Tcache.get(key) if hit is not None: return hit if len(lam) > m or len(mu) > m: _Tcache[key] = 0 return 0 cl = Counter(list(lam) + [0] * (m - len(lam))) cm = Counter(list(mu) + [0] * (m - len(mu))) rows = sorted(cl) rmul = [cl[v] for v in rows] cols = sorted(cm) cmul = [cm[v] for v in cols] r, s = len(rows), len(cols) @lru_cache(maxsize=None) def rec(j: int, rem: Tuple[int, ...]) -> int: if j == s: return 1 if not any(rem) else 0 q = cols[j] dj = cmul[j] R = sum(rem) if dj > R: return 0 total = 0 def go(i: int, left: int, acc: List[int]) -> None: nonlocal total if i == r: if left: return w = factorial(dj) v = 1 for t in range(r): ni = acc[t] w //= factorial(ni) v *= factorial(rows[t] + q) ** ni nr = tuple(rem[t] - acc[t] for t in range(r)) total += w * v * rec(j + 1, nr) return for ni in range(min(rem[i], left) + 1): acc.append(ni) go(i + 1, left - ni, acc) acc.pop() go(0, dj, []) return total * comb(R, dj) res = rec(0, tuple(rmul)) _Tcache[key] = res return res def _prodfact(lam: Partition) -> int: r = 1 for p in lam: r *= factorial(p) return r def K_prodF(m: int, e: int, lam: Partition, mu: Partition): return Fr(factorial(e) * Tval(m, lam, mu), factorial(sum(lam) + sum(mu) + m + e)) # -------------------------------------------------------------------------- # engine # -------------------------------------------------------------------------- class EvenEngine: def __init__(self, k: int, eps: Fr, d: int, kind: str = "float"): if k < 2: raise ValueError("k >= 2") self.k, self.eps, self.d, self.kind = k, eps, d, kind parts = even_partitions_upto(d) B: List[Tuple[int, Partition]] = [] for lam in parts: deg = sum(lam) for a in range(0, d - deg + 1): B.append((a, lam)) B.sort(key=lambda x: (x[0] + sum(x[1]), sum(x[1]), x[1], x[0])) self.B = B self.n = len(B) self.parts = parts # value sets and sub-partitions self.vals = {} self.subs = {} for lam in parts: c = Counter(list(lam) + [0] * (k - len(lam))) vs = sorted(c) self.vals[lam] = vs self.subs[lam] = {v: self._sub(lam, v) for v in vs} self._build_tables() @staticmethod def _sub(lam: Partition, v: int) -> Partition: if v == 0: return lam lst = list(lam) lst.remove(v) return tuple(sorted(lst, reverse=True)) def _build_tables(self): if self.kind == "float": conv = float elif self.kind == "exact": conv = lambda x: Fr(x) elif self.kind == "fmpq": from flint import fmpq def conv(x): if isinstance(x, Fr): return fmpq(x.numerator, x.denominator) return fmpq(x) else: raise ValueError(self.kind) self.conv = conv self.Tk = {} self.Tkm = {} for lam in self.parts: for mu in self.parts: self.Tk[(lam, mu)] = conv(Tval(self.k, lam, mu)) self.Tkm[(lam, mu)] = conv(Tval(self.k - 1, lam, mu)) # Q(A,c0) = sum_e C(A,e)(2eps)^{A-e}(1-eps)^e e!/(c0+k-1+e)! eps = Fr(self.eps) Amax = 2 * self.d + 2 c0max = 2 * self.d f2e = conv(2 * eps) f1e = conv(1 - eps) self.Q = {} for A in range(Amax + 1): for c0 in range(c0max + 1): s = Fr(0) for e in range(A + 1): s += (Fr(comb(A, e)) * (2 * eps) ** (A - e) * (1 - eps) ** e * Fr(factorial(e), factorial(c0 + self.k - 1 + e))) self.Q[(A, c0)] = conv(s) self.f2e, self.f1e = f2e, f1e # (1+eps)^power table base = conv(1 + eps) self.pw = [conv(Fr(1 + eps) ** p) for p in range(self.k + 2 * self.d + 2)] self.cfac = {} for lam in self.parts: for a in range(0, self.d - sum(lam) + 1): self.cfac[(a, lam)] = {v: conv(Fr(factorial(a) * factorial(v), factorial(a + v + 1))) for v in self.vals[lam]} def M1(self, i: int, j: int): a, lam = self.B[i] c, mu = self.B[j] e = a + c T = self.Tk[(lam, mu)] sc = self.pw[self.k + a + c + sum(lam) + sum(mu)] if self.kind == "float": return sc * (factorial(e) / factorial(sum(lam) + sum(mu) + self.k + e)) * T return sc * self.conv(Fr(factorial(e), factorial(sum(lam) + sum(mu) + self.k + e))) * T def M2(self, i: int, j: int): a, lam = self.B[i] c, mu = self.B[j] zero = 0.0 if self.kind == "float" else self.conv(0) tot = zero f1e = self.f1e for v in self.vals[lam]: ca = self.cfac[(a, lam)][v] g = self.subs[lam][v] for w in self.vals[mu]: cb = self.cfac[(c, mu)][w] dd = self.subs[mu][w] A = a + v + 1 + c + w + 1 c0 = sum(g) + sum(dd) term = ca * cb * self.Tkm[(g, dd)] * self.Q[(A, c0)] if self.kind == "float": term *= (1 - float(self.eps)) ** (self.k - 1 + c0) else: term *= f1e ** (self.k - 1 + c0) tot += term if self.kind == "float": return self.k * tot return self.conv(self.k) * tot def matrices(self, progress=False): n = self.n if self.kind == "float": import numpy as np A1 = np.empty((n, n)); A2 = np.empty((n, n)) else: A1 = [[Fr(0)] * n for _ in range(n)] A2 = [[Fr(0)] * n for _ in range(n)] for i in range(n): if progress and i % 250 == 0: print(" row", i, "/", n, flush=True) for j in range(i, n): v1 = self.M1(i, j); v2 = self.M2(i, j) if self.kind == "float": A1[i, j] = A1[j, i] = v1 A2[i, j] = A2[j, i] = v2 else: A1[i][j] = A1[j][i] = v1 A2[i][j] = A2[j][i] = v2 self.A1, self.A2 = A1, A2 return A1, A2 def float_matrices(self): if self.kind == "float": return self.matrices() import numpy as np A1 = np.array([[float(x) for x in r] for r in self.A1]) A2 = np.array([[float(x) for x in r] for r in self.A2]) return A1, A2 def arb_matrices(self, prec: int = 192, progress: bool = False): """Exact rational entries lifted to python-flint arb_mat at `prec` bits.""" import flint from flint import arb, arb_mat flint.ctx.prec = prec n = self.n A1 = arb_mat(n, n) A2 = arb_mat(n, n) for i in range(n): if progress and i % 250 == 0: print(" arb row", i, "/", n, flush=True) for j in range(i, n): a1 = arb(self.M1(i, j)) a2 = arb(self.M2(i, j)) A1[i, j] = a1; A1[j, i] = a1 A2[i, j] = a2; A2[j, i] = a2 return A1, A2 def eig_float(A1, A2): from scipy.linalg import eigh return eigh(A2, A1, eigvals_only=True)[-1] if __name__ == "__main__": import sys k = int(sys.argv[1]) if len(sys.argv) > 1 else 5 d = int(sys.argv[2]) if len(sys.argv) > 2 else 3 eps = Fr(sys.argv[3]) if len(sys.argv) > 3 else Fr(0) kind = sys.argv[4] if len(sys.argv) > 4 else "float" e = EvenEngine(k, eps, d, kind) print("k", k, "eps", eps, "d", d, "n", e.n, kind) A1, A2 = e.matrices(progress=True) if kind == "float": print("M =", eig_float(A1, A2)) else: F1 = [[float(x) for x in r] for r in A1] F2 = [[float(x) for x in r] for r in A2] print("M =", eig_float(F1, F2))