#!/usr/bin/env python3 """Price the source's large-coordinate budget on the enlarged support. Source (Althoefer 2026, eq. (4)): F is supported on T46 = { t in [0,1]^46 : sum t_i < A + eps_s = 0.2658, sum_{t_i > delta} t_i <= B_{#{t_i>delta}} } with delta = 3/250, B_1 = B_2 = 3/20, B_m = 4/25 (m >= 3). Scaling t = A u turns the support into (1+eta) R_46 with eta = eps_s/A and the budget into delta' = delta/A, B'_r = B_r/A. This module extracts the extremal polynomial F of the engine at degree d and Monte-Carlo estimates the fraction of its L2 mass lying inside / outside the budget region, i.e. the price the budget imposes on the variational value. """ from __future__ import annotations import os import sys from fractions import Fraction as Fr from itertools import product import numpy as np HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) import flint # noqa: E402 from even_engine import EvenEngine # noqa: E402 from flint_chol import whiten # noqa: E402 # -------------------------------------------------------------------------- # monomial symmetric function evaluation (vectorised over sample points) # -------------------------------------------------------------------------- def alpha_states(alpha): from collections import Counter c = Counter(alpha) vals = sorted(c) # distinct nonzero part values mults = [c[v] for v in vals] r = len(vals) idx = {} states = [] for s in product(*[range(m + 1) for m in mults]): idx[s] = len(states) states.append(s) trans = [] # (src, dst, j) with dst = src + e_j for s in states: for j in range(r): if s[j] < mults[j]: t = list(s); t[j] += 1 trans.append((idx[s], idx[tuple(t)], j)) final = idx[tuple(mults)] return vals, mults, states, trans, final def eval_msym(points, alpha, cache): """m_alpha at each row of points (N,k), vectorised DP. Returns (N,).""" key = alpha if key not in cache: cache[key] = alpha_states(alpha) vals, mults, states, trans, final = cache[key] N, k = points.shape S = len(states) dp = np.zeros((N, S)) dp[:, 0] = 1.0 for i in range(k): x = points[:, i] powers = [x ** v for v in vals] new = dp.copy() for (s, d, j) in trans: new[:, d] += dp[:, s] * powers[j] dp = new return dp[:, final] def eval_F(points, coeffs, B, eps): """F = sum_i c_i (1+eps-P1)^{a_i} m_{alpha_i} at each row of points.""" z = 1.0 + float(eps) - points.sum(axis=1) cache = {} out = np.zeros(points.shape[0]) # group by alpha to avoid recomputing m_alpha from collections import defaultdict groups = defaultdict(list) for i, (a, alpha) in enumerate(B): groups[alpha].append((a, coeffs[i])) for alpha, items in groups.items(): m = eval_msym(points, alpha, cache) acc = np.zeros_like(m) for a, c in items: if c != 0.0: acc += c * z ** a out += m * acc return out # -------------------------------------------------------------------------- def top_eigenvector(k, eps, d): """Top generalized eigenvector of the diagonal-scaled pencil (double). Double precision is adequate at the low degrees used for the Monte-Carlo pricing; the value itself is taken from the high-precision sweep. """ from scipy.linalg import eigh e = EvenEngine(k, eps, d, "float") A1, A2 = e.matrices() dg = 1.0 / np.sqrt(np.diag(A1)) S1 = (A1 * dg[:, None]) * dg[None, :] S2 = (A2 * dg[:, None]) * dg[None, :] w, V = eigh(S2, S1) x = V[:, -1] # S2 x = lam S1 x c = dg * x # original-basis coefficients return e, w[-1], c def cap_price(k, eps, d, N=20000, seed=0): e, lam, c = top_eigenvector(k, eps, d) rng = np.random.default_rng(seed) E = rng.exponential(1.0, size=(N, k + 1)) pts = (1.0 + float(eps)) * E[:, :k] / E.sum(axis=1, keepdims=True) F = eval_F(pts, c, e.B, e.eps) delta = 3 / 250 B1 = 3 / 20 Bm = 4 / 25 A = Fr(2583, 10000) def budget_ok(P, delta_c, B1_c, Bm_c): m = P > delta_c s = (P * m).sum(axis=1) r = m.sum(axis=1) cap = np.where(r <= 2, B1_c, Bm_c) return s <= cap, s, r res = {"k": k, "eps": str(eps), "d": d, "n": e.n, "lambda": lam} F2 = F * F tot = F2.sum() pts_t = pts * float(A) ok, s, r = budget_ok(pts_t, float(delta), float(B1), float(Bm)) res["t_coords_mass_inside"] = float(F2[ok].sum() / tot) res["t_coords_mean_sum"] = float(s.mean()) res["t_coords_frac_ok"] = float(ok.mean()) oku, su, ru = budget_ok(pts, float(delta / A), float(B1 / A), float(Bm / A)) res["u_coords_mass_inside"] = float(F2[oku].sum() / tot) res["u_coords_mean_sum"] = float(su.mean()) res["u_coords_frac_ok"] = float(oku.mean()) return res if __name__ == "__main__": import json k = int(sys.argv[1]); eps = Fr(sys.argv[2]); d = int(sys.argv[3]) N = int(sys.argv[4]) if len(sys.argv) > 4 else 20000 out = sys.argv[5] if len(sys.argv) > 5 else None r = cap_price(k, eps, d, N=N) print(json.dumps(r, indent=2)) if out: with open(out, "w") as f: json.dump(r, f, indent=2)