# Section 7: the maximum of the 1-norm on O(N) equals the maximum of Tr|S| over the sign matrices S of order N, so the search for # the optimal almost Hadamard matrices is the search for the ground states of an Ising model with N^2 spins and energy -Tr|S|. We cool # this model by simulated annealing with single flips, then finish by the alternating maximization S -> sgn(polar part of S), and we # compare the result with the 1-norm of the design matrix. Usage: python3 anneal.py [steps per run, default 300000] [runs, default 4] import numpy as np, sys from designs import SETS, matrices rng = np.random.default_rng(13) def energy(S): return np.linalg.svd(S, compute_uv=False).sum() def polish(S): f = energy(S) while True: w, s, vt = np.linalg.svd(S); T = np.sign(w @ vt); T[T == 0] = 1; g = energy(T) if g <= f + 1e-12: return f, S S, f = T, g def anneal(n, steps, hot=0.6, cold=0.02): S = rng.choice([-1.0, 1.0], size=(n, n)); f = energy(S); best = (f, S.copy()) for t in range(steps): T = hot*(cold/hot)**(t/steps); i, j = rng.integers(n, size=2); S[i, j] = -S[i, j]; g = energy(S) if g >= f or rng.random() < np.exp((g - f)/T): f = g if f > best[0]: best = (f, S.copy()) else: S[i, j] = -S[i, j] return polish(best[1]) if __name__ == "__main__": steps = int(sys.argv[1]) if len(sys.argv) > 1 else 300000; runs = int(sys.argv[2]) if len(sys.argv) > 2 else 4 known = {v: np.abs(matrices(v, D)[0]).sum() for name, (v, D) in SETS.items()} for n in (5, 6, 7, 9, 10, 11, 13, 14): res = [anneal(n, steps) for _ in range(runs)]; f, S = max(res, key=lambda x: x[0]); w, s, vt = np.linalg.svd(S); U = w @ vt line = "N=%d: best Tr|S| = %.6f, ratio %.6f, all runs: %s; fits its polar part: %s" % (n, f, f/n**1.5, " ".join("%.4f" % x[0] for x in res), bool((np.sign(U) == S).all())) if n in known: line += "; design matrix %.6f" % known[n] print(line, flush=True); print(" S =", S.astype(int).tolist(), flush=True)