#!/usr/bin/env python3 # -*- coding: utf-8 -*- """The price lemma's EXACT escape, measured on the (D1) object rather than restated. The lemma (return #912, proved; the (D1) case of it is #902/#905) says R := ||A||_nuc / ||A||_F = 1 + (|c_2|/|c_1|) * v * sqrt(D_mu D_t) + O(v^2), D_mu = (U_2U_4 - U_3^2)/U_2^2 over the ROW family {mu_w}, D_t = (V_2V_4 - V_3^2)/V_2^2 over the COLUMN family {t_m}, so the first-order excess vanishes EXACTLY iff one of the two families is constant (D = 0). This script does three things that a restatement cannot: (1) SPLIT -- which of the two dispersions actually carries the price of the corpus's own box; (2) ESCAPE -- flatten one family exactly and measure that the linear term is GONE (R - 1 ~ v^2); (3) LEGITIMACY-- for the two flattenings a smoothing or averaging choice could actually make (a SHORT window in the inner variable; a narrow SHELL of the ratio family), measure what the flattening BUYS (the price falls) and what it COSTS (the mass it discards), so that "legitimate" is a number and not an adjective. Object, notation and the measured constant are the run's own: rows = the distinct ratios w = h/l of the (A,L)-box, columns = m in (M, 2M], t_m = M/m, phase x = v * mu_w * t_m, profile e(x) - e(rho x) (work/r2-nuc-exact.py, work/r2-nuc-C-analytic.py). """ import cmath import io import math import os import sys from fractions import Fraction as F import numpy as np RUN = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) OUT = os.path.join(RUN, "artifacts", "flat-factor.out") buf = io.StringIO() def say(s=""): print(s) buf.write(s + "\n") def box_ratios(A0, L0, band=None, weighted=False): """The ROW family values w = h/l. Two normalisations, and the conclusion is only safe if it survives BOTH: `weighted=False` gives the DISTINCT ratios (the row family as the lemma's object indexes it, and the one the measured R matched), `weighted=True` gives one row per PAIR (h,l), i.e. each ratio repeated by its multiplicity -- which is what the interface actually sums over. Quoting a "mass kept" from the unweighted normalisation alone would be the same error as reading a price off a bound. """ pairs = [F(h, l) for h in range(A0, 2 * A0) for l in range(L0, 2 * L0)] if band is not None: c = F(A0, L0) pairs = [w for w in pairs if abs(w / c - 1) <= band[0]] return pairs if weighted else sorted(set(pairs)) def moments(A0, L0, M, eps=F(1), band=None, weighted=False): """U_j over the ROW ratios, V_j over the COLUMN window (M, M(1+eps)].""" wtyp = F(A0, L0) mus = [w / wtyp for w in box_ratios(A0, L0, band, weighted)] hi = M + max(1, int(round(float(eps) * M))) ts = [F(M, m) for m in range(M + 1, hi + 1)] U = [sum(mu ** j for mu in mus) / len(mus) for j in range(1, 5)] V = [sum(t ** j for t in ts) / len(ts) for j in range(1, 5)] return U, V, len(mus), len(ts) def dispersions(A0, L0, M, eps=F(1), band=None, weighted=False): U, V, nw, nt = moments(A0, L0, M, eps, band, weighted) Dmu = float((U[1] * U[3] - U[2] ** 2) / U[1] ** 2) Dt = float((V[1] * V[3] - V[2] ** 2) / V[1] ** 2) return Dmu, Dt, nw, nt def C_pred(A0, L0, M, rho, eps=F(1), band=None, weighted=False): U, V, _, _ = moments(A0, L0, M, eps, band, weighted) disc = math.sqrt(float((U[1] * U[3] - U[2] ** 2) * (V[1] * V[3] - V[2] ** 2))) / float(U[1] * V[1]) return math.pi * (1.0 + float(rho)) * disc def measure(v, A0, L0, M, rho, eps=F(1), band=None, flat_rows=False, flat_cols=False, weighted=False): """R = ||A||_nuc/||A||_F, exact integer phase reduction, same construction as r2-nuc-C-analytic.py.""" beta = F(v.numerator * M * L0, v.denominator * A0) ws = box_ratios(A0, L0, band, weighted) hi = M + max(1, int(round(float(eps) * M))) ms = list(range(M + 1, hi + 1)) if flat_rows: ws = [F(A0, L0)] # D_mu = 0 exactly: one ratio if flat_cols: ms = [M] # D_t = 0 exactly: one inner point A = np.empty((len(ws), len(ms)), dtype=complex) for i, w in enumerate(ws): bn, bd = beta.numerator * w.numerator, beta.denominator * w.denominator for j, m in enumerate(ms): m2 = M if flat_cols else m f1 = (bn % (bd * m2)) / (bd * m2) f2 = ((rho.numerator * bn) % (rho.denominator * bd * m2)) / (rho.denominator * bd * m2) A[i, j] = cmath.exp(2j * math.pi * f1) - cmath.exp(2j * math.pi * f2) sv = np.linalg.svd(A, compute_uv=False) return float(sv.sum()) / float(math.sqrt((np.abs(A) ** 2).sum())) def main(): A0, L0, M = 8, 8, 512 rho = F(2, 1) v = F(1, 1000) say("the price lemma's exact escape, measured on the (D1) object -- box A0=L0=8, M=512, rho=2") say("=" * 100) say() say("(1) SPLIT -- which family carries the price? (the price is |c2/c1| v sqrt(D_mu D_t), |c2/c1| = pi(1+rho) = %.4f)" % (math.pi * 3)) say(" %-40s %12s %12s %10s %10s" % ("family", "D_mu", "D_t", "sqrt(DmuDt)", "C_pred")) for name, eps, band, wt in [("full window, full box [distinct ratios]", F(1), None, False), ("full window, full box [per pair]", F(1), None, True), ("column window /2, full box [distinct]", F(1, 2), None, False), ("column window /4, full box [distinct]", F(1, 4), None, False), ("column window /8, full box [distinct]", F(1, 8), None, False), ("full window, shell <= 1/2 [per pair]", F(1), (F(1, 2),), True), ("full window, shell <= 1/4 [per pair]", F(1), (F(1, 4),), True), ("full window, shell <= 1/8 [per pair]", F(1), (F(1, 8),), True), ("full window, shell <= 1/2 [distinct]", F(1), (F(1, 2),), False), ("full window, shell <= 1/4 [distinct]", F(1), (F(1, 4),), False), ("full window, shell <= 1/8 [distinct]", F(1), (F(1, 8),), False)]: Dmu, Dt, nw, nt = dispersions(A0, L0, M, eps, band, wt) say(" %-40s %12.3e %12.3e %10.4f %10.4f" % (name, Dmu, Dt, math.sqrt(Dmu * Dt), C_pred(A0, L0, M, rho, eps, band, wt))) say() say("(2) ESCAPE -- flatten one family EXACTLY (a one-element family is constant, D = 0) and look for the") say(" linear term. If the escape is exact, R - 1 is O(v^2) and (R-1)/v -> 0 while (R-1)/v^2 stays bounded:") say(" %-14s %12s %12s %12s %12s" % ("v", "R-1 (full)", "R-1 (rows flat)", "R-1 (cols flat)", "R-1 (both)")) for vv in [F(1, 1000), F(1, 100), F(1, 30), F(1, 10)]: r_full = measure(vv, A0, L0, M, rho) - 1.0 r_row = measure(vv, A0, L0, M, rho, flat_rows=True) - 1.0 r_col = measure(vv, A0, L0, M, rho, flat_cols=True) - 1.0 r_both = measure(vv, A0, L0, M, rho, flat_rows=True, flat_cols=True) - 1.0 say(" %-14s %12.3e %12.3e %12.3e %12.3e" % (str(vv), r_full, r_row, r_col, r_both)) say(" and the same in the v-normalised units, where the escape shows as a vanishing coefficient:") say(" %-14s %14s %14s %14s" % ("v", "(R-1)/v full", "rows flat", "cols flat")) for vv in [F(1, 1000), F(1, 100)]: say(" %-14s %14.3e %14.3e %14.3e" % (str(vv), (measure(vv, A0, L0, M, rho) - 1) / float(vv), (measure(vv, A0, L0, M, rho, flat_rows=True) - 1) / float(vv), (measure(vv, A0, L0, M, rho, flat_cols=True) - 1) / float(vv))) say() say("(3) LEGITIMACY -- what a smoothing or averaging choice BUYS and what it COSTS.") say(" Column side: shorten the inner window to a fraction eps of the dyadic block.") say(" %-8s %10s %10s %12s %12s %14s" % ("eps", "kept cols", "C_pred", "buy = C(eps)/C(1)", "mass kept", "buy x mass")) base_C = C_pred(A0, L0, M, rho) for eps in [F(1), F(1, 2), F(1, 4), F(1, 8)]: _, _, _, nt = dispersions(A0, L0, M, eps) c = C_pred(A0, L0, M, rho, eps) say(" %-8s %10d %10.4f %12.4f %12.4f %14.4f" % (str(eps), nt, c, c / base_C, float(eps), (c / base_C) * float(eps))) say(" Row side: keep only the ratio shell |w/w_typ - 1| <= delta. 'profit' = mass kept / buy, so") say(" profit > 1 means the shell buys the price back for LESS mass than it saves.") say(" %-8s %10s %10s %12s %12s %10s %10s" % ("delta", "kept rows", "C_pred", "buy", "mass kept", "buy x mass", "profit")) for wt in (False, True): _, _, nw0, _ = dispersions(A0, L0, M, F(1), None, wt) bC = C_pred(A0, L0, M, rho) say(" -- rows normalised %s" % ("per pair" if wt else "by distinct ratio")) for band in [(F(1, 2),), (F(1, 4),), (F(1, 8),)]: _, _, nw, _ = dispersions(A0, L0, M, F(1), band, wt) c = C_pred(A0, L0, M, rho, F(1), band, wt) buy, mass = c / bC, nw / nw0 say(" %-8s %10d %10.4f %12.4f %12.4f %10.4f %10.4f" % (str(band[0]), nw, c, buy, mass, buy * mass, mass / buy)) say() say("=" * 100) say("READING.") say(" * WHICH family carries the price is a measurement, not a guess, and my own first draft of this") say(" sentence had it backwards: for the corpus's box the RATIO family carries the larger dispersion") say(" (D_mu = 1.12e-01 against D_t = 2.08e-02 in the unweighted normalisation), even though the box has") say(" already confined the ratio to a factor-4 window. Both factors are of the same order, and the") say(" price is the geometric mean, so neither can be ignored.") say(" * the escape is EXACT and not merely first order: with one row (or one inner point) the matrix IS") say(" rank one, so nuclear = Frobenius identically and R - 1 = 0 to machine precision at every v --") say(" measured 1e-16 against 4.5e-04 for the full family at v = 1/1000.") say(" * the two flattenings a smoothing can make are NOT equivalent, and the last two tables are the") say(" reason: shortening the inner window is ANTI-optimal (buy = 0.667 while mass = 0.500, so the") say(" price is bought at 1.33x the mass saved, worsening to 1.78x), while the ratio SHELL is") say(" favourable in BOTH normalisations (profit 1.25x and 1.29x at delta = 1/2, 1.26x and 1.62x at") say(" delta = 1/4) -- i.e. the ratios far from typical carry the dispersion without carrying the mass.") say(" * neither is a saving on its own: the discarded part is still discarded, and only an interface that") say(" AVERAGES over that index (so that the discarded part is what the average was already taking) can") say(" keep the price reduction without paying the mass.") with open(OUT, "w", encoding="utf-8", newline="\n") as fh: fh.write(buf.getvalue()) say("wrote %s" % os.path.relpath(OUT, RUN)) return 0 if __name__ == "__main__": sys.exit(main())