← Session 35 — ii′-6-R: Lagarias 6.1 right-tail re-proof (E-032/D-073)
thinking (4637 chars)
Now write the E-032 script. Design summary (from my analysis):
**E-032 — the true complex right-tail horizontal integral at T = √n vs its envelope**
- Integrand: f(w) = k_n(w−1)·(−ζ′/ζ(w)), w = σ+iT, σ ∈ [3, 2√n] (right part).
- −ζ′/ζ: Dirichlet series Σ_{k≤K(σ)} Λ(k)k^{−w} primary for σ ≥ 5 (certified tail < 1e-11); mpmath for σ ∈ [3, 5), validated against Dirichlet at σ ∈ {3, 3.5, 4, 4.5, 5}.
- T = c√n + ε, ε zero-avoiding from a deterministic scan; c ∈ {1, 1.5, 2}.
- n ∈ {10³, 10⁴, 10⁵}.
- Trapezoid h = 0.05 and 0.02 (convergence check); report Re, Im, |I|, envelope, ratio.
- For c = 1: also mid part [−5/2, 3] (mpmath) → full I_II.
- Checks: V0 envelope matches E-031; VA validation; VC convergence; V3 repair envelope collapse.
Let me write it carefully.
Key functions:
- lam_sieve(Kmax): smallest prime factor sieve → Λ(k) = log p if k = p^a else 0, as mpf logs cached.
- tail_cert(sig, K): [log(K−1)/(sig−1) + 1/(sig−1)²]·(K−1)^{1−sig}, K−1 ≥ 2.
- choose_K(sig, tol): scan.
- dser(sig, T, tol): Σ_{k=2}^{K} lam[k]·k^{−(sig+iT)} — use mp.power(k, −w) or exp(−w·logk) with cached logk. exp route: mpc exp of (−logk·sig − i·logk·T): mp.exp(−w·logk). Cache logk as mpf.
- mp_neg_ld(w): −mp.zeta(w, derivative=1)/mp.zeta(w).
- kernel(n, sig, T): (1 + 1/(sig−1+iT))**n − 1.
- integrand(n, sig, T, mode): kernel × D.
- zero-avoiding ε: zeros near T0: find index range: estimate count via zetazero... simplest: binary search using mp.zetazero(j).im around T0. N(T) ≈ ...; use mp.zetazero on a bracket: find j0 = largest j with zetazero(j).im < T0 − 2... Actually simpler: j_approx = int((T0/(2π))·log(T0/(2π·e))) then scan j in [j_approx−20, j_approx+20] collecting those with |im − T0| ≤ 3. Deterministic.
- ε scan: for eps in {0, 0.05, ..., 0.95}: clearance = min over collected γ of |T0+eps − γ|; pick max clearance (ties → smallest eps).
- integrate(n, T, a, b, h, mode_fn): N = ceil((b−a)/h); h_eff = (b−a)/N; trapezoid complex + envelope.
- mode_fn: for right part: sig < 5 → mpmath, else Dirichlet(tol=1e-11). For mid part: mpmath.
For speed, note the right part at h=0.02, n=10⁵: [3, 632.4]: N = 31472; of these σ<5: 100 points mpmath; σ≥5: 31372 Dirichlet evals with K(σ): σ=5 → 700 terms, decaying; total term-evals ≈ Σ over grid ~ maybe 31372 × avg 20 ≈ 6e5 × ~20µs = 12 s. h=0.05 grid: ~5 s. Mid part mpmath: 275 evals ×2 (zeta+zeta') ×2 grids = ~1100 zeta evals at t=316, dps 50 — could be ~100 ms each = 110 s. Hmm. Total across all (n,c): right parts: 9 combos × (5–15 s) ≈ 90 s; mpmath [3,5]: 9 × 100×2×0.05 s ≈ 90 s; mid parts (c=1): 3 × 110 s ≈ 5.5 min. Total ~8 min. Detached, fine. Could trim mid part to h=0.05 only... but convergence check wants two grids. Keep; it's detached.
Actually — mp.zeta at dps 50 with t=632 (c=2, n=10⁵): Euler–Maclaurin cost ~ O(t)? mpmath zeta uses Euler–Maclaurin with ~ (t/2π)^{1/2}... no, the main sum is to N ~ t/2π? For the Riemann–Siegel it's √; mpmath uses Euler–Maclaurin: N ~ max(44·(dps/15)... terms + M ~ t/2π. At t = 632, M ~ 100 — cheap. Fine.
Also — validation spot checks use Dirichlet tol=1e-8 at σ=3 → K ≈ 2e4: one-off ~1 s. ✓ sieve to 25000 (K(3, 1e-8): (K−1)² > 1e8·(log(K−1)/2+1/4): K−1 = 2e4: LHS 4e8, RHS 1e8·(9.9/2+0.25) = 5.2e8 — not quite; K−1 = 2.3e4: LHS 5.3e8 ✓. So K(3, 1e-8) ≈ 2.3e4. Sieve to 25000.
For σ ≥ 5 tol=1e-11: K(5) = 700. Sieve 25000 covers everything.
mpmath validation comparison tolerance: |mp − dser| ≤ 1e-6·max(1, |D|) — report actual.
Checks:
- V0: envelope at (10⁴, c=1) within [0.9e4, 1.2e4] (E-031: 1.05e4); at (10⁵, c=1) within [4e10, 8e10] (E-031: 5.87e10).
- VA: max discrepancy ≤ 1e-6 (relative-ish).
- VC: |I_h − I_{h/2}| ≤ max(1e-4·|I_{h/2}|, 1e-4·√n·...) — report; check |delta| ≤ 1e-3·max(|I|, envelope·1e-3...). Keep simple: |delta| ≤ 1e-4·envelope AND |delta| ≤ 0.01·|I| if |I| > 0 — hmm if I is tiny by cancellation the relative check fails spuriously. Use: |delta| ≤ 1e-4·envelope (absolute-ish, scaled). That's the meaningful scale. If |I| ~ envelope that's 1e-4 relative ✓; if I ≪ envelope it's still a fine absolute bound.
- V3: envelope ratio Env(c=2)/Env(c=1) < 1e-6 at n=10⁵ (E-031 V3 says the product at c=2 is e^{−43} vs e^{21} — envelope ratio ~e^{−60}-ish, definitely < 1e-6). And |I_right(c=2)| small — report.
The verdict fields in JSON: for c=1 per n: Re(I_right), Im(I_right), |I_right|, envelope, |I|/env, Re/env, |I|/(√n·log n) maybe. Plus mid/full for c=1.
Output CSV: one row per (n, c) with the right-part data + mid/full for c=1.
Now write the file. Also cache convention: skip if JSON exists and no --force.
After the run: register, write D-073 §2, commit.
Let me write the script.
Writing the E-032 script now. Design: Dirichlet series (certified tail) as primary for σ ≥ 5, mpmath for σ ∈ [3,5) validated against it; trapezoid with two grids for convergence; n ∈ {10³, 10⁴, 10⁵}, c ∈ {1, 1.5, 2}; full mid-segment for c = 1.
tool: Write
input
{
"file_path": "D:\\Python\\Quantum\\research\\numerics\\scripts\\E-032-horizontal-true-integral.py",
"content": "#!/usr/bin/env python3\n\"\"\"E-032 \u2014 F-D side thread ii\u2032-6-R, sub-stage 6-R-0 (deliverable D1):\nthe TRUE complex right-tail horizontal integral of Lagarias's Thm 6.1\ncontour at T = sqrt(n) (zero-avoiding) vs the E-031 envelope.\n\nQUESTION (D-073 \u00a70): is the per-side claim |I_II| = O(sqrt(n)) FALSE at\nT = sqrt(n) \u2014 i.e. is the true complex integral\n I_right = int_{3}^{2 sqrt(n)} k_n(w-1) . (-zeta'/zeta(w)) d sigma,\n w = sigma + iT,\nof the SAME order as the envelope int |...| d sigma (~5.9e10 at n=1e5,\nE-031), or is it MUCH SMALLER via oscillation (hidden cancellation)?\n\nMETHOD (dps 50):\n * -zeta'/zeta(w) for sigma >= 5: TRUNCATED DIRICHLET SERIES\n sum_{k<=K} Lambda(k)/k^w with K chosen per sigma so the certified\n tail bound [log(K-1)/(sig-1) + 1/(sig-1)^2] . (K-1)^{1-sig} < 1e-11.\n Exact (this IS -zeta'/zeta for re > 1), no library zeta involved.\n * sigma in [3, 5): mpmath -zeta'/zeta (moderate args, trustworthy),\n VALIDATED against the Dirichlet series at sigma in {3, 3.5, 4, 4.5, 5}\n (tol 1e-8, K ~ 2.3e4 at sigma=3).\n * kernel k_n(w-1) = (1 + 1/(w-1))^n - 1, direct mpmath power.\n * T = c.sqrt(n) + eps, eps from a deterministic zero-avoiding scan\n (max-clearance grid point, zeros via mp.zetazero).\n * Composite trapezoid, two grids h = 0.05 / 0.02; the h-delta is the\n quadrature-error estimate. Integrand analytic, |phase'| <= ~2\n rad/unit, endpoints tiny vs peak => near-spectral convergence.\n\nCHECKS:\n V0: the refined envelope at c=1 reproduces E-031 (n=1e4: ~1.05e4;\n n=1e5: ~5.9e10).\n VA: mpmath-vs-Dirichlet validation max discrepancy <= 1e-6.\n VC: trapezoid h-delta <= 1e-4 . envelope.\n V3: the repair collapses the envelope: Env(c=2)/Env(c=1) < 1e-6 (n=1e5).\n\nVERDICT FIELDS (reported, not auto-decided): Re/Im/|I_right|, envelope,\n|I|/envelope, and for c=1 also the mid part [-5/2, 3] (mpmath zeta) and\nthe full I_II = I_mid + I_right.\n\"\"\"\nimport json\nimport math\nimport sys\nfrom pathlib import Path\n\nimport mpmath as mp\n\nmp.mp.dps = 50\n\nDATA = Path(__file__).resolve().parent.parent / \"data\"\nCSV = DATA / \"E-032-horizontal-true-integral.csv\"\nJSON = DATA / \"E-032-horizontal-true-integral.json\"\n\nSIEVE_MAX = 25000\nTOL_DIR = mp.mpf(\"1e-11\") # Dirichlet tail certificate target (sigma >= 5)\nTOL_SPOT = mp.mpf(\"1e-8\") # validation spot checks (sigma in {3..5})\nNS = [10 ** 3, 10 ** 4, 10 ** 5]\nCS = [1.0, 1.5, 2.0]\n\n# ---------------------------------------------------------------- von Mangoldt\n_spf = list(range(SIEVE_MAX + 1))\nfor _i in range(2, int(SIEVE_MAX ** 0.5) + 1):\n if _spf[_i] == _i:\n for _j in range(_i * _i, SIEVE_MAX + 1, _i):\n if _spf[_j] == _j:\n _spf[_j] = _i\nLAM = [mp.mpf(0)] * (SIEVE_MAX + 1) # Lambda(k) as mpf log p, else 0\nLOGK = [mp.mpf(0)] * (SIEVE_MAX + 1)\nfor _k in range(2, SIEVE_MAX + 1):\n LOGK[_k] = mp.log(_k)\n _p = _spf[_k]\n _m = _k\n while _m % _p == 0:\n _m //= _p\n if _m == 1:\n LAM[_k] = LOGK[_p]\n\n\ndef tail_cert(sig, K):\n \"\"\"Certified bound on sum_{k>K} Lambda(k)/k^sig (sig >= 3, K >= 3):\n <= integral_{K-1}^inf log x / x^sig dx (summand decreasing).\"\"\"\n if K <= 3:\n return mp.inf\n u = mp.mpf(K - 1)\n return (mp.log(u) / (sig - 1) + 1 / (sig - 1) ** 2) * u ** (1 - sig)\n\n\ndef choose_K(sig, tol):\n K = 4\n while tail_cert(sig, K) >= tol:\n K += 1\n if K > SIEVE_MAX:\n raise RuntimeError(f\"K exceeds sieve at sig={sig}\")\n return K\n\n\ndef dser(sig, T, tol=TOL_DIR):\n \"\"\"-zeta'/zeta(sig + iT) via the truncated Dirichlet series, re > 1.\"\"\"\n K = choose_K(sig, tol)\n w = mp.mpc(sig, T)\n s = mp.mpc(0)\n for k in range(2, K + 1):\n if LAM[k]:\n s += LAM[k] * mp.exp(-w * LOGK[k])\n return s, K\n\n\ndef mp_neg_ld(sig, T):\n w = mp.mpc(sig, T)\n return -mp.zeta(w, derivative=1) / mp.zeta(w)\n\n\ndef neg_ld(sig, T):\n \"\"\"Primary -zeta'/zeta: mpmath on [3,5), Dirichlet series on [5,inf).\"\"\"\n if sig < 5:\n return mp_neg_ld(sig, T), 0\n return dser(sig, T)\n\n\ndef kernel(n, sig, T):\n return (1 + 1 / mp.mpc(sig - 1, T)) ** n - 1\n\n\ndef integrand(n, sig, T):\n d, _ = neg_ld(sig, T)\n return kernel(n, sig, T) * d\n\n\ndef zeros_near(T0, half=3.0):\n \"\"\"Zero ordinates within [T0-half, T0+half] (deterministic).\"\"\"\n j_approx = int((T0 / (2 * math.pi)) * math.log(T0 / (2 * math.pi * math.e)))\n out = []\n for j in range(max(1, j_approx - 30), j_approx + 30):\n g = mp.zetazero(j).imag\n if abs(g - T0) <= half:\n out.append(g)\n return out\n\n\ndef choose_eps(T0):\n \"\"\"Deterministic zero-avoiding eps in {0, .05, ..., .95}.\"\"\"\n zs = zeros_near(T0)\n best = (-1, 0.0)\n for i in range(20):\n eps = i * 0.05\n cl = min((abs(T0 + eps - g) for g in zs), default=mp.mpf(half := 3.0))\n if cl > best[0]:\n best = (cl, eps)\n return best[1], best[0]\n\n\ndef integrate(n, T, a, b, h, use_mid=False):\n \"\"\"Trapezoid for I = int_a^b f d sigma and the envelope int |f|.\"\"\"\n N = int(math.ceil((b - a) / h))\n he = (b - a) / N\n tot = mp.mpc(0)\n env = mp.mpf(0)\n for i in range(N + 1):\n sig = a + i * he\n if use_mid or sig < 5:\n f = kernel(n, sig, T) * mp_neg_ld(sig, T)\n else:\n f = integrand(n, sig, T)\n wt = mp.mpf(\"0.5\") if i in (0, N) else mp.mpf(1)\n tot += wt * f\n env += wt * abs(f)\n return tot * he, env * he\n\n\ndef right_part(n, T, h):\n return integrate(n, T, 3.0, 2 * mp.sqrt(n), h)\n\n\ndef main() -> int:\n if JSON.exists() and \"--force\" not in sys.argv:\n print(f\"cached: {JSON} (use --force to recompute)\")\n return 0\n\n rows = []\n results = {}\n max_val_disc = mp.mpf(0)\n\n for n in NS:\n sq = mp.sqrt(n)\n for c in CS:\n T0 = c * sq\n eps, clearance = choose_eps(T0)\n T = T0 + eps\n # ---- validation (mpmath vs Dirichlet spot checks) ----\n val = {}\n for sig in [3.0, 3.5, 4.0, 4.5, 5.0]:\n dm, _k = dser(mp.mpf(sig), T, tol=TOL_SPOT)\n dd = mp_neg_ld(sig, T)\n disc = abs(dm - dd)\n val[sig] = disc\n if disc > max_val_disc:\n max_val_disc = disc\n # ---- right part, two grids ----\n I1, E1 = right_part(n, T, 0.05)\n I2, E2 = right_part(n, T, 0.02)\n rec = {\n \"n\": n, \"c\": c, \"T\": T, \"eps\": eps, \"clearance\": clearance,\n \"I_right\": I2, \"env\": E2, \"hdelta\": abs(I1 - I2),\n \"env_hdelta\": abs(E1 - E2),\n \"val_max\": max(val.values()),\n }\n # ---- mid part + full side (c = 1 only) ----\n if c == 1.0:\n M1, ME1 = integrate(n, T, -2.5, 3.0, 0.05, use_mid=True)\n M2, ME2 = integrate(n, T, -2.5, 3.0, 0.02, use_mid=True)\n rec.update({\"I_mid\": M2, \"env_mid\": ME2,\n \"mid_hdelta\": abs(M1 - M2),\n \"I_full\": M2 + I2})\n results[(n, c)] = rec\n print(f\"n={n} c={c}: T={mp.nstr(T,8)} eps={eps} \"\n f\"|I_right|={mp.nstr(abs(I2),4)} env={mp.nstr(E2,4)} \"\n f\"ratio={mp.nstr(abs(I2)/E2,3)} hdelta={mp.nstr(rec['hdelta'],2)}\",\n flush=True)\n rows.append({\n \"check\": \"right_part\", \"n\": str(n), \"c\": mp.nstr(c, 3),\n \"T\": mp.nstr(T, 10), \"eps\": str(eps),\n \"clearance\": mp.nstr(clearance, 4),\n \"Re_I_right\": mp.nstr(I2.real, 8),\n \"Im_I_right\": mp.nstr(I2.imag, 8),\n \"abs_I_right\": mp.nstr(abs(I2), 6),\n \"envelope\": mp.nstr(E2, 6),\n \"abs_over_env\": mp.nstr(abs(I2) / E2, 4),\n \"Re_over_env\": mp.nstr(abs(I2.real) / E2, 4),\n \"abs_over_sqrtn\": mp.nstr(abs(I2) / sq, 4),\n \"hdelta\": mp.nstr(rec[\"hdelta\"], 3),\n \"val_max_disc\": mp.nstr(rec[\"val_max\"], 3),\n \"abs_I_mid\": mp.nstr(abs(rec[\"I_mid\"]), 6) if c == 1.0 else \"\",\n \"envelope_mid\": mp.nstr(rec[\"env_mid\"], 6) if c == 1.0 else \"\",\n \"Re_I_full\": mp.nstr(rec[\"I_full\"].real, 8) if c == 1.0 else \"\",\n \"abs_I_full\": mp.nstr(abs(rec[\"I_full\"]), 6) if c == 1.0 else \"\",\n })\n\n # ---------------------------------------------------------------- checks\n r = results\n v0 = (mp.mpf(\"0.9e4\") < r[(10 ** 4, 1.0)][\"env\"] < mp.mpf(\"1.2e4\")) and \\\n (mp.mpf(\"4e10\") < r[(10 ** 5, 1.0)][\"env\"] < mp.mpf(\"8e10\"))\n va = max_val_disc <= mp.mpf(\"1e-6\")\n vc = all(rec[\"hdelta\"] <= mp.mpf(\"1e-4\") * rec[\"env\"] for rec in r.values())\n v3 = r[(10 ** 5, 2.0)][\"env\"] / r[(10 ** 5, 1.0)][\"env\"] < mp.mpf(\"1e-6\")\n\n DATA.mkdir(exist_ok=True)\n with CSV.open(\"w\") as f:\n f.write(\",\".join(rows[0].keys()) + \"\\n\")\n for row in rows:\n f.write(\",\".join(row.values()) + \"\\n\")\n\n verdict = {}\n for n in NS:\n rec = r[(n, 1.0)]\n verdict[str(n)] = {\n \"Re_I_right\": mp.nstr(rec[\"I_right\"].real, 8),\n \"Im_I_right\": mp.nstr(rec[\"I_right\"].imag, 8),\n \"abs_I_right\": mp.nstr(abs(rec[\"I_right\"]), 6),\n \"envelope_right\": mp.nstr(rec[\"env\"], 6),\n \"abs_over_env\": mp.nstr(abs(rec[\"I_right\"]) / rec[\"env\"], 4),\n \"abs_over_sqrtn\": mp.nstr(abs(rec[\"I_right\"]) / mp.sqrt(n), 4),\n \"abs_I_mid\": mp.nstr(abs(rec[\"I_mid\"]), 6),\n \"envelope_mid\": mp.nstr(rec[\"env_mid\"], 6),\n \"Re_I_full\": mp.nstr(rec[\"I_full\"].real, 8),\n \"abs_I_full\": mp.nstr(abs(rec[\"I_full\"]), 6),\n \"abs_I_right_c1.5\": mp.nstr(abs(r[(n, 1.5)][\"I_right\"]), 6),\n \"abs_I_right_c2\": mp.nstr(abs(r[(n, 2.0)][\"I_right\"]), 6),\n }\n sidecar = {\n \"experiment\": \"E-032\",\n \"title\": \"F-D side thread 6-R-0: the TRUE complex right-tail horizontal \"\n \"integral of Lagarias Thm 6.1 at T = sqrt(n) vs the E-031 envelope\",\n \"session\": \"039\",\n \"thread\": \"ii-prime-6-R\",\n \"design_doc\": \"D-073\",\n \"finding_doc\": \"D-072\",\n \"checks\": {\"V0_envelope_reproduces_E031\": bool(v0),\n \"VA_mpmath_vs_dirichlet_le_1e-6\": bool(va),\n \"VC_trapezoid_convergence\": bool(vc),\n \"V3_repair_envelope_collapses\": bool(v3)},\n \"all_checks_pass\": bool(v0 and va and vc and v3),\n \"dps\": 50,\n \"method\": \"-zeta'/zeta by truncated Dirichlet series (certified tail \"\n \"1e-11) for sigma >= 5; mpmath validated against it at \"\n \"{3,3.5,4,4.5,5} for sigma in [3,5); trapezoid h=0.05/0.02.\",\n \"verdict_inputs\": verdict,\n \"notes\": \"Verdict rule (D-073): |I_right| ~ envelope => per-side claim \"\n \"FALSE at T = sqrt(n); |I_right| << envelope => hidden \"\n \"cancellation (STOP and report).\",\n }\n with JSON.open(\"w\") as f:\n json.dump(sidecar, f, indent=2)\n print(f\"V0 envelope~E031: {bool(v0)} | VA validation: {bool(va)} \"\n f\"(max {mp.nstr(max_val_disc,2)}) | VC convergence: {bool(vc)} | \"\n f\"V3 repair: {bool(v3)}\")\n print(f\"wrote {CSV} + {JSON}\")\n return 0 if sidecar[\"all_checks_pass\"] else 1\n\n\nif __name__ == \"__main__\":\n sys.exit(main())\n"
}
raw content_json (database row)
[{"type":"thinking","thinking":"Now write the E-032 script. Design summary (from my analysis):\n\n**E-032 — the true complex right-tail horizontal integral at T = √n vs its envelope**\n\n- Integrand: f(w) = k_n(w−1)·(−ζ′/ζ(w)), w = σ+iT, σ ∈ [3, 2√n] (right part).\n- −ζ′/ζ: Dirichlet series Σ_{k≤K(σ)} Λ(k)k^{−w} primary for σ ≥ 5 (certified tail < 1e-11); mpmath for σ ∈ [3, 5), validated against Dirichlet at σ ∈ {3, 3.5, 4, 4.5, 5}.\n- T = c√n + ε, ε zero-avoiding from a deterministic scan; c ∈ {1, 1.5, 2}.\n- n ∈ {10³, 10⁴, 10⁵}.\n- Trapezoid h = 0.05 and 0.02 (convergence check); report Re, Im, |I|, envelope, ratio.\n- For c = 1: also mid part [−5/2, 3] (mpmath) → full I_II.\n- Checks: V0 envelope matches E-031; VA validation; VC convergence; V3 repair envelope collapse.\n\nLet me write it carefully.\n\nKey functions:\n- lam_sieve(Kmax): smallest prime factor sieve → Λ(k) = log p if k = p^a else 0, as mpf logs cached.\n- tail_cert(sig, K): [log(K−1)/(sig−1) + 1/(sig−1)²]·(K−1)^{1−sig}, K−1 ≥ 2.\n- choose_K(sig, tol): scan.\n- dser(sig, T, tol): Σ_{k=2}^{K} lam[k]·k^{−(sig+iT)} — use mp.power(k, −w) or exp(−w·logk) with cached logk. exp route: mpc exp of (−logk·sig − i·logk·T): mp.exp(−w·logk). Cache logk as mpf.\n- mp_neg_ld(w): −mp.zeta(w, derivative=1)/mp.zeta(w).\n- kernel(n, sig, T): (1 + 1/(sig−1+iT))**n − 1.\n- integrand(n, sig, T, mode): kernel × D.\n- zero-avoiding ε: zeros near T0: find index range: estimate count via zetazero... simplest: binary search using mp.zetazero(j).im around T0. N(T) ≈ ...; use mp.zetazero on a bracket: find j0 = largest j with zetazero(j).im < T0 − 2... Actually simpler: j_approx = int((T0/(2π))·log(T0/(2π·e))) then scan j in [j_approx−20, j_approx+20] collecting those with |im − T0| ≤ 3. Deterministic.\n- ε scan: for eps in {0, 0.05, ..., 0.95}: clearance = min over collected γ of |T0+eps − γ|; pick max clearance (ties → smallest eps). \n- integrate(n, T, a, b, h, mode_fn): N = ceil((b−a)/h); h_eff = (b−a)/N; trapezoid complex + envelope.\n- mode_fn: for right part: sig < 5 → mpmath, else Dirichlet(tol=1e-11). For mid part: mpmath.\n\nFor speed, note the right part at h=0.02, n=10⁵: [3, 632.4]: N = 31472; of these σ<5: 100 points mpmath; σ≥5: 31372 Dirichlet evals with K(σ): σ=5 → 700 terms, decaying; total term-evals ≈ Σ over grid ~ maybe 31372 × avg 20 ≈ 6e5 × ~20µs = 12 s. h=0.05 grid: ~5 s. Mid part mpmath: 275 evals ×2 (zeta+zeta') ×2 grids = ~1100 zeta evals at t=316, dps 50 — could be ~100 ms each = 110 s. Hmm. Total across all (n,c): right parts: 9 combos × (5–15 s) ≈ 90 s; mpmath [3,5]: 9 × 100×2×0.05 s ≈ 90 s; mid parts (c=1): 3 × 110 s ≈ 5.5 min. Total ~8 min. Detached, fine. Could trim mid part to h=0.05 only... but convergence check wants two grids. Keep; it's detached.\n\nActually — mp.zeta at dps 50 with t=632 (c=2, n=10⁵): Euler–Maclaurin cost ~ O(t)? mpmath zeta uses Euler–Maclaurin with ~ (t/2π)^{1/2}... no, the main sum is to N ~ t/2π? For the Riemann–Siegel it's √; mpmath uses Euler–Maclaurin: N ~ max(44·(dps/15)... terms + M ~ t/2π. At t = 632, M ~ 100 — cheap. Fine.\n\nAlso — validation spot checks use Dirichlet tol=1e-8 at σ=3 → K ≈ 2e4: one-off ~1 s. ✓ sieve to 25000 (K(3, 1e-8): (K−1)² > 1e8·(log(K−1)/2+1/4): K−1 = 2e4: LHS 4e8, RHS 1e8·(9.9/2+0.25) = 5.2e8 — not quite; K−1 = 2.3e4: LHS 5.3e8 ✓. So K(3, 1e-8) ≈ 2.3e4. Sieve to 25000.\n\nFor σ ≥ 5 tol=1e-11: K(5) = 700. Sieve 25000 covers everything.\n\nmpmath validation comparison tolerance: |mp − dser| ≤ 1e-6·max(1, |D|) — report actual.\n\nChecks:\n- V0: envelope at (10⁴, c=1) within [0.9e4, 1.2e4] (E-031: 1.05e4); at (10⁵, c=1) within [4e10, 8e10] (E-031: 5.87e10).\n- VA: max discrepancy ≤ 1e-6 (relative-ish).\n- VC: |I_h − I_{h/2}| ≤ max(1e-4·|I_{h/2}|, 1e-4·√n·...) — report; check |delta| ≤ 1e-3·max(|I|, envelope·1e-3...). Keep simple: |delta| ≤ 1e-4·envelope AND |delta| ≤ 0.01·|I| if |I| > 0 — hmm if I is tiny by cancellation the relative check fails spuriously. Use: |delta| ≤ 1e-4·envelope (absolute-ish, scaled). That's the meaningful scale. If |I| ~ envelope that's 1e-4 relative ✓; if I ≪ envelope it's still a fine absolute bound.\n- V3: envelope ratio Env(c=2)/Env(c=1) < 1e-6 at n=10⁵ (E-031 V3 says the product at c=2 is e^{−43} vs e^{21} — envelope ratio ~e^{−60}-ish, definitely < 1e-6). And |I_right(c=2)| small — report.\n\nThe verdict fields in JSON: for c=1 per n: Re(I_right), Im(I_right), |I_right|, envelope, |I|/env, Re/env, |I|/(√n·log n) maybe. Plus mid/full for c=1.\n\nOutput CSV: one row per (n, c) with the right-part data + mid/full for c=1.\n\nNow write the file. Also cache convention: skip if JSON exists and no --force.\n\nAfter the run: register, write D-073 §2, commit.\n\nLet me write the script.","signature":""},{"type":"text","text":"Writing the E-032 script now. Design: Dirichlet series (certified tail) as primary for σ ≥ 5, mpmath for σ ∈ [3,5) validated against it; trapezoid with two grids for convergence; n ∈ {10³, 10⁴, 10⁵}, c ∈ {1, 1.5, 2}; full mid-segment for c = 1."},{"type":"tool_use","id":"Write_12","name":"Write","input":{"file_path":"D:\\Python\\Quantum\\research\\numerics\\scripts\\E-032-horizontal-true-integral.py","content":"#!/usr/bin/env python3\n\"\"\"E-032 — F-D side thread ii′-6-R, sub-stage 6-R-0 (deliverable D1):\nthe TRUE complex right-tail horizontal integral of Lagarias's Thm 6.1\ncontour at T = sqrt(n) (zero-avoiding) vs the E-031 envelope.\n\nQUESTION (D-073 §0): is the per-side claim |I_II| = O(sqrt(n)) FALSE at\nT = sqrt(n) — i.e. is the true complex integral\n I_right = int_{3}^{2 sqrt(n)} k_n(w-1) . (-zeta'/zeta(w)) d sigma,\n w = sigma + iT,\nof the SAME order as the envelope int |...| d sigma (~5.9e10 at n=1e5,\nE-031), or is it MUCH SMALLER via oscillation (hidden cancellation)?\n\nMETHOD (dps 50):\n * -zeta'/zeta(w) for sigma >= 5: TRUNCATED DIRICHLET SERIES\n sum_{k<=K} Lambda(k)/k^w with K chosen per sigma so the certified\n tail bound [log(K-1)/(sig-1) + 1/(sig-1)^2] . (K-1)^{1-sig} < 1e-11.\n Exact (this IS -zeta'/zeta for re > 1), no library zeta involved.\n * sigma in [3, 5): mpmath -zeta'/zeta (moderate args, trustworthy),\n VALIDATED against the Dirichlet series at sigma in {3, 3.5, 4, 4.5, 5}\n (tol 1e-8, K ~ 2.3e4 at sigma=3).\n * kernel k_n(w-1) = (1 + 1/(w-1))^n - 1, direct mpmath power.\n * T = c.sqrt(n) + eps, eps from a deterministic zero-avoiding scan\n (max-clearance grid point, zeros via mp.zetazero).\n * Composite trapezoid, two grids h = 0.05 / 0.02; the h-delta is the\n quadrature-error estimate. Integrand analytic, |phase'| <= ~2\n rad/unit, endpoints tiny vs peak => near-spectral convergence.\n\nCHECKS:\n V0: the refined envelope at c=1 reproduces E-031 (n=1e4: ~1.05e4;\n n=1e5: ~5.9e10).\n VA: mpmath-vs-Dirichlet validation max discrepancy <= 1e-6.\n VC: trapezoid h-delta <= 1e-4 . envelope.\n V3: the repair collapses the envelope: Env(c=2)/Env(c=1) < 1e-6 (n=1e5).\n\nVERDICT FIELDS (reported, not auto-decided): Re/Im/|I_right|, envelope,\n|I|/envelope, and for c=1 also the mid part [-5/2, 3] (mpmath zeta) and\nthe full I_II = I_mid + I_right.\n\"\"\"\nimport json\nimport math\nimport sys\nfrom pathlib import Path\n\nimport mpmath as mp\n\nmp.mp.dps = 50\n\nDATA = Path(__file__).resolve().parent.parent / \"data\"\nCSV = DATA / \"E-032-horizontal-true-integral.csv\"\nJSON = DATA / \"E-032-horizontal-true-integral.json\"\n\nSIEVE_MAX = 25000\nTOL_DIR = mp.mpf(\"1e-11\") # Dirichlet tail certificate target (sigma >= 5)\nTOL_SPOT = mp.mpf(\"1e-8\") # validation spot checks (sigma in {3..5})\nNS = [10 ** 3, 10 ** 4, 10 ** 5]\nCS = [1.0, 1.5, 2.0]\n\n# ---------------------------------------------------------------- von Mangoldt\n_spf = list(range(SIEVE_MAX + 1))\nfor _i in range(2, int(SIEVE_MAX ** 0.5) + 1):\n if _spf[_i] == _i:\n for _j in range(_i * _i, SIEVE_MAX + 1, _i):\n if _spf[_j] == _j:\n _spf[_j] = _i\nLAM = [mp.mpf(0)] * (SIEVE_MAX + 1) # Lambda(k) as mpf log p, else 0\nLOGK = [mp.mpf(0)] * (SIEVE_MAX + 1)\nfor _k in range(2, SIEVE_MAX + 1):\n LOGK[_k] = mp.log(_k)\n _p = _spf[_k]\n _m = _k\n while _m % _p == 0:\n _m //= _p\n if _m == 1:\n LAM[_k] = LOGK[_p]\n\n\ndef tail_cert(sig, K):\n \"\"\"Certified bound on sum_{k>K} Lambda(k)/k^sig (sig >= 3, K >= 3):\n <= integral_{K-1}^inf log x / x^sig dx (summand decreasing).\"\"\"\n if K <= 3:\n return mp.inf\n u = mp.mpf(K - 1)\n return (mp.log(u) / (sig - 1) + 1 / (sig - 1) ** 2) * u ** (1 - sig)\n\n\ndef choose_K(sig, tol):\n K = 4\n while tail_cert(sig, K) >= tol:\n K += 1\n if K > SIEVE_MAX:\n raise RuntimeError(f\"K exceeds sieve at sig={sig}\")\n return K\n\n\ndef dser(sig, T, tol=TOL_DIR):\n \"\"\"-zeta'/zeta(sig + iT) via the truncated Dirichlet series, re > 1.\"\"\"\n K = choose_K(sig, tol)\n w = mp.mpc(sig, T)\n s = mp.mpc(0)\n for k in range(2, K + 1):\n if LAM[k]:\n s += LAM[k] * mp.exp(-w * LOGK[k])\n return s, K\n\n\ndef mp_neg_ld(sig, T):\n w = mp.mpc(sig, T)\n return -mp.zeta(w, derivative=1) / mp.zeta(w)\n\n\ndef neg_ld(sig, T):\n \"\"\"Primary -zeta'/zeta: mpmath on [3,5), Dirichlet series on [5,inf).\"\"\"\n if sig < 5:\n return mp_neg_ld(sig, T), 0\n return dser(sig, T)\n\n\ndef kernel(n, sig, T):\n return (1 + 1 / mp.mpc(sig - 1, T)) ** n - 1\n\n\ndef integrand(n, sig, T):\n d, _ = neg_ld(sig, T)\n return kernel(n, sig, T) * d\n\n\ndef zeros_near(T0, half=3.0):\n \"\"\"Zero ordinates within [T0-half, T0+half] (deterministic).\"\"\"\n j_approx = int((T0 / (2 * math.pi)) * math.log(T0 / (2 * math.pi * math.e)))\n out = []\n for j in range(max(1, j_approx - 30), j_approx + 30):\n g = mp.zetazero(j).imag\n if abs(g - T0) <= half:\n out.append(g)\n return out\n\n\ndef choose_eps(T0):\n \"\"\"Deterministic zero-avoiding eps in {0, .05, ..., .95}.\"\"\"\n zs = zeros_near(T0)\n best = (-1, 0.0)\n for i in range(20):\n eps = i * 0.05\n cl = min((abs(T0 + eps - g) for g in zs), default=mp.mpf(half := 3.0))\n if cl > best[0]:\n best = (cl, eps)\n return best[1], best[0]\n\n\ndef integrate(n, T, a, b, h, use_mid=False):\n \"\"\"Trapezoid for I = int_a^b f d sigma and the envelope int |f|.\"\"\"\n N = int(math.ceil((b - a) / h))\n he = (b - a) / N\n tot = mp.mpc(0)\n env = mp.mpf(0)\n for i in range(N + 1):\n sig = a + i * he\n if use_mid or sig < 5:\n f = kernel(n, sig, T) * mp_neg_ld(sig, T)\n else:\n f = integrand(n, sig, T)\n wt = mp.mpf(\"0.5\") if i in (0, N) else mp.mpf(1)\n tot += wt * f\n env += wt * abs(f)\n return tot * he, env * he\n\n\ndef right_part(n, T, h):\n return integrate(n, T, 3.0, 2 * mp.sqrt(n), h)\n\n\ndef main() -> int:\n if JSON.exists() and \"--force\" not in sys.argv:\n print(f\"cached: {JSON} (use --force to recompute)\")\n return 0\n\n rows = []\n results = {}\n max_val_disc = mp.mpf(0)\n\n for n in NS:\n sq = mp.sqrt(n)\n for c in CS:\n T0 = c * sq\n eps, clearance = choose_eps(T0)\n T = T0 + eps\n # ---- validation (mpmath vs Dirichlet spot checks) ----\n val = {}\n for sig in [3.0, 3.5, 4.0, 4.5, 5.0]:\n dm, _k = dser(mp.mpf(sig), T, tol=TOL_SPOT)\n dd = mp_neg_ld(sig, T)\n disc = abs(dm - dd)\n val[sig] = disc\n if disc > max_val_disc:\n max_val_disc = disc\n # ---- right part, two grids ----\n I1, E1 = right_part(n, T, 0.05)\n I2, E2 = right_part(n, T, 0.02)\n rec = {\n \"n\": n, \"c\": c, \"T\": T, \"eps\": eps, \"clearance\": clearance,\n \"I_right\": I2, \"env\": E2, \"hdelta\": abs(I1 - I2),\n \"env_hdelta\": abs(E1 - E2),\n \"val_max\": max(val.values()),\n }\n # ---- mid part + full side (c = 1 only) ----\n if c == 1.0:\n M1, ME1 = integrate(n, T, -2.5, 3.0, 0.05, use_mid=True)\n M2, ME2 = integrate(n, T, -2.5, 3.0, 0.02, use_mid=True)\n rec.update({\"I_mid\": M2, \"env_mid\": ME2,\n \"mid_hdelta\": abs(M1 - M2),\n \"I_full\": M2 + I2})\n results[(n, c)] = rec\n print(f\"n={n} c={c}: T={mp.nstr(T,8)} eps={eps} \"\n f\"|I_right|={mp.nstr(abs(I2),4)} env={mp.nstr(E2,4)} \"\n f\"ratio={mp.nstr(abs(I2)/E2,3)} hdelta={mp.nstr(rec['hdelta'],2)}\",\n flush=True)\n rows.append({\n \"check\": \"right_part\", \"n\": str(n), \"c\": mp.nstr(c, 3),\n \"T\": mp.nstr(T, 10), \"eps\": str(eps),\n \"clearance\": mp.nstr(clearance, 4),\n \"Re_I_right\": mp.nstr(I2.real, 8),\n \"Im_I_right\": mp.nstr(I2.imag, 8),\n \"abs_I_right\": mp.nstr(abs(I2), 6),\n \"envelope\": mp.nstr(E2, 6),\n \"abs_over_env\": mp.nstr(abs(I2) / E2, 4),\n \"Re_over_env\": mp.nstr(abs(I2.real) / E2, 4),\n \"abs_over_sqrtn\": mp.nstr(abs(I2) / sq, 4),\n \"hdelta\": mp.nstr(rec[\"hdelta\"], 3),\n \"val_max_disc\": mp.nstr(rec[\"val_max\"], 3),\n \"abs_I_mid\": mp.nstr(abs(rec[\"I_mid\"]), 6) if c == 1.0 else \"\",\n \"envelope_mid\": mp.nstr(rec[\"env_mid\"], 6) if c == 1.0 else \"\",\n \"Re_I_full\": mp.nstr(rec[\"I_full\"].real, 8) if c == 1.0 else \"\",\n \"abs_I_full\": mp.nstr(abs(rec[\"I_full\"]), 6) if c == 1.0 else \"\",\n })\n\n # ---------------------------------------------------------------- checks\n r = results\n v0 = (mp.mpf(\"0.9e4\") < r[(10 ** 4, 1.0)][\"env\"] < mp.mpf(\"1.2e4\")) and \\\n (mp.mpf(\"4e10\") < r[(10 ** 5, 1.0)][\"env\"] < mp.mpf(\"8e10\"))\n va = max_val_disc <= mp.mpf(\"1e-6\")\n vc = all(rec[\"hdelta\"] <= mp.mpf(\"1e-4\") * rec[\"env\"] for rec in r.values())\n v3 = r[(10 ** 5, 2.0)][\"env\"] / r[(10 ** 5, 1.0)][\"env\"] < mp.mpf(\"1e-6\")\n\n DATA.mkdir(exist_ok=True)\n with CSV.open(\"w\") as f:\n f.write(\",\".join(rows[0].keys()) + \"\\n\")\n for row in rows:\n f.write(\",\".join(row.values()) + \"\\n\")\n\n verdict = {}\n for n in NS:\n rec = r[(n, 1.0)]\n verdict[str(n)] = {\n \"Re_I_right\": mp.nstr(rec[\"I_right\"].real, 8),\n \"Im_I_right\": mp.nstr(rec[\"I_right\"].imag, 8),\n \"abs_I_right\": mp.nstr(abs(rec[\"I_right\"]), 6),\n \"envelope_right\": mp.nstr(rec[\"env\"], 6),\n \"abs_over_env\": mp.nstr(abs(rec[\"I_right\"]) / rec[\"env\"], 4),\n \"abs_over_sqrtn\": mp.nstr(abs(rec[\"I_right\"]) / mp.sqrt(n), 4),\n \"abs_I_mid\": mp.nstr(abs(rec[\"I_mid\"]), 6),\n \"envelope_mid\": mp.nstr(rec[\"env_mid\"], 6),\n \"Re_I_full\": mp.nstr(rec[\"I_full\"].real, 8),\n \"abs_I_full\": mp.nstr(abs(rec[\"I_full\"]), 6),\n \"abs_I_right_c1.5\": mp.nstr(abs(r[(n, 1.5)][\"I_right\"]), 6),\n \"abs_I_right_c2\": mp.nstr(abs(r[(n, 2.0)][\"I_right\"]), 6),\n }\n sidecar = {\n \"experiment\": \"E-032\",\n \"title\": \"F-D side thread 6-R-0: the TRUE complex right-tail horizontal \"\n \"integral of Lagarias Thm 6.1 at T = sqrt(n) vs the E-031 envelope\",\n \"session\": \"039\",\n \"thread\": \"ii-prime-6-R\",\n \"design_doc\": \"D-073\",\n \"finding_doc\": \"D-072\",\n \"checks\": {\"V0_envelope_reproduces_E031\": bool(v0),\n \"VA_mpmath_vs_dirichlet_le_1e-6\": bool(va),\n \"VC_trapezoid_convergence\": bool(vc),\n \"V3_repair_envelope_collapses\": bool(v3)},\n \"all_checks_pass\": bool(v0 and va and vc and v3),\n \"dps\": 50,\n \"method\": \"-zeta'/zeta by truncated Dirichlet series (certified tail \"\n \"1e-11) for sigma >= 5; mpmath validated against it at \"\n \"{3,3.5,4,4.5,5} for sigma in [3,5); trapezoid h=0.05/0.02.\",\n \"verdict_inputs\": verdict,\n \"notes\": \"Verdict rule (D-073): |I_right| ~ envelope => per-side claim \"\n \"FALSE at T = sqrt(n); |I_right| << envelope => hidden \"\n \"cancellation (STOP and report).\",\n }\n with JSON.open(\"w\") as f:\n json.dump(sidecar, f, indent=2)\n print(f\"V0 envelope~E031: {bool(v0)} | VA validation: {bool(va)} \"\n f\"(max {mp.nstr(max_val_disc,2)}) | VC convergence: {bool(vc)} | \"\n f\"V3 repair: {bool(v3)}\")\n print(f\"wrote {CSV} + {JSON}\")\n return 0 if sidecar[\"all_checks_pass\"] else 1\n\n\nif __name__ == \"__main__\":\n sys.exit(main())\n"},"extra_content":null}]