"""Riemann zero re-verification, round 4 (3,000 zeros).

Same pipeline as compute/riemann_verify_r2.py:
1. LOCATION (PARI/GP 2.19.1, C): lfunzeros on the Riemann zeta
   L-function. The first 2,000 zeros are reused from round 2
   (data/riemann_zeros_r2.json t values, themselves from round 1
   [0, 1420] and round 2 [1420, 2560]); zeros 2,001-2,042 are the
   leftover 42 values of the round 2 upper scan file
   (data/riemann_t_r2_upper.txt, which held 1,042 zeros of which the
   first 1,000 were used in round 2); zeros 2,043-3,000 come from a
   third scan on [2560, 3550] (data/riemann_t_r4_upper.txt,
   compute/riemann_scan_r4_upper.gp). The full [0, 3550] scan was not
   attempted: the full [0, 2560] scan in round 2 overflowed the
   default 8 MB PARI stack, so the interval was split instead.
   The first 2,000 t values of the combined list are checked for an
   exact match against the round 2 file.
2. REAL-PART CHECK (mpmath, Python, dps 30): for each PARI t value, one
   Newton step from s0 = 0.5 + i t using zeta and zeta prime. The step is
   free to move off the line; the reported real part is what it gives.
   Also records |zeta(0.5 + i t)| per zero.
3. COUNT CHECK: number of zeros found vs the Riemann-von Mangoldt count
   N(T) at T = t_3000. A missed off-line zero would show as a mismatch.

Run: ./pari/bin/gp -q < compute/riemann_scan_r4_upper.gp > data/riemann_t_r4_upper.txt
     (first 2,000 t values already in data/riemann_zeros_r2.json)
     then python3 compute/riemann_verify_r4.py
   The Newton loop checkpoints every 100 zeros to
   data/riemann_verify_r4_progress.json and resumes from it, so a
   killed run restarts where it left off instead of from zero.
   The checkpoint is deterministic per zero, so a resumed run
   produces the same results as an uninterrupted one.
"""
import hashlib, json, time
from pathlib import Path
from mpmath import mp, mpf, mpc, zeta, pi, log

mp.dps = 30
DATA = Path(__file__).resolve().parent.parent / "data"
PROGRESS = DATA / "riemann_verify_r4_progress.json"
N_TARGET = 3000
N_PREVIOUS = 2000
SCAN_R1_SECONDS = 44.46
SCAN_R2_SECONDS = 907.3


