#!/usr/bin/env python3 """0002 -- EXP-1: the ring-conditioned co-rotation test (fully vectorised). See corotation_experiment.json for the output. Headline: (a) Psi(n)=theta(n+2)-theta(n) is NOT a function of n mod P -- inside one residue class mod 15015 it ranges from pi/4 (k=2) to 0.01 (k=123), with corr(k, Psi) strongly negative. So route 61's finite-modulus obstruction does not apply to it. (b) The naive matched-residue twin/composite contrast is therefore SPURIOUS: it only measures the twin/composite ring-index difference. (c) Conditioned inside each ring k, the contrast is the honest test. (d) The pair-level ring statistic (does n+2 leave the ring of n?) is not ring-dominated and is reported separately. """ import math, json from collections import defaultdict import numpy as np def isqrt_arr(m): r = np.sqrt(m.astype(np.float64)).astype(np.int64) r = np.maximum(r - 1, 0) while True: rr = r + 1 bad = rr * rr <= m if not bad.any(): break r[bad] = rr[bad] return r def k_of(m): return (isqrt_arr(m - 1) + 1) // 2 def ulam_xy(m): k = k_of(m); mm = m - (2 * k - 1) ** 2 x = np.where(mm <= 2*k, k, np.where(mm <= 4*k, 3*k - mm, np.where(mm <= 6*k, -k, mm - 7*k))) y = np.where(mm <= 2*k, mm - k, np.where(mm <= 4*k, k, np.where(mm <= 6*k, 5*k - mm, -k))) x = np.where(m == 1, 0, x); y = np.where(m == 1, 0, y) return x.astype(np.float64), y.astype(np.float64) def Psi_of(m): x1, y1 = ulam_xy(m); x2, y2 = ulam_xy(m + 2) d = np.arctan2(y2, x2) - np.arctan2(y1, x1) return (d + np.pi) % (2 * np.pi) - np.pi def main(X=4_000_000): Q = [3, 5, 7, 11, 13] P = 1 for p in Q: P *= p is_p = np.ones(X + 3, dtype=bool); is_p[:2] = False for i in range(2, math.isqrt(X + 2) + 1): if is_p[i]: is_p[i*i::i] = False n = np.arange(X - 1, dtype=np.int64) ok = np.ones(X - 1, dtype=bool) for p in Q: ok &= (n % p != 0) & ((n + 2) % p != 0) adm = n[ok] psi = Psi_of(adm); k = k_of(adm) tw_mask = is_p[adm] & is_p[adm + 2] twins = adm[tw_mask]; comp = adm[~tw_mask] psi_t, psi_c = psi[tw_mask], psi[~tw_mask] k_t, k_c = k[tw_mask], k[~tw_mask] # (a) corr = float(np.corrcoef(k, psi)[0, 1]) res_pairs = np.unique(np.stack([adm % P, np.round(psi, 6)], axis=1), axis=0) counts = defaultdict(set) for r, v in res_pairs.tolist(): counts[r].add(v) spreading = sum(1 for v in counts.values() if len(v) > 1) # (b) naive matched-residue comp_by_res = defaultdict(list) for m in comp.tolist(): comp_by_res[m % P].append(m) nt, nc = [], [] for t in twins.tolist(): if t % P in comp_by_res: nt.append(t); nc.append(comp_by_res[t % P][0]) def z_of(a, b): if len(a) < 2 or len(b) < 2: return None se = math.sqrt(np.var(a)/len(a) + np.var(b)/len(b)) return None if se == 0 else round(float((np.mean(a)-np.mean(b))/se), 3) # (c) ring-conditioned def psi_of_scalar(m): return float(Psi_of(np.array([m]))[0]) byk_t, byk_c = defaultdict(list), defaultdict(list) for v, kk, tag in zip(adm.tolist(), k.tolist(), tw_mask.tolist()): (byk_t if tag else byk_c)[kk].append(v) per_ring = [] for kk in sorted(byk_t): if len(byk_t[kk]) >= 25 and len(byk_c[kk]) >= 25: at = np.array([psi_of_scalar(v) for v in byk_t[kk]]) ac = np.array([psi_of_scalar(v) for v in byk_c[kk]]) per_ring.append({"k": int(kk), "n_twin": len(at), "n_comp": len(ac), "z_cos_Psi": z_of(np.cos(at), np.cos(ac)), "z_sin_Psi": z_of(np.sin(at), np.sin(ac))}) zs = [r["z_sin_Psi"] for r in per_ring if r["z_sin_Psi"] is not None] pooled = z_of(np.cos(psi_t), np.cos(psi_c)) pooled_sin = z_of(np.sin(psi_t), np.sin(psi_c)) # (d) pair-level ring statistic tw_leave = int((k_of(twins + 2) != k_t).sum()) cp_leave = int((k_of(comp + 2) != k_c).sum()) out = { "X": X, "P": P, "n_admissible": int(len(adm)), "n_twins": int(len(twins)), "n_comp": int(len(comp)), "a_Psi_not_function_of_n_mod_P": spreading > 0, "a_residue_classes_with_multiple_Psi": spreading, "a_total_residue_classes": len(counts), "a_corr_k_Psi": round(corr, 6), "b_naive_matched_pairs": len(nt), "b_naive_z_cos": z_of(np.cos(psi_of_scalar_arr := np.array([psi_of_scalar(v) for v in nt])), np.cos(np.array([psi_of_scalar(v) for v in nc]))), "b_naive_mean_k_twin": round(float(k_of(np.array(nt)).mean()), 2), "b_naive_mean_k_comp": round(float(k_of(np.array(nc)).mean()), 2), "c_rings_with_25_each": len(per_ring), "c_pooled_z_cos_Psi": pooled, "c_pooled_z_sin_Psi": pooled_sin, "c_sin_z_min": min(zs) if zs else None, "c_sin_z_max": max(zs) if zs else None, "c_frac_rings_abs_z_gt_2": (sum(1 for v in zs if abs(v) > 2)/len(zs)) if zs else None, "c_per_ring": per_ring, "d_twins_leaving_ring_fraction": tw_leave/len(twins), "d_comp_leaving_ring_fraction": cp_leave/len(comp), "d_twins_leaving": tw_leave, "d_comp_leaving": cp_leave, } print(json.dumps({k2: v for k2, v in out.items() if k2 != "c_per_ring"}, indent=2)) json.dump(out, open("corotation_experiment.json", "w"), indent=2) if __name__ == "__main__": main()