#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Controls for work/r2-nuc-exact.py. Is R = 1 + C*v a genuine absolute law? Four controls, each of which could break the conclusion of r2-nuc-exact.py: (1) the box (rank / #distinct w) -- already in the main script; redone larger here. (2) the endpoint ratio rho = z'/z0' -- the corpus has z0 = x/2, z in [x/2,x], so rho in [1,2]; rho = 2 is the extreme, the interior values must be checked. (3) the frequency window M -- the phase is beta w/m with m in (M,2M]; the ratio is scale-invariant but the discretisation is not. (4) the COEFFICIENTS. This is the one that matters. The corpus's coefficient is coeff(pair) = (b_{u1} c_{h1}) * conj(b_{u2} c_{h2}) = alpha_p * conj(alpha_q), with b_u ARBITRARY and only |b_u| <= B. The note's bound (2) is uniform in b_u, so the economy must survive an arbitrary rank-one reweighting of the pair family -- including wildly unequal and adversarially-phased alpha. If it does not, the economy is a property of equal coefficients and not of the object. """ import cmath import math import random from fractions import Fraction import numpy as np from importlib.machinery import SourceFileLoader _m = SourceFileLoader("nuc", "work/r2-nuc-exact.py").load_module() box_ratios, profile_matrix, spectrum, frac_mod = _m.box_ratios, _m.profile_matrix, _m.spectrum, _m.frac_mod def psi_columns(v, ws, M, rho): """|I| x len(ws) matrix of Psi_w(m) = e(beta w/m) - e(rho beta w/m).""" A0, L0 = 8, 8 beta = Fraction(v.numerator * M * L0, v.denominator * A0) ms = list(range(M + 1, 2 * M + 1)) X = np.empty((len(ms), len(ws)), dtype=complex) for j, m in enumerate(ms): for i, w in enumerate(ws): bn = beta.numerator * w.numerator bd = beta.denominator * w.denominator f1 = frac_mod(bn, bd * m) f2 = frac_mod(rho.numerator * bn, rho.denominator * bd * m) X[j, i] = cmath.exp(2j * math.pi * f1) - cmath.exp(2j * math.pi * f2) return X def pair_matrix(X, alphas): """|I| x d^2 matrix, column (p,q) = alpha_p conj(alpha_q) * X[:,p] conj(X[:,q]).""" d = X.shape[1] cols = [] for p in range(d): for q in range(d): cols.append(alphas[p] * np.conj(alphas[q]) * X[:, p] * np.conj(X[:, q])) return np.array(cols).T def lapack_nuc(V): """exact SVD nuclear norm and Frobenius norm of a (possibly tall) matrix. tall matrices: use the smaller Gram side so the exact spectrum is the same. """ rank, nuc, fro, sv = spectrum(V) return nuc, fro, nuc / fro def main(): M0 = 512 ws8 = box_ratios(8, 8) rho2 = Fraction(2, 1) print("=" * 88) print("CONTROL 2: the endpoint ratio rho = z'/z0' (corpus: rho in [1,2])") print("=" * 88) print("%-14s %10s %10s %10s %10s" % ("v", "rho=5/4", "rho=3/2", "rho=2", "rho=5/2")) for v in [Fraction(1, 100), Fraction(1, 10), Fraction(1, 2), Fraction(1, 1), Fraction(4, 1)]: row = [] for rho in [Fraction(5, 4), Fraction(3, 2), Fraction(2, 1), Fraction(5, 2)]: V, _ = profile_matrix(v, 8, 8, M0, rho) _, _, R = lapack_nuc(V) row.append(R) print("%-14s %10.4f %10.4f %10.4f %10.4f" % (str(v), *row)) print() print(" small-v constants C = (R-1)/v:") for v in [Fraction(1, 1000), Fraction(1, 100)]: row = [] for rho in [Fraction(5, 4), Fraction(3, 2), Fraction(2, 1), Fraction(5, 2)]: V, _ = profile_matrix(v, 8, 8, M0, rho) _, _, R = lapack_nuc(V) row.append((R - 1) / float(v)) print(" v=%-8s %8.4f %8.4f %8.4f %8.4f" % (str(v), *row)) print() print("=" * 88) print("CONTROL 3: the frequency window M (beta scales with M so v is held fixed)") print("=" * 88) print("%-14s %10s %10s %10s %10s (band A0=8, L0=8, rho=2)" % ("v", "M=128", "M=512", "M=2048", "M=8192")) for v in [Fraction(1, 100), Fraction(1, 10), Fraction(1, 1), Fraction(10, 1)]: row = [] for M in [128, 512, 2048, 8192]: V, _ = profile_matrix(v, 8, 8, M, rho2) _, _, R = lapack_nuc(V) row.append(R) print("%-14s %10.4f %10.4f %10.4f %10.4f" % (str(v), *row)) print(" (C = (R-1)/v):") for v in [Fraction(1, 100), Fraction(1, 1000)]: row = [] for M in [128, 512, 2048, 8192]: V, _ = profile_matrix(v, 8, 8, M, rho2) _, _, R = lapack_nuc(V) row.append((R - 1) / float(v)) print(" v=%-8s %8.4f %8.4f %8.4f %8.4f" % (str(v), *row)) print() print("=" * 88) print("CONTROL 4: ARBITRARY COEFFICIENTS. coeff(pair) = alpha_p * conj(alpha_q),") print("the rank-one reweighting the corpus's |b_u| <= B allows. Pair family, d=23.") print("=" * 88) rng = random.Random(20260917) d = 23 ws = ws8[:d] print("%-10s %-26s %10s %10s %10s" % ("v", "weights alpha_p", "R (equal)", "R (weighted)", "sqrt(rank)")) for v in [Fraction(1, 100), Fraction(1, 10), Fraction(1, 2), Fraction(1, 1), Fraction(5, 1)]: X = psi_columns(v, ws, M0, rho2) V0 = pair_matrix(X, np.ones(d, dtype=complex)) _, _, R_equal = lapack_nuc(V0) for label, al in [ ("|a|=1, random phase", np.array([cmath.exp(2j * math.pi * rng.random()) for _ in range(d)])), ("lognormal |a| x random ph", np.array([math.exp(rng.gauss(0, 1.5)) * cmath.exp(2j * math.pi * rng.random()) for _ in range(d)])), ("a_p = (-1)^p", np.array([(-1.0) ** p for p in range(d)], dtype=complex)), ]: V1 = pair_matrix(X, al) _, _, R_w = lapack_nuc(V1) print("%-10s %-26s %10.4f %10.4f %10.3f" % (str(v), label, R_equal, R_w, math.sqrt(d * d))) print() print(" reading: if the weighted column stays ~equal to the unweighted one, the economy is") print(" a property of the OBJECT (the double-index product structure), not of the weights.") return 0 if __name__ == "__main__": raise SystemExit(main())