def main():
    started = time.time()
    r2 = json.loads((DATA / "riemann_zeros_r2.json").read_text())
    assert r2["zeros_checked"] == N_PREVIOUS, "round-2 file must hold 2,000 zeros"
    lower = [mpf(z["t_bisect"]) for z in r2["zeros"]]
    assert len(lower) == N_PREVIOUS
    mid_lines = (DATA / "riemann_t_r2_upper.txt").read_text().split()
    mid_count = int(mid_lines[0])
    mid_all = [mpf(x) for x in mid_lines[1:]]
    assert len(mid_all) == mid_count == 1042, "round-2 upper file must hold 1,042 zeros"
    mid = mid_all[N_PREVIOUS - 1000:]
    assert len(mid) == 42, "round-2 upper file must hold 42 zeros past the first 2,000"
    assert mid[0] > lower[-1], "middle zeros must continue above the round-2 last zero"
    upper_lines = (DATA / "riemann_t_r4_upper.txt").read_text().split()
    upper_count = int(upper_lines[0])
    upper = [mpf(x) for x in upper_lines[1:]]
    assert len(upper) == upper_count, f"count line {upper_count} != values {len(upper)}"
    assert upper[0] > mid[-1], "upper scan must continue above the middle zeros"
    assert len(lower) + len(mid) + len(upper) >= N_TARGET, (
        f"combined {len(lower) + len(mid) + len(upper)} zeros, need {N_TARGET}")
    t_values = (lower + mid + upper)[:N_TARGET]

    results = []
    max_re_dev = mpf('0')
    max_abs_zeta = mpf('0')
    worst_i = -1
    elapsed_prior = 0.0
    if PROGRESS.exists():
        prog = json.loads(PROGRESS.read_text())
        assert len(prog["results"]) < N_TARGET, "progress file already complete"
        assert all(prog["results"][i]["t_bisect"] == mp.nstr(t_values[i], 25)
                   for i in range(len(prog["results"]))), "progress t values mismatch"
        results = prog["results"]
        max_re_dev = mpf(prog["max_re_deviation"])
        max_abs_zeta = mpf(prog["max_abs_zeta_at_line"])
        worst_i = prog["worst_zero_n"] - 1
        elapsed_prior = prog["elapsed_seconds"]
        print(f"resuming from checkpoint: {len(results)} zeros done", flush=True)
    for i in range(len(results), N_TARGET):
        tn = t_values[i]
        s0 = mpc(mpf('0.5'), tn)
        s1 = s0 - zeta(s0) / zeta(s0, derivative=1)
        re_dev = abs(s1.real - mpf('0.5'))
        az = abs(zeta(s0))
        if re_dev > max_re_dev:
            max_re_dev, worst_i = re_dev, i
        if az > max_abs_zeta:
            max_abs_zeta = az
        results.append({
            "n": i + 1,
            "t_bisect": mp.nstr(tn, 25),
            "re": mp.nstr(s1.real, 25),
            "im": mp.nstr(s1.imag, 25),
            "re_deviation": mp.nstr(re_dev, 5),
            "abs_zeta_at_line": mp.nstr(az, 5),
        })
        if (i + 1) % 100 == 0:
            PROGRESS.write_text(json.dumps({
                "results": results,
                "max_re_deviation": mp.nstr(max_re_dev, 5),
                "max_abs_zeta_at_line": mp.nstr(max_abs_zeta, 5),
                "worst_zero_n": worst_i + 1,
                "elapsed_seconds": round(elapsed_prior + time.time() - started, 2),
            }))
            print(f"checkpoint: {i + 1} zeros done", flush=True)
    total_seconds = elapsed_prior + time.time() - started

    # Exact-match check of the first 2,000 t values against round 2.
    first2000_match = all(
        results[i]["t_bisect"] == r2["zeros"][i]["t_bisect"]
        for i in range(N_PREVIOUS))

    scan_file = DATA / "riemann_scan_r4_upper_seconds.txt"
    scan_seconds = float(scan_file.read_text().strip()) if scan_file.exists() else None

    T = t_values[-1]
    expected = (T / (2 * pi)) * log(T / (2 * pi)) - T / (2 * pi) + mpf('0.875')
    s_of_T = N_TARGET - float(expected)

    out = {
        "run": "riemann-zero-verification-r4",
        "date": "2026-11-20",
        "method": "Location: PARI/GP 2.19.1 lfunzeros on the Riemann zeta L-function, realprecision 30. First 2,000 zeros reused from round 2 (intervals [0, 1420] and [1420, 2560]); zeros 2,001-2,042 are the 42 leftover values of the round 2 upper scan; zeros 2,043-3,000 from a third scan on [2560, 3550]. Real-part check: one mpmath Newton step per zero from 0.5 + i t using zeta and zeta prime, dps 30, free to move off the line.",
        "zeros_checked": N_TARGET,
        "first_2000_match_round2": first2000_match,
        "t_first": mp.nstr(t_values[0], 25),
        "t_last": mp.nstr(t_values[-1], 25),
        "max_re_deviation": mp.nstr(max_re_dev, 5),
        "worst_zero_n": worst_i + 1,
        "max_abs_zeta_at_line": mp.nstr(max_abs_zeta, 5),
        "von_mangoldt_expected_at_T": float(expected),
        "S_of_T": s_of_T,
        "scan_seconds": scan_seconds,
        "scan_seconds_round1_reused": SCAN_R1_SECONDS,
        "scan_seconds_round2_reused": SCAN_R2_SECONDS,
        "total_seconds": round(total_seconds, 2),
        "zeros": results,
    }
    p = DATA / "riemann_zeros_r4.json"
    p.write_text(json.dumps(out, indent=2))
    print(json.dumps({k: v for k, v in out.items() if k != "zeros"}, indent=2))
    print("results_sha256:", hashlib.sha256(p.read_bytes()).hexdigest())


if __name__ == "__main__":
    main()
