#!/usr/bin/env python3 """ rrc_anti_connections.py — Map Anti-Diophantine connections between manifold routes. Finds structural, algebraic, and paper-level connections between Anti-Diophantine equations across different manifold routes. Output: shared-data/data/anti_connections_v1.json """ from __future__ import annotations import json import re import subprocess import sys from collections import defaultdict from pathlib import Path RECEIPT_PATH = Path("archive/experimental-shim-probes/rrc_equation_classifier_receipt.json") OUT_PATH = Path("shared-data/data/anti_connections_v1.json") NEON_HOST = "neon-64gb" CONTAINER = "arxiv-pg" DB = "arxiv" def ssh_query(sql: str, timeout: int = 30) -> list[list[str]]: result = subprocess.run([ "ssh", NEON_HOST, f"podman exec {CONTAINER} psql -U postgres -d {DB} -t -A -F '|' -c \"{sql}\"" ], capture_output=True, text=True, timeout=timeout) return [line.split("|") for line in result.stdout.strip().split("\n") if line] def extract_features(text: str) -> dict: features = {} if not text: return features t = str(text) features.update({ "has_sum": "\\sum" in t or "\\Sigma" in t, "has_int": "\\int" in t, "has_partial": "\\partial" in t, "has_log": "\\log" in t or "\\ln" in t, "has_sqrt": "\\sqrt" in t, "has_exp": "^" in t or "\\exp" in t, "has_frac": "\\frac" in t, "has_theta": "\\theta" in t, "has_phi": "\\phi" in t, "has_sigma": "\\sigma" in t, "has_delta": "\\delta" in t, "has_max": "max(" in t, "has_min": "min(" in t, "has_clip": "clip" in t.lower(), "has_norm": "\\|" in t or "norm" in t.lower(), "has_sum_over": "\\sum_" in t, "has_prod": "\\prod" in t, "has_arrow_to": "\\rightarrow" in t or "→" in t, "has_subscript": "_" in t and "_" not in t[:t.find("_")+2], "eq_len": len(t), }) return features def main(): d = json.loads(RECEIPT_PATH.read_text()) all_eqs = d["compiled_equations"] # Separate by regime anti_eqs = [e for e in all_eqs if e["equation_record"].get("manifold_regime") == "anti_diophantine"] diop_eqs = [e for e in all_eqs if e["equation_record"].get("manifold_regime") == "diophantine"] connections = { "schema": "anti_connections_v1", "description": "Cross-route connections between Anti-Diophantine equations", "anti_total": len(anti_eqs), "diophantine_total": len(diop_eqs), "route_bridges": [], "structural_clusters": [], "shared_paper_graph": [], } # ── 1. Structural clusters: equations sharing similar features across routes ── feature_sigs = defaultdict(list) for e in anti_eqs: rec = e["equation_record"] feats = extract_features(str(rec.get("equation", ""))) sig = tuple(sorted((k, v) for k, v in feats.items() if v and k != "eq_len")) feature_sigs[sig].append({ "name": rec.get("name", "?"), "route": rec.get("manifold_route", "?"), }) for sig, eqs in sorted(feature_sigs.items(), key=lambda x: -len(x[1])): if len(eqs) >= 2: routes_in_cluster = set(e["route"] for e in eqs) if len(routes_in_cluster) >= 2: connections["structural_clusters"].append({ "shared_features": [k for k, v in sig if v], "equation_count": len(eqs), "routes": sorted(routes_in_cluster), "equations": [e["name"] for e in eqs], }) # ── 2. Route bridges: shared LaTeX constructs between pairs of routes ── anti_by_route = defaultdict(list) for e in anti_eqs: rec = e["equation_record"] anti_by_route[rec.get("manifold_route", "?")].append(e) routes = sorted(anti_by_route.keys()) for i in range(len(routes)): for j in range(i + 1, len(routes)): r1, r2 = routes[i], routes[j] # Extract shared features syms1 = set() syms2 = set() for e in anti_by_route[r1]: feats = extract_features(str(e["equation_record"].get("equation", ""))) syms1 |= {k for k, v in feats.items() if v and k != "eq_len"} for e in anti_by_route[r2]: feats = extract_features(str(e["equation_record"].get("equation", ""))) syms2 |= {k for k, v in feats.items() if v and k != "eq_len"} shared = syms1 & syms2 if shared: connections["route_bridges"].append({ "route_a": r1, "route_b": r2, "shared_features": sorted(shared), "a_count": len(anti_by_route[r1]), "b_count": len(anti_by_route[r2]), }) # ── 3. Shared paper graph: same arxiv paper matched to different routes ── paper_route_map = defaultdict(set) for e in d["compiled_equations"]: rec = e["equation_record"] pid = rec.get("arxiv_paper_id", "") route = rec.get("manifold_route", "unclassified") if pid and route != "unclassified": paper_route_map[pid].add(route) for pid, rts in sorted(paper_route_map.items(), key=lambda x: -len(x[1])): if len(rts) >= 2: # Get paper title from DB rows = ssh_query(f"SELECT title FROM arxiv_papers WHERE paper_id = '{pid}'") title = rows[0][0] if rows and len(rows[0]) >= 1 else "" connections["shared_paper_graph"].append({ "paper_id": pid, "title": title[:100], "routes": sorted(rts), }) # ── 4. Algebraic signature: Anti-Diophantine equations share specific patterns ── # Compute the feature signature unique to Anti-Diophantine equations anti_features = defaultdict(int) diop_features = defaultdict(int) total_anti = len(anti_eqs) total_diop = len(diop_eqs) for e in anti_eqs: feats = extract_features(str(e["equation_record"].get("equation", ""))) for k, v in feats.items(): if v and k != "eq_len": anti_features[k] += 1 for e in diop_eqs: feats = extract_features(str(e["equation_record"].get("equation", ""))) for k, v in feats.items(): if v and k != "eq_len": diop_features[k] += 1 connections["anti_signature"] = { "features_enriched_in_anti": sorted( [k for k in anti_features if anti_features[k] / max(total_anti, 1) > diop_features.get(k, 0) / max(total_diop, 1) * 2], ), "anti_feature_frequencies": { k: f"{anti_features[k]}/{total_anti}" for k, v in sorted(anti_features.items(), key=lambda x: -x[1])[:10] }, "diop_feature_frequencies": { k: f"{diop_features[k]}/{total_diop}" for k, v in sorted(diop_features.items(), key=lambda x: -x[1])[:10] }, } OUT_PATH.parent.mkdir(parents=True, exist_ok=True) OUT_PATH.write_text(json.dumps(connections, indent=2, ensure_ascii=False)) print(f"=== Anti-Diophantine Connections ===") print(f" Anti equations: {len(anti_eqs)}") print(f" Diophantine equations: {len(diop_eqs)}") print(f" Route bridges: {len(connections['route_bridges'])}") print(f" Structural clusters: {len(connections['structural_clusters'])}") print(f" Shared paper links: {len(connections['shared_paper_graph'])}") print() print("Route bridges (shared features between Anti-Diophantine routes):") for rb in connections["route_bridges"]: print(f" {rb['route_a']:25s} ↔ {rb['route_b']:25s} shared={rb['shared_features']}") print() print("Structural clusters (shared feature signatures across routes):") for sc in connections["structural_clusters"][:5]: print(f" {sc['equation_count']} eqs across {sc['routes']}") print(f" features: {sc['shared_features']}") print() print("Enriched in Anti-Diophantine:", connections["anti_signature"]["features_enriched_in_anti"]) print(f"\nSaved to {OUT_PATH}") if __name__ == "__main__": main()