#!/usr/bin/env python3 """Haar starts and monotone geodesic ascent for the entrywise 1-norm on U_4. Run on macOS with NumPy: caffeinate -i python3 ahc_ascent.py Three workers, seven cumulative active hours; rerun from the same directory to resume. Read ahc-ascent-out/summary.txt. Each committed start and endpoint, all diagnostics and the two spectra at regular endpoints are in results.sqlite, table runs. Complex blobs are 4x4 C-order little-endian complex128; spectra are ascending little-endian float64 of lengths 16 and 9. To inspect an endpoint in Python: blob = db.execute("select endpoint from runs where id=?", (run_id,)).fetchone()[0] u = np.frombuffer(blob, dtype="...k", a, basis).real def horizontal(u): # Remove four right phases, then three independent left phases. left = adj(u)[:, None] @ BASIS[:3] @ u[:, None] columns = coordinates(left, OFF).swapaxes(1, 2) return np.linalg.qr(columns, mode="complete")[0][:, :, 3:] def expstep(u, b): eig, v = np.linalg.eigh(b) return u @ ((v * np.exp(1j * eig)[:, None, :]) @ adj(v)) def hessian(u): """Geodesic second derivatives for U exp(i t B), tr(B^2)=1.""" s, x, _ = components(u) w = ((u[:, None] @ BASIS) * s[:, None].conj()).real w = (w / np.sqrt(abs(u))[:, None]).reshape(-1, 16, 16) y = (x + adj(x)) / 2 h = w @ w.swapaxes(1, 2) - coordinates(y, PRODUCTS).reshape(-1, 16, 16) h = (h + h.swapaxes(1, 2)) / 2 c = horizontal(u) reduced = c.swapaxes(1, 2) @ h[:, 4:, 4:] @ c return np.linalg.eigvalsh(h), np.linalg.eigvalsh(reduced) def metrics(u): return (float(abs(u).sum()), float(np.linalg.norm(components(u)[2])), float(abs(u).min()), float(abs(abs(u) - .5).max()), float(np.linalg.norm(adj(u) @ u - EYE))) def calibration(): x = .609609789010948 b = np.sqrt((1 - x*x) / 3) u = np.full((4, 4), b, complex) u[0, 0] = x row = (-x + 2j * np.sin(2 * np.pi * np.arange(3) / 3)) / 3 for j in range(3): u[j+1, 1:] = np.roll(row, j) return u def self_check(): u = calibration() lval, g, _, _, unity = metrics(u) full, reduced = hessian(u[None]) if (abs(lval - 7.63761518684038) > 1e-12 or max(g, unity) > 1e-13 or abs(reduced[0, -1] - 2.876887) > 1e-5 or sum(full[0] > CURV_TOL) != 1 or sum(abs(full[0]) < CURV_TOL) != 7): raise RuntimeError("Order-four saddle calibration failed") f = np.exp(2j * np.pi * np.outer(np.arange(4), np.arange(4)) / 4) / 2 if hessian(f[None])[0].max() > 1e-12: raise RuntimeError("Fourier Hessian calibration failed") rng = np.random.default_rng(19) v = haar(rng, 1) b = rng.normal(size=(4, 4)) + 1j * rng.normal(size=(4, 4)) b = (b + adj(b)) / 2 b /= np.linalg.norm(b) step = .0001 plus, minus = [abs(expstep(v, sign * step * b[None])).sum() for sign in (1, -1)] phase, x, residual = components(v[0]) grad = -np.trace(residual @ b).real q = (((v[0] @ b * phase.conj()).real ** 2) / abs(v[0])).sum() q -= np.trace(x @ b @ b).real if (abs((plus-minus)/(2*step) - grad) > 1e-6 or abs((plus+minus-2*abs(v).sum())/step**2 - q) > 2e-5): raise RuntimeError("Geodesic finite difference check failed") # Unaccelerated search direction at every step is the exact Riemannian gradient. # A safeguarded spectral step and Armijo backtracking only change its length. BATCH = 16 MAX_STEPS = 1800 def starts_for(rng, size, batch_id): return "haar", haar(rng, size), None def solve(starts, policy): u = starts.copy() n = len(u) steps = np.zeros(n, dtype=int) scale = np.full(n, .5) active = np.ones(n, dtype=bool) for _ in range(MAX_STEPS): idx = np.flatnonzero(active) if not len(idx): break v = u[idx] _, _, r = components(v) g2 = np.sum(abs(r)**2, axis=AX) spread = abs(abs(v) - .5).max(axis=AX) go = (g2 > GRAD_TOL**2) & (spread > FLAT_TOL) & (abs(v).min(axis=AX) > 1e-13) active[idx[~go]] = False idx, v, r, g2 = idx[go], v[go], r[go], g2[go] # On U_4, sum((|u|-.5)^2) = 8-L(u). This avoids cancellation near L=8. deficit = np.sum((abs(v)-.5)**2, axis=AX) pending = np.arange(len(idx)) for _ in range(24): if not len(pending): break rows = idx[pending] t = scale[rows] proposal = expstep(v[pending], -t[:, None, None] * r[pending]) new_deficit = np.sum((abs(proposal)-.5)**2, axis=AX) good = deficit[pending] - new_deficit >= 1e-4*t*g2[pending] if np.any(good): accepted = rows[good] q = pending[good] u[accepted] = proposal[good] steps[accepted] += 1 new_r = components(proposal[good])[2] curvature = np.sum((r[q].conj()*(r[q]-new_r)).real, axis=AX) # For this inner product the previous gradient transports to itself. scale[accepted] = np.clip(t[good]*g2[q]/np.maximum(curvature, 1e-25), .05, 100.) scale[rows[~good]] *= .5 pending = pending[~good] active[idx[pending]] = False # preserve a line-search stall for inspection return u, np.zeros(n, dtype=int), steps def pack(a, dtype): return np.asarray(a, dtype=dtype).tobytes(order="C") def classify(lval, grad, minimum, spread, unity, top): if unity > 1e-10: return "unitarity_failure" if minimum <= ENTRY_TOL: return "near_zero" if spread <= FLAT_TOL: return "near_flat" if grad > GRAD_TOL: return "unresolved" if top <= CURV_TOL: return "candidate" return "nonflat_uphill" def run_batch(seed, batch_id): rng = np.random.default_rng(np.random.SeedSequence([seed, batch_id])) family, starts, policy = starts_for(rng, BATCH, batch_id) endpoints = [] def process(block, offset): # A failed batched factorization must not discard its healthy neighbors. try: u, warm, steps = solve(block, policy) stats = [metrics(v) for v in u] regular = [k for k, m in enumerate(stats) if m[2] > ENTRY_TOL and m[4] <= 1e-10] spectra = {} if regular: full, reduced = hessian(u[regular]) spectra = dict(zip(regular, zip(full, reduced))) rows = [] for k, (v, m) in enumerate(zip(u, stats)): if not np.isfinite(m).all(): raise FloatingPointError("nonfinite endpoint") top = fblob = rblob = None if k in spectra: fs, rs = spectra[k] if not np.isfinite(fs).all() or not np.isfinite(rs).all(): raise FloatingPointError("nonfinite spectrum") top = float(rs[-1]) fblob, rblob = pack(fs, " 1: for j in range(len(block)): process(block[j:j+1], offset+j) else: # The exact starting matrix is retained even when LAPACK returns no endpoint. endpoints.append((batch_id*BATCH+offset, family, "numeric_error", "", None, None, None, None, None, None, None, None, pack(block[0], "= stop_at: report(db, folder, "Requested time already reached", base, config) return report(db, folder, "Starting/resuming", base, config) # Find gaps from an interrupted run before issuing any new IDs. known = {row[0] for row in db.execute("SELECT id FROM batches ORDER BY id")} last_id = max(known, default=-1) gaps = iter(i for i in range(last_id+1) if i not in known) fresh = last_id+1 stopped = False def stop(signum, frame): nonlocal stopped stopped = True signal.signal(signal.SIGINT, stop) signal.signal(signal.SIGTERM, stop) print("Output:", folder.resolve(), "| workers:", args.workers, "| progress every minute | Ctrl-C saves and stops after active batches.", flush=True) last_print = began with ProcessPoolExecutor(args.workers, mp_context=multiprocessing.get_context("spawn"), initializer=ignore_interrupt) as pool: jobs = {} issued = 0 def fill(): nonlocal fresh, issued while not stopped and elapsed() < stop_at and len(jobs) < args.workers: if args.batches is not None and len(known) + issued >= args.batches: break i = next(gaps, None) if i is None: i, fresh = fresh, fresh+1 jobs[pool.submit(run_batch, args.seed, i)] = i issued += 1 fill() while jobs: done, _ = wait(jobs, timeout=2., return_when=FIRST_COMPLETED) for task in done: batch_id = jobs.pop(task) commit_batch(db, batch_id, task.result(), elapsed()) if time.monotonic()-last_print >= 60: with db: set_meta(db, "seconds", elapsed()) report(db, folder, "Running", elapsed(), config) last_print = time.monotonic() fill() total = elapsed() with db: set_meta(db, "seconds", total) report(db, folder, "Paused" if stopped else "Finished", total, config) def main(): p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) p.add_argument("--hours", type=float, default=7., help="total active campaign hours, including earlier sessions") p.add_argument("--workers", type=int, default=3, help="parallel processes (default: 3)") p.add_argument("--seed", type=int, default=DEFAULT_SEED) p.add_argument("--out", type=Path, default=Path("ahc-"+MODE+"-out")) p.add_argument("--batches", type=int, help="optional total limit, 16 starts per batch") p.add_argument("--self-test", action="store_true", help="check derivatives and calibration only") args = p.parse_args() if (not math.isfinite(args.hours) or args.hours <= 0 or args.workers < 1 or args.seed < 0 or (args.batches is not None and args.batches < 1)): p.error("hours, workers and batches must be positive; seed must be nonnegative") self_check() print("Order-four saddle, Fourier matrix and finite differences: OK.", flush=True) if args.self_test: return args.out.mkdir(parents=True, exist_ok=True) with (args.out/"run.lock").open("a") as lock: try: fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) except BlockingIOError: p.error("another process is using this output folder") with closing(sqlite3.connect(args.out/"results.sqlite")) as db: campaign(args, db, args.out) if __name__ == "__main__": main()