"""
verify_P156.py — Addendum P156: G1 as a Known Unknown

Computes G1_abs and G1_rel, verifies G1 > 0, prints the Schur-thread
summary table (P151–P155), and checks the 1/17² proximity.

Copyright: Léon Fernando Vlegels. MIT License.
"""

import math
import sys

PASS = FAIL = 0

def check(n, desc, cond):
    global PASS, FAIL
    ok = bool(cond); PASS += ok; FAIL += (not ok)
    print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {desc}")

# ── Constants ─────────────────────────────────────────────────────────────────
ALPHA_INV     = 4*math.pi**3 + math.pi**2 + math.pi   # ≈ 137.036
BREATH_PERIOD = math.pi * ALPHA_INV                    # Ω = π·α⁻¹ ≈ 430.512
G1_abs        = 432 - BREATH_PERIOD                    # ≈ 1.4878
G1_rel        = G1_abs / BREATH_PERIOD                 # ≈ 3.4558e-3
TARGET        = BREATH_PERIOD / 9                      # Ω/9 ≈ 47.835

print("P156 VERIFICATION: G1 AS A KNOWN UNKNOWN")
print(f"ALPHA_INV      = {ALPHA_INV:.10f}")
print(f"BREATH_PERIOD  = {BREATH_PERIOD:.10f}")
print(f"432            = 432 (exact integer)")
print(f"G1_abs         = 432 − Ω = {G1_abs:.10f}")
print(f"G1_rel         = G1_abs/Ω = {G1_rel:.10e}  ({G1_rel*100:.6f}%)")
print(f"432/Ω          = {432/BREATH_PERIOD:.10f}")
print()

# ── Test 1: G1 > 0 ────────────────────────────────────────────────────────────
print("S1  G1 > 0")
check(1, f"G1_abs = {G1_abs:.10f} > 0", G1_abs > 0)
check(2, f"G1_rel = {G1_rel:.10e} > 0", G1_rel > 0)
print()

# ── Test 2: Schur thread summary table ────────────────────────────────────────
print("S2  Schur thread summary table (P151–P155)")

# All closed-form Schur results are rational.
DISCRETE      = 48            # B_{G2}(H_s,H_s) [P151]
F4_killing    = 36            # B_{F4}(H_s,H_s) [P153]
F4_product    = F4_killing * 12  # = 432 [P153]
G2_haar       = 48**2 / 14   # 48²/14 ≈ 164.571 [P154-P155]
SU3_haar      = 12**2 / 8    # 144/8 = 18 [P155]

rows = [
    ("Discrete B_{G2}(H_s,H_s)   [P151]",   DISCRETE,   DISCRETE / TARGET),
    ("F4 killing B_{F4}(H_s,H_s) [P153]",   F4_killing, None),
    ("  F4 product 36×12          [P153]",   F4_product, F4_product / TARGET),
    ("G2 Haar avg  48²/14         [P154-55]", G2_haar,   G2_haar / TARGET),
    ("SU(3) Haar avg  12²/8       [P155]",   SU3_haar,   SU3_haar / TARGET),
    ("Target Ω/9  (transcendental)",         TARGET,     1.0),
]

header = f"  {'Quantity':<40} {'Value':>10}  {'Ratio/TARGET':>13}"
print(header)
print("  " + "-" * 67)
for label, val, ratio in rows:
    if ratio is None:
        print(f"  {label:<40} {val:>10.4f}  {'—':>13}")
    else:
        print(f"  {label:<40} {val:>10.4f}  {ratio:>13.6f}")
print()

# Verify all Schur results are rational (i.e. finite-precision exact fractions)
check(3, "DISCRETE = 48 (B_{G2}(H_s,H_s), P151)", DISCRETE == 48)
check(4, "F4_killing = 36 (B_{F4}(H_s,H_s), P153)", F4_killing == 36)
check(5, "F4_product = 36×12 = 432 (P153)", F4_product == 432)
check(6, "G2_haar = 48²/14 = 1152/7 (P154-55)", abs(G2_haar - 1152/7) < 1e-12)
check(7, "SU3_haar = 12²/8 = 18 (P155)", SU3_haar == 18.0)
print("  All Schur values are rational (integer/integer).")
print()

# ── Test 3: Rational ≠ transcendental (numerical check) ──────────────────────
print("S3  No rational Schur value equals target Ω/9")
schur_vals = [DISCRETE, G2_haar, SU3_haar]
labels_s   = ["DISCRETE=48", "G2_haar≈164.6", "SU3_haar=18"]
all_clear  = True
for v, lbl in zip(schur_vals, labels_s):
    delta = abs(v - TARGET)
    rel   = delta / TARGET
    within_G1 = rel < G1_rel
    flag = "WITHIN G1" if within_G1 else "outside G1"
    mark = "PASS" if not within_G1 or v == DISCRETE else "NOTE"
    print(f"  {lbl:<18} |v − Ω/9| = {delta:8.4f}  ({rel*100:.4f}%)  [{flag}]")
    if within_G1 and v != DISCRETE:
        all_clear = False

# Only DISCRETE should be within G1 of the target
check(8, "DISCRETE is within G1 of target Ω/9", abs(DISCRETE - TARGET) / TARGET < G1_rel)
check(9, "G2_haar is not within G1 of target Ω/9", abs(G2_haar  - TARGET) / TARGET > G1_rel)
check(10, "SU3_haar is not within G1 of target Ω/9", abs(SU3_haar - TARGET) / TARGET > G1_rel)
print()
print("  DISCRETE is the unique Schur value within G1 of Ω/9.")
print()

# ── Test 4: 1/17² proximity ───────────────────────────────────────────────────
print("S4  1/17² proximity")
inv17sq  = 1.0 / 17**2          # 1/289
ratio_17 = G1_rel / inv17sq
print(f"  G1_rel     = {G1_rel:.10e}")
print(f"  1/17²      = {inv17sq:.10e}")
print(f"  ratio      = G1_rel / (1/17²) = {ratio_17:.6f}")
print(f"  deviation  = {abs(1 - ratio_17)*100:.4f}% from unity")
# Proximity is within 0.2% — note without asserting equality (it's not exact)
check(11, "Proximity within 0.2% of 1/17² — noted, not asserted structural",
      abs(1 - ratio_17) < 0.002)
print()

# ── Summary ───────────────────────────────────────────────────────────────────
print("SUMMARY")
print(f"  G1_abs = 432 − π·α⁻¹ = {G1_abs:.6f}")
print(f"  G1_rel = {G1_rel:.6e}  ({G1_rel*100:.4f}%)")
print(f"  432/Ω  = {432/BREATH_PERIOD:.10f}")
print()
print("  Proof of non-zero: 432 ∈ ℚ, Ω = 4π⁴+π³+π² ∉ ℚ (Lindemann–Weierstrass)")
print("  Closure class: empty by Schur orthogonality (all Haar integrals ∈ ℚ)")
print("  Status: KNOWN UNKNOWN — value computable, non-zero proved,")
print("          closure class proved empty.  G1 thread CLOSED (P151–P156).")
print(f"\n{'='*60}\nRESULT: {PASS} PASS / {FAIL} FAIL")
sys.exit(0 if FAIL == 0 else 1)
