# Section 6: cooling at zero temperature, in 2 ways, from Haar random orthogonal matrices. (1) the polar iteration U -> polar part of sgn(U), # which increases the 1-norm and stops at the plain local maxima; (2) the gradient ascent U -> U exp(tA), with A = (X^t - X)/2, X = S^t U, # with backtracking, which stays in the connected component of U and can stop at plain, neutral and twisted local maxima. We print the # distribution of the end values, the number of distinct end values, and how often the design matrices are found, and also the # average of the 1-norm over the Haar measure, which is N^2 Gamma(N/2)/(sqrt(pi) Gamma((N+1)/2)). # Usage: python3 ascent.py [runs for the polar iteration, default 2000] [runs for the gradient ascent, default 300] import numpy as np, sys from math import gamma, sqrt, pi from designs import SETS, matrices rng = np.random.default_rng(11) def haar(n): q, r = np.linalg.qr(rng.normal(size=(n, n))); return q*np.sign(np.diag(r)) def polar(S): w, s, vt = np.linalg.svd(S); return w @ vt def quench(U, steps=5000): f = np.abs(U).sum() for _ in range(steps): V = polar(np.sign(U)); g = np.abs(V).sum() if g <= f + 1e-13: break U, f = V, g return U, f def settle(U): # if U is close to the plain or twisted polar part of its sign matrix, jump there S = np.sign(U); w, s, vt = np.linalg.svd(S); d = np.ones(len(U)); d[-1] = -1 for kind, V in (("plain", w @ vt), ("twisted", (w*d) @ vt)): if np.abs(V - U).max() < 1e-3 and np.abs(V).min() > 1e-9 and (np.sign(V) == S).all(): if s[-1] < 1e-9: return "neutral", V if kind == "plain" or s[-2] - s[-1] > 1e-9: return kind, V return None, U def ascent(U, steps=200000): n = len(U); f = np.abs(U).sum(); t = 0.1; I = np.eye(n) for it in range(steps): X = np.sign(U).T @ U; A = (X.T - X)/2 if it % 50 == 0 or np.abs(A).max() < 1e-4: kind, V = settle(U) if kind: return kind, V, np.abs(V).sum() while t > 1e-15: V = U @ np.linalg.solve(I - t*A/2, I + t*A/2); g = np.abs(V).sum() if g > f: U, f = V, g; t *= 1.5; break t /= 2 else: break return "stuck", U, f if __name__ == "__main__": runs = int(sys.argv[1]) if len(sys.argv) > 1 else 2000; runs2 = int(sys.argv[2]) if len(sys.argv) > 2 else 300 best = {v: np.abs(matrices(v, D)[0]).sum() for name, (v, D) in SETS.items()}; twin = {v: np.abs(matrices(v, D)[1]).sum() for name, (v, D) in SETS.items()} for n in (4, 5, 6, 7, 8, 9, 10, 11, 12, 13): vals = np.array([quench(haar(n))[1] for _ in range(runs)])/n**1.5; r = np.round(vals, 7) print("N=%d average of the 1-norm over the Haar measure, divided by N^1.5: %.4f" % (n, sqrt(n)*gamma(n/2)/(sqrt(pi)*gamma((n + 1)/2)))) line = "N=%d polar iteration, %d runs: mean %.4f, best %.4f, %d distinct end values, Hadamard %d times" % (n, runs, vals.mean(), vals.max(), len(set(r)), int((vals > 1 - 1e-7).sum())) if n in best: line += ", design matrix (%.4f) %d times" % (best[n]/n**1.5, int((np.abs(vals - best[n]/n**1.5) < 1e-7).sum())) print(line, flush=True) for n in (4, 5, 6, 7, 8, 9, 10, 11, 12, 13): res = [ascent(haar(n)) for _ in range(runs2)]; kinds = [k for k, U, f in res]; vals = np.array([f for k, U, f in res])/n**1.5 tw = np.array([f for k, U, f in res if k == "twisted"])/n**1.5 line = "N=%d gradient ascent, %d runs: plain %d, neutral %d, twisted %d, stuck %d; mean %.4f, best %.4f, %d distinct end values" % (n, runs2, kinds.count("plain"), kinds.count("neutral"), kinds.count("twisted"), kinds.count("stuck"), vals.mean(), vals.max(), len(set(np.round(vals, 7)))) if len(tw): line += "; twisted values from %.4f to %.4f" % (tw.min(), tw.max()) if n in best: line += "; design matrix %d times, its twin (%.4f) %d times" % (int((np.abs(vals - best[n]/n**1.5) < 1e-7).sum()), twin[n]/n**1.5, int((np.abs(vals - twin[n]/n**1.5) < 1e-7).sum())) print(line, flush=True)