#!/usr/bin/env nix-shell
#!nix-shell -p python312Packages.numpy -i python3
"""nuvmap_spectral_driver.py - Run on neon via nix-shell"""

import numpy as np
import json
from pathlib import Path

def compute_spectral_gap(N: int, p: int) -> dict:
    """Compute spectral gap for BMCTE eigensolid at p/N=1/7."""
    lam = np.exp(-p * p / N)
    
    # Sparse signature matrix (diagonal for now)
    sig_vals = [8704, 9088, 9984, 8704, 9088, 9984, 8704, 9088]
    mat = np.diag(sig_vals)
    
    # Power iteration for dominant eigenvalue
    v = np.random.randn(8)
    v = v / np.linalg.norm(v)
    for _ in range(100):
        Mv = mat @ v
        norm = np.linalg.norm(Mv)
        if norm < 1e-10:
            break
        v = Mv / norm
    
    ev_max = float(v @ mat @ v)
    density = np.count_nonzero(mat) / mat.size
    
    return {
        "N": N, "p": p,
        "lambda_theory": float(lam),
        "spectral_gap": ev_max,
        "density": float(density),
        "at_threshold": abs(p/N - 1/7) < 0.001
    }

if __name__ == "__main__":
    result = compute_spectral_gap(80000, 11429)
    print(json.dumps(result, indent=2))
    Path("neon_spectral_result.json").write_text(json.dumps(result, indent=2))