# Sections 2 and 7: census of the local maxima of the 1-norm on O(N), in terms of sign matrices. A sign matrix S with singular value decomposition # S = W D V^t, with singular values s_1 >= ... >= s_N in decreasing order, comes with the 2 orthogonal matrices W V^t and W R V^t, with # R = diag(1,...,1,-1), and these are the candidates for a local maximum with sign matrix S, when s_{N-1} > s_N. We count the sign matrices # S which are the sign matrix of W V^t with s_N > 0 (plain), of W R V^t with s_N > 0 (twisted), and of both with s_N = 0 (neutral). # Exhaustive for N = 3,...,6, with the first row and column positive, and random samples of 2^22 sign matrices for N = 7, 8, 9. # Usage: python3 census.py import numpy as np, time rng = np.random.default_rng(5) def scan(S): w, s, vt = np.linalg.svd(S); P = w @ vt; d = np.ones(S.shape[-1]); d[-1] = -1; T = (w*d) @ vt; ok = s[:, -1] > 1e-9 fp = (np.abs(P).min(axis=(1, 2)) > 1e-9) & (np.sign(P) == S).all(axis=(1, 2)); ft = (np.abs(T).min(axis=(1, 2)) > 1e-9) & (np.sign(T) == S).all(axis=(1, 2)) gap = s[:, -2] - s[:, -1] > 1e-9 return ok & fp, ok & gap & ft, ~ok & gap & fp & ft, ~ok & gap & (fp ^ ft), s def exhaustive(n, batch=1 << 18): m = (n - 1)**2 for start in range(0, 1 << m, batch): idx = np.arange(start, min(start + batch, 1 << m), dtype=np.int64) S = np.ones((len(idx), n, n)); S[:, 1:, 1:] = (((idx[:, None] >> np.arange(m)) & 1)*2.0 - 1).reshape(-1, n - 1, n - 1); yield S def sample(n, total, batch=1 << 17): for _ in range(total//batch): yield rng.choice([-1.0, 1.0], size=(batch, n, n)) def report(n, gen, label): count, np_, nt, nn, odd, bp, bt, bn, t0 = 0, 0, 0, 0, 0, 0, (0, None, None), 0, time.time() for S in gen: plain, twisted, neutral, half, s = scan(S); count += len(S); np_ += int(plain.sum()); nt += int(twisted.sum()); nn += int(neutral.sum()); odd += int(half.sum()) if neutral.any(): bn = max(bn, (s.sum(1)*neutral).max()) if plain.any(): bp = max(bp, (s.sum(1)*plain).max()) if twisted.any(): val = (s.sum(1) - 2*s[:, -1])*twisted; k = val.argmax() if val[k] > bt[0]: bt = (val[k], S[k].copy(), s[k].copy()) print("N=%d, %s, %d sign matrices: plain %d, neutral %d, twisted %d; best norm/N^1.5: plain %.6f, neutral %.6f, twisted %.6f; neutral with only 1 of the 2 matrices fitting: %d (%.0fs)" % (n, label, count, np_, nn, nt, bp/n**1.5, bn/n**1.5, bt[0]/n**1.5, odd, time.time() - t0), flush=True) if bt[1] is not None: print(" best twisted: singular values %s\n S = %s" % (np.round(bt[2], 4), bt[1].astype(int).tolist()), flush=True) if __name__ == "__main__": for n in (3, 4, 5, 6): report(n, exhaustive(n), "all") for n in (7, 8, 9): report(n, sample(n, 1 << 22), "sample")