#!/usr/bin/env python3 """Two counter-rotating conical helices for the two prime series, plus polygon-shaped gauges and Davenport-Heilbronn (quartic character mod 5) rotation. Construction (complex plane + t axis): helix A (anti-clockwise): z_A(n) = r(n) * exp(+ i theta(n)) at height t = n helix B (clockwise): z_B(n) = r(n) * exp(- i theta(n)) at height t = n radial decay: r(n) = r0 / (1 + lambda n) rotation: theta(n) = 2*pi*n/g (default; one rotation per gauge g) torsion: theta(n) = 2*pi*n/g + eps*(n/g)^2 (variable-rate option) polygon gauge: theta quantised to m directions (regular m-gon cross-section) DH rotation: theta(n) += arg(chi_5(n)) with chi_5 the quartic character mod 5 Key measured facts (honest, heuristic/measured rung): 1. A single fixed gauge gives the pair phase difference theta(n)-theta(n+2) = -4*pi/g, a constant independent of n, so ONE helix cannot separate twins. 2. Twin detection needs the multi-gauge cosine (Ramanujan) kernel, i.e. the sieve product over all coprime gauges q <= z (see kernel_study.py). 3. This script measures how polygon (m-gon) quantisation and the DH twist degrade or preserve that sieve's twin separation. """ import json, math import numpy as np import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # noqa: F401 def sieve_primes(N): is_p = np.ones(N + 1, dtype=bool); is_p[:2] = False for i in range(2, int(N**0.5) + 1): if is_p[i]: is_p[i*i::i] = False return is_p def chi5(n): """Quartic character mod 5: chi(1)=1, chi(2)=i, chi(3)=-i, chi(4)=-1, chi(0)=0. This is the twist underlying the Davenport-Heilbronn Dirichlet series.""" r = n % 5 return {0: 0j, 1: 1+0j, 2: 0+1j, 3: 0-1j, 4: -1+0j}[r] def main(): N = 3000 z = 200 g = 11 # single gauge for the visualisation lam = 0.0005 # radial decay rate r0 = 1.0 is_p = sieve_primes(N) primes = np.flatnonzero(is_p)[1:] # odd primes only for the helix twin = np.flatnonzero(is_p[3:N-1] & is_p[5:N+1]) + 3 small = [p for p in np.flatnonzero(is_p) if p <= z] # ---- visualisation: two conical helices with prime points ---- fig = plt.figure(figsize=(12, 6)) ax = fig.add_subplot(111, projection="3d") t = np.arange(3, N + 1) r = r0 / (1 + lam * t) thA = 2*math.pi*t/g thB = -2*math.pi*t/g # helix curves (all integers as a fine guide) for th, col in [(thA, "tab:blue"), (thB, "tab:red")]: ax.plot(r*np.cos(th), r*np.sin(th), t, color=col, lw=0.5, alpha=0.5) # prime points rp = r0 / (1 + lam*primes) ax.scatter(rp*np.cos(2*math.pi*primes/g), rp*np.sin(2*math.pi*primes/g), primes, c="tab:blue", s=6, label=f"prime series A (CW-negative/ACW), gauge {g}") ax.scatter(rp*np.cos(-2*math.pi*primes/g), rp*np.sin(-2*math.pi*primes/g), primes, c="tab:red", s=6, label="prime series B (opposite rotation)") # twin primes highlighted rt = r0 / (1 + lam*twin) ax.scatter(rt*np.cos(2*math.pi*twin/g), rt*np.sin(2*math.pi*twin/g), twin, c="gold", s=22, marker="o", edgecolors="black", linewidths=0.4, label=f"twin primes (lower member), N={len(twin)}") ax.set_xlabel("Re z"); ax.set_ylabel("Im z"); ax.set_zlabel("t (helix length)") ax.set_title("Two conical helices (counter-rotating) carrying the prime series; twin primes highlighted") ax.legend(loc="upper left", fontsize=8) plt.tight_layout() plt.savefig("outputs/helix_twins.png", dpi=110) plt.close() # ---- fact 1: single-gauge phase difference is constant ---- dph = (2*math.pi*t/g - 2*math.pi*(t+2)/g) # = -4*pi/g print(f"single gauge g={g}: pair phase diff theta(n)-theta(n+2) = {dph[0]/math.pi:.4f}*pi (constant)") # ---- polygon gauge + DH rotation: twin-sieve reconstruction fidelity ---- # exact twin sieve I_z (Ramanujan cosine) over [3, N] nn = np.arange(3, N + 1, dtype=np.int64) def exact_sieve(): Iz = np.ones(N + 1); Iz[0:3] = 0.0 for p in small: c1 = np.where(nn % p == 0, p - 1, -1) c2 = np.where((nn + 2) % p == 0, p - 1, -1) Iz[3:] *= ((p - 1 - c1)/p) * ((p - 1 - c2)/p) return (Iz[3:] >= 1.0 - 1e-12).astype(int) Iexact = exact_sieve() def polygon_sieve(m): """Quantise the phase to m directions (m-gon) and rebuild the sieve.""" Iz = np.ones(N + 1); Iz[0:3] = 0.0 for p in small: c1 = np.zeros(len(nn)); c2 = np.zeros(len(nn)) for a in range(1, p): k = np.round((a * nn / p % 1.0) * m).astype(int) % m k2 = np.round((a * (nn + 2) / p % 1.0) * m).astype(int) % m c1 += np.cos(2*math.pi*k/m) c2 += np.cos(2*math.pi*k2/m) Iz[3:] *= ((p - 1 - c1)/p) * ((p - 1 - c2)/p) return Iz[3:] def dh_sieve(): """Davenport-Heilbronn twist: multiply each phase by chi_5(a) (quartic character mod 5) before summing, i.e. Re sum_a chi_5(a) e^{2 pi i a n/p}.""" Iz = np.ones(N + 1); Iz[0:3] = 0.0 for p in small: c1 = np.zeros(len(nn)); c2 = np.zeros(len(nn)) for a in range(1, p): ch = chi5(a) # complex unit c1 += (ch.real * np.cos(2*math.pi*a*nn/p) - ch.imag * np.sin(2*math.pi*a*nn/p)) c2 += (ch.real * np.cos(2*math.pi*a*(nn+2)/p) - ch.imag * np.sin(2*math.pi*a*(nn+2)/p)) Iz[3:] *= ((p - 1 - c1)/p) * ((p - 1 - c2)/p) return Iz[3:] rng = np.random.default_rng(0) composite = np.flatnonzero(~is_p & (np.arange(N + 1) >= 6)) sample = rng.choice(composite, size=len(twin), replace=False) def localization(w): w = np.asarray(w) tw = w[np.array(twin) - 3].astype(float) ntw = w[sample - 3].astype(float) return float((tw.mean() - ntw.mean()) / (tw.std() + ntw.std() + 1e-12)) def precision_recall(I): I = np.asarray(I) n_idx = np.arange(3, N + 1) tp = n_idx[(n_idx >= z + 2) & (I >= 0.5)] tp_twin = is_p[tp] & is_p[np.minimum(tp + 2, N)] prec = float(tp_twin.mean()) if len(tp) else 0.0 recall = float((I[np.array(twin) - 3] >= 0.5).mean()) return prec, recall, int(len(tp)) print("twin-sieve reconstruction (localization = twin vs non-twin separation):") rows = {} prec, recall, sz = precision_recall(Iexact) rows["exact_ramanujan"] = {"localization": localization(Iexact), "precision": prec, "recall": recall} print(f" {'exact Ramanujan cosine':26s} localization={localization(Iexact):+.3f} precision={prec:7.2%} recall={recall:6.2%} max-set={sz}") for m in [4, 5, 6, 8, 12, 24]: w = polygon_sieve(m) mae = float(np.abs(w - Iexact).mean()) rows[f"polygon_m{m}"] = {"localization": localization(w), "mae_vs_exact": mae} print(f" {f'polygon m={m}-gon':26s} localization={localization(w):+.3f} MAE-vs-exact={mae:.5f}") w = dh_sieve() rows["dh_quartic_twist"] = {"localization": localization(w)} print(f" {'DH quartic twist':26s} localization={localization(w):+.3f}") out = { "N": N, "z": z, "gauge_g": g, "num_twins": int(len(twin)), "single_gauge_phase_diff": float(dph[0]), "single_gauge_note": "theta(n)-theta(n+2) = -4*pi/g is constant in n: one helix cannot separate twins", "fidelity": rows, "note": ("Measured rung. The multi-gauge cosine kernel is the twin detector; " "polygon quantisation and the DH twist are alternate discretisations " "whose fidelity is reported above."), } with open("outputs/helix_results.json", "w") as f: json.dump(out, f, indent=2) print("wrote outputs/helix_results.json and outputs/helix_twins.png") if __name__ == "__main__": main()