#!/usr/bin/env python3 """ Raven's Matrix → RRC shape query for the Burgers-Hilbert η_c threshold. Constructs a 3×3 Raven matrix where: Rows = viscosity ν ∈ {0.05, 0.10, 0.20} Columns = Hilbert coupling η ∈ {0.025, 0.050, 0.100} Cells = energy ratio E1/E0 after 500 steps The missing cell is the η_c = ν/2 threshold crossing. The shape index finds the closest solved theorem with the same Sidon sumset structure. """ import json import math import sys from pathlib import Path SHAPE_INDEX = Path("/home/allaun/lean_corpus/shape_index.json") # ─── Raven matrix for η_c threshold ───────────────────────────────────── RAVEN_MATRIX = { "name": "Burgers-Hilbert η_c threshold", "description": ( "3×3 Raven matrix: rows = ν (0.05,0.10,0.20), " "cols = η (0.025,0.050,0.100). " "Each cell = energy ratio E1/E0 after 500 steps. " "Missing cell = η_c = ν/2 threshold crossing." ), "row_labels": ["ν=0.05", "ν=0.10", "ν=0.20"], "col_labels": ["η=0.025", "η=0.050", "η=0.100"], # Simulated energy ratios based on the sweep results "cells": [ [0.85, 0.55, 0.30], # ν=0.05 row [0.78, 0.50, 0.27], # ν=0.10 row (sweep data) [0.65, 0.42, 0.22], # ν=0.20 row ], "missing_cell_index": (1, 1), # center cell = threshold crossing "missing_cell_value": 0.50, # predicted η_c = ν/2 } # ─── Sidon signature from Raven matrix ────────────────────────────────── SIDON_ADDRESSES = [1, 2, 4, 8, 16, 32, 64, 128, 256] def raven_to_sidon_signature(matrix: dict) -> dict: """Compute the Sidon sumset signature of a Raven matrix.""" cells = matrix["cells"] n_rows = len(cells) n_cols = len(cells[0]) # Flatten the 3×3 grid to 9 values, each with a Sidon address flat = [] for i in range(n_rows): for j in range(n_cols): idx = i * n_cols + j flat.append({ "row": i, "col": j, "sidon_address": SIDON_ADDRESSES[idx], "value": cells[i][j], }) # Compute pairwise sums for adjacent cells (row/column neighbors) pairs = [] sums = set() for i in range(n_rows): for j in range(n_cols): idx = i * n_cols + j a = SIDON_ADDRESSES[idx] # Right neighbor if j + 1 < n_cols: idx_r = i * n_cols + (j + 1) b = SIDON_ADDRESSES[idx_r] pairs.append((idx, idx_r, a + b)) sums.add(a + b) # Bottom neighbor if i + 1 < n_rows: idx_d = (i + 1) * n_cols + j b = SIDON_ADDRESSES[idx_d] pairs.append((idx, idx_d, a + b)) sums.add(a + b) # Row rules: each row has a transformation pattern row_rules = [] for i in range(n_rows): v0 = cells[i][0] v1 = cells[i][1] v2 = cells[i][2] ratio1 = v1 / v0 if v0 > 0 else 0 ratio2 = v2 / v1 if v1 > 0 else 0 row_rules.append({ "row": i, "pattern": [round(v, 4) for v in [v0, v1, v2]], "ratio_01": round(ratio1, 4), "ratio_12": round(ratio2, 4), }) # Column rules col_rules = [] for j in range(n_cols): v0 = cells[0][j] v1 = cells[1][j] v2 = cells[2][j] ratio1 = v1 / v0 if v0 > 0 else 0 ratio2 = v2 / v1 if v1 > 0 else 0 col_rules.append({ "col": j, "pattern": [round(v, 4) for v in [v0, v1, v2]], "ratio_01": round(ratio1, 4), "ratio_12": round(ratio2, 4), }) # Sidon sumset density n_pairs = len(pairs) unique_sums = len(sums) sumset_density = unique_sums / n_pairs if n_pairs > 0 else 1.0 return { "n_cells": len(flat), "n_pairs": n_pairs, "unique_sums": unique_sums, "sumset_density": round(sumset_density, 4), "sidon_addresses": SIDON_ADDRESSES[:len(flat)], "row_rules": row_rules, "col_rules": col_rules, "row_rule_consistency": all( abs(r["ratio_01"] - col_rules[0]["ratio_01"]) < 0.2 for r in row_rules ), "col_rule_consistency": all( abs(c["ratio_01"] - row_rules[0]["ratio_01"]) < 0.2 for c in col_rules ), } # ─── Query shape index ────────────────────────────────────────────────── def query_shape_index(sig: dict, top_k: int = 5) -> list[dict]: """Find nearest-neighbor theorems by Sidon sumset distance.""" with open(SHAPE_INDEX) as f: idx = json.load(f) target_density = sig["sumset_density"] target_n_pairs = sig["n_pairs"] scored = [] for t in idx["all_theorems"]: density = t["sidon_signature"]["sumset_density"] n_vars = t["constraint_graph"]["n_vars"] shape = t["famm"]["rrc_shape"] state = t["famm"]["famm_state"] d_density = abs(density - target_density) d_vars = abs(n_vars - target_n_pairs) * 0.05 d_state = 0 if state == "ACCEPT" else 0.5 distance = d_density + d_vars + d_state scored.append({ "distance": round(distance, 4), "name": t["name"], "shape": shape, "state": state, "sidon_density": density, "n_vars": n_vars, "path": t["path"], "line": t["line"], "signature": t.get("signature", ""), }) scored.sort(key=lambda x: x["distance"]) return scored[:top_k] # ─── Main ──────────────────────────────────────────────────────────────── def main(): print("Raven's Matrix → RRC Shape Query") print("=" * 60) print(f"Matrix: {RAVEN_MATRIX['name']}") print(f" {RAVEN_MATRIX['description']}") print() # Print the 3×3 grid cells = RAVEN_MATRIX["cells"] print(f" {'':>10} {RAVEN_MATRIX['col_labels'][0]:>10} " f"{RAVEN_MATRIX['col_labels'][1]:>10} {RAVEN_MATRIX['col_labels'][2]:>10}") for i in range(3): print(f" {RAVEN_MATRIX['row_labels'][i]:>10} {cells[i][0]:>10.3f} " f"{cells[i][1]:>10.3f} {cells[i][2]:>10.3f}") print(f" {'missing':>10} {'':>10} {'[???]':>10} {'':>10}") print(f" Missing cell = η_c / ν threshold crossing") print(f" Predicted value ≈ 0.50 (η_c = ν/2)") print() # Compute Sidon signature sig = raven_to_sidon_signature(RAVEN_MATRIX) print(f"Sidon sumset signature:") print(f" Cells: {sig['n_cells']} (3×3)") print(f" Pairs: {sig['n_pairs']}") print(f" Unique sums: {sig['unique_sums']}") print(f" Density: {sig['sumset_density']}") print(f" Row rules: {[r['ratio_01'] for r in sig['row_rules']]}") print(f" Col rules: {[c['ratio_01'] for c in sig['col_rules']]}") print(f" Rule consistency: {sig['row_rule_consistency']}") print() # Query the shape index print(f"Querying shape index ({SHAPE_INDEX})...") neighbors = query_shape_index(sig) print(f" Nearest neighbors:") for n in neighbors[:5]: print(f" [{n['shape']:30}] {n['state']:6} d={n['distance']:.4f} {n['name']}") print(f" {n['path']}:{n['line']}") print(f" density={n['sidon_density']:.3f} vars={n['n_vars']}") print() # RRC classification if sig["row_rule_consistency"] and sig["col_rule_consistency"]: rrc = "ACCEPT — single rule governs both rows and columns" elif sig["row_rule_consistency"] or sig["col_rule_consistency"]: rrc = "SignalShapedRouteCompiler — one axis has consistent rules, other has ambiguity" else: rrc = "HOLD — no consistent rule across either axis" print(f"RRC classification: {rrc}") print() # Emit receipt import hashlib from datetime import datetime, timezone receipt = { "schema": "rrc_raven_query_v1", "claim_boundary": "raven_matrix_3x3;sidon_sumset_query", "raven_matrix": RAVEN_MATRIX, "sidon_signature": sig, "nearest_neighbors": neighbors[:5], "rrc_classification": rrc, "computed_at": datetime.now(timezone.utc).isoformat(), } canonical = json.dumps(receipt, sort_keys=True, separators=(",", ":")) receipt["receipt_sha256"] = hashlib.sha256(canonical.encode()).hexdigest() with open("raven_threshold_query_receipt.json", "w") as f: json.dump(receipt, f, indent=2) print(f"Receipt: raven_threshold_query_receipt.json") print(f"SHA256: {receipt['receipt_sha256']}") if __name__ == "__main__": main()