#!/usr/bin/env python3
"""Real-axis zero/pole map of maximal quaternion-order ideal zetas Z_D(s)=zeta(2s)zeta(2s-1)prod_{p|D}(1-p^(1-2s)),
D=2,3,30, in ideal-index coordinate s and reduced-norm coordinate w=2s. 10 Nov 2026 PT.
Prediction (known facts about zeta only): simple zeros at every negative half-integer s=-1/2,-1,-3/2,...;
order r-1 at s=1/2 (r = number of ramified primes); simple pole at s=1; no other real zeros or poles.
Check: 60-digit mpmath scan of s in [-8.2, 0.98] with step 1/400 for sign changes, root refinement,
derivative at each root, and Laurent orders at 1/2 and 1. Numerical check of an analytic statement, NOT an interval certificate."""
import mpmath as mp, json
mp.mp.dps = 60
CASES = {2: [2], 3: [3], 30: [2, 3, 5]}
def Z(s, P): return mp.zeta(2*s)*mp.zeta(2*s-1)*mp.fprod(1-mp.mpf(p)**(1-2*s) for p in P)
LO, HI, STEP = mp.mpf('-8.2'), mp.mpf('0.98'), mp.mpf(1)/400
out = {}
for D, P in CASES.items():
    pred = [-mp.mpf(k)/2 for k in range(1, 17)]  # -1/2 .. -8
    xs = []; s = LO
    while s <= HI:
        xs.append(s); s += STEP
    roots = []; prev = None
    for s in xs:
        if abs(2*s-1) < 1e-30 or abs(2*s) < 1e-30: prev = None; continue
        v = Z(s, P)
        if v == 0: roots.append(s); prev=None; continue
        if prev is not None and mp.sign(v) != mp.sign(prev[1]):
            r = mp.findroot(lambda t: Z(t, P), (prev[0], s), solver='anderson')
            roots.append(r)
        prev = (s, v)
    rec = []
    for r in roots:
        k = mp.nint(2*r)
        rec.append(dict(root=mp.nstr(r, 25), nearest_half_integer=str(k/2), err=mp.nstr(abs(r-k/2), 5),
                        derivative=mp.nstr(mp.diff(lambda t: Z(t, P), r), 15)))
    # half-line behaviour at 1/2: Z(1/2+h) ~ c h^(r-1)
    h = [mp.mpf(10)**-e for e in (8, 12, 16)]
    ordc = [mp.nstr(mp.log(abs(Z(mp.mpf(1)/2+hh, P)))/mp.log(hh), 12) for hh in h]
    lead = Z(mp.mpf(1)/2+h[-1], P)/h[-1]**(len(P)-1)
    pole = [mp.nstr((mp.mpf(1)+hh-1)*Z(1+hh, P), 20) for hh in h]  # residue estimate at s=1
    resid = mp.zeta(2)*mp.fprod(1-mp.mpf(p)**-1 for p in P)/2
    ok = len(roots) == len(pred) and all(abs(a-b) < mp.mpf(10)**-40 for a, b in zip(sorted(roots), sorted(pred)))
    out[str(D)] = dict(ramified=P, n_roots=len(roots), predicted=[str(x) for x in pred], all_match=bool(ok),
        roots=rec, order_at_half_estimates=ordc, predicted_order_at_half=len(P)-1, leading_coeff_at_half=mp.nstr(lead, 15),
        residue_at_1_estimates=pole, predicted_residue_at_1=mp.nstr(resid, 20))
    print(D, 'roots', len(roots), 'match', ok, 'order', ordc, 'lead', mp.nstr(lead,12), 'res', pole[-1], mp.nstr(resid,15), flush=True)
out['scan'] = dict(lo=str(LO), hi=str(HI), step='1/400', dps=60, note='grid sign-change scan; even-order zeros would be invisible to sign changes, but the analytic argument excludes them; numerical check, not interval certificate')
out['coordinates'] = 'reduced-norm w=2s: zeros at every negative integer w=-1,-2,...,-16 in the scanned window, order r-1 at w=1, pole at w=2, functional-equation centre w=1'
json.dump(out, open('order-real-axis-results.json', 'w'), indent=1)
