"""
verify_P175_deltaQLC.py
Addendum 175: δ_QLC from G₂ → A₂ Weight-Lattice Geometry.

Assertions:
  1.  θ_C = π/14; sin(π/14) = λ ≈ 0.22252 (6 sig figs)
  2.  QLC exact complementarity: π/4 − π/14 = 5π/28 ≈ 32.143°
  3.  Adjacent 3/3̄ weight angle = π/3 (exact, from hyperplane coords)
  4.  G₂/A₂ angular mismatch: π/6 − π/14 = 2π/21
  5.  Gross QLC mismatch: π/3 − π/4 = π/12
  6.  Primary formula: δ_QLC = (3/5)·sin(π/14)⁴; compute value
  7.  Alternative formula: δ_QLC = (7/12)·sin(π/14)⁴; compute value
  8.  Primary formula within 5% of fitted 0.00147 — ASSERT TRUE
  9.  Alternative formula within 5% of fitted 0.00147 — ASSERT TRUE
 10.  Primary formula strictly more accurate than alternative
 11.  Coefficient identity: 3/5 = dim(3-rep)/(dim(7-rep) − rank(G₂))
 12.  Alternative identity: 7/12 = dim(7-rep)/|Φ_{G₂}|
 13.  λ⁴ is in the correct O(10⁻³) regime for δ_QLC

All arithmetic uses mpmath at mp.dps = 55.
"""

import mpmath
mpmath.mp.dps = 55

pi  = mpmath.pi
sq3 = mpmath.sqrt(3)

PASSES = []
FAILS  = []


def report(name, ok, detail=""):
    if ok:
        PASSES.append(name)
    else:
        FAILS.append(name)
    status = "PASS" if ok else "FAIL"
    print(f"  [{status}] {name}")
    if detail:
        print(f"          {detail}")


# ─────────────────────────────────────────────────────────────────────────────
# Fundamental constants
# ─────────────────────────────────────────────────────────────────────────────

theta_C  = pi / 14
lam      = mpmath.sin(theta_C)          # Wolfenstein parameter
lam4     = lam ** 4

FITTED   = mpmath.mpf('0.00147')        # P74 fitted value (exact as given)
TOL_5PCT = mpmath.mpf('0.05')           # 5% tolerance

# G₂ / A₂ representation data
DIM_7    = mpmath.mpf(7)                # dim(G₂ fundamental rep)
DIM_3    = mpmath.mpf(3)                # dim(A₂ triplet)
RANK_G2  = mpmath.mpf(2)               # rank(G₂) = rank(A₂) = 2
N_ROOTS_G2 = mpmath.mpf(12)            # |Φ_{G₂}| = 12


# ─────────────────────────────────────────────────────────────────────────────
# 1.  θ_C = π/14; sin(π/14) ≈ 0.22252 to 6 sig figs
# ─────────────────────────────────────────────────────────────────────────────
ok1 = abs(lam - mpmath.mpf('0.22252')) < mpmath.mpf('5e-6')
report(
    f"1. sin(π/14) ≈ 0.22252 [actual: {mpmath.nstr(lam, 12)}]",
    ok1,
    f"λ = {mpmath.nstr(lam, 20)}",
)


# ─────────────────────────────────────────────────────────────────────────────
# 2.  QLC exact complementarity: π/4 − π/14 = 5π/28
# ─────────────────────────────────────────────────────────────────────────────
qlc_exact  = pi / 4 - pi / 14          # = 5π/28
five_pi_28 = 5 * pi / 28
deg_qlc    = qlc_exact * 180 / pi

ok2 = mpmath.almosteq(qlc_exact, five_pi_28, 1e-50)
report(
    f"2. π/4 − π/14 = 5π/28 ≈ {mpmath.nstr(deg_qlc, 8)}°",
    ok2,
    f"5π/28 = {mpmath.nstr(five_pi_28, 15)} rad",
)


# ─────────────────────────────────────────────────────────────────────────────
# 3.  Adjacent 3/3̄ weight angle = π/3
# ─────────────────────────────────────────────────────────────────────────────
# A₂ hyperplane weights: rows of (2,-1,-1)/√6, (-1,2,-1)/√6, (-1,-1,2)/√6
# 3-rep:    w1=(2,-1,-1)/√6; 3̄-rep: wb2=(1,-2,1)/√6 (adjacent choice)
f = mpmath.mpf

w1  = [f('2')/f('6'), f('-1')/f('6'), f('-1')/f('6')]
wb2 = [f('1')/f('6'), f('-2')/f('6'), f('1')/f('6')]

norm2 = lambda v: sum(x**2 for x in v)
dot   = lambda a, b: sum(x*y for x, y in zip(a, b))

# Use un-normalised vectors; normalise in the angle computation
n_w1  = mpmath.sqrt(norm2(w1))
n_wb2 = mpmath.sqrt(norm2(wb2))
cos_adj = dot(w1, wb2) / (n_w1 * n_wb2)
angle_adj = mpmath.acos(cos_adj)

ok3 = mpmath.almosteq(angle_adj, pi / 3, 1e-50)
report(
    f"3. Adjacent 3/3̄ weight angle = π/3 = 60°",
    ok3,
    f"angle = {mpmath.nstr(angle_adj, 15)} rad, π/3 = {mpmath.nstr(pi/3, 15)} rad",
)


# ─────────────────────────────────────────────────────────────────────────────
# 4.  G₂/A₂ angular mismatch: π/6 − π/14 = 2π/21
# ─────────────────────────────────────────────────────────────────────────────
delta_root  = pi / 6 - pi / 14
two_pi_21   = 2 * pi / 21

ok4 = mpmath.almosteq(delta_root, two_pi_21, 1e-50)
report(
    f"4. G₂/A₂ mismatch π/6 − π/14 = 2π/21 ≈ {mpmath.nstr(delta_root, 8)} rad",
    ok4,
    f"2π/21 = {mpmath.nstr(two_pi_21, 15)}",
)


# ─────────────────────────────────────────────────────────────────────────────
# 5.  Gross QLC mismatch: π/3 − π/4 = π/12
# ─────────────────────────────────────────────────────────────────────────────
Delta        = pi / 3 - pi / 4
pi_over_12   = pi / 12

ok5 = mpmath.almosteq(Delta, pi_over_12, 1e-50)
report(
    f"5. Gross QLC mismatch π/3 − π/4 = π/12 ≈ {mpmath.nstr(Delta, 8)} rad",
    ok5,
    f"π/12 = {mpmath.nstr(pi_over_12, 15)}",
)


# ─────────────────────────────────────────────────────────────────────────────
# 6.  Primary formula: δ_QLC = (3/5)·λ⁴
# ─────────────────────────────────────────────────────────────────────────────
coeff_primary = DIM_3 / (DIM_7 - RANK_G2)   # = 3/(7-2) = 3/5
d_primary     = coeff_primary * lam4

ok6 = mpmath.almosteq(coeff_primary, mpmath.mpf(3) / mpmath.mpf(5), 1e-50)
report(
    f"6. Primary coefficient 3/(7−2) = 3/5 = {float(coeff_primary):.6f}",
    ok6,
    f"δ_QLC(primary) = {mpmath.nstr(d_primary, 10)}",
)


# ─────────────────────────────────────────────────────────────────────────────
# 7.  Alternative formula: δ_QLC = (7/12)·λ⁴
# ─────────────────────────────────────────────────────────────────────────────
coeff_alt = DIM_7 / N_ROOTS_G2              # = 7/12
d_alt     = coeff_alt * lam4

ok7 = mpmath.almosteq(coeff_alt, mpmath.mpf(7) / mpmath.mpf(12), 1e-50)
report(
    f"7. Alternative coefficient 7/|Φ_G₂| = 7/12 = {float(coeff_alt):.6f}",
    ok7,
    f"δ_QLC(alt)     = {mpmath.nstr(d_alt, 10)}",
)


# ─────────────────────────────────────────────────────────────────────────────
# 8.  Primary formula within 5% of fitted 0.00147
# ─────────────────────────────────────────────────────────────────────────────
err_primary = abs(d_primary - FITTED) / FITTED
ok8 = err_primary < TOL_5PCT
report(
    f"8. Primary δ_QLC within 5% of 0.00147  [err = {mpmath.nstr(err_primary*100, 4)}%]",
    ok8,
    f"δ_QLC(primary) = {mpmath.nstr(d_primary, 8)}, target = {FITTED}",
)


# ─────────────────────────────────────────────────────────────────────────────
# 9.  Alternative formula within 5% of fitted 0.00147
# ─────────────────────────────────────────────────────────────────────────────
err_alt = abs(d_alt - FITTED) / FITTED
ok9 = err_alt < TOL_5PCT
report(
    f"9. Alternative δ_QLC within 5% of 0.00147  [err = {mpmath.nstr(err_alt*100, 4)}%]",
    ok9,
    f"δ_QLC(alt)     = {mpmath.nstr(d_alt, 8)}, target = {FITTED}",
)


# ─────────────────────────────────────────────────────────────────────────────
# 10. Primary formula strictly more accurate than alternative
# ─────────────────────────────────────────────────────────────────────────────
ok10 = err_primary < err_alt
report(
    f"10. Primary is more accurate than alternative  [{mpmath.nstr(err_primary*100,4)}% < {mpmath.nstr(err_alt*100,4)}%]",
    ok10,
)


# ─────────────────────────────────────────────────────────────────────────────
# 11. Coefficient identity: 3/5 = dim(3)/(dim(7) − rank(G₂))
# ─────────────────────────────────────────────────────────────────────────────
lhs11 = mpmath.mpf(3) / mpmath.mpf(5)
rhs11 = DIM_3 / (DIM_7 - RANK_G2)

ok11 = mpmath.almosteq(lhs11, rhs11, 1e-50)
report(
    "11. 3/5 = dim(3)/(dim(7) − rank(G₂)) = 3/(7−2)",
    ok11,
    f"lhs={float(lhs11)}, rhs={float(rhs11)}",
)


# ─────────────────────────────────────────────────────────────────────────────
# 12. Alternative identity: 7/12 = dim(7)/|Φ_{G₂}|
# ─────────────────────────────────────────────────────────────────────────────
lhs12 = mpmath.mpf(7) / mpmath.mpf(12)
rhs12 = DIM_7 / N_ROOTS_G2

ok12 = mpmath.almosteq(lhs12, rhs12, 1e-50)
report(
    "12. 7/12 = dim(7)/|Φ_{G₂}| = 7/12",
    ok12,
    f"lhs={float(lhs12):.8f}, rhs={float(rhs12):.8f}",
)


# ─────────────────────────────────────────────────────────────────────────────
# 13. λ⁴ is in the correct O(10⁻³) regime
# ─────────────────────────────────────────────────────────────────────────────
ok13 = (mpmath.mpf('5e-4') < lam4 < mpmath.mpf('5e-3'))
report(
    f"13. λ⁴ ∈ (5×10⁻⁴, 5×10⁻³)  [λ⁴ = {mpmath.nstr(lam4, 6)}]",
    ok13,
    f"λ⁴ = {mpmath.nstr(lam4, 20)}",
)


# ─────────────────────────────────────────────────────────────────────────────
# Summary table
# ─────────────────────────────────────────────────────────────────────────────
print()
print("=" * 64)
print("  P175 δ_QLC Summary")
print("=" * 64)
print(f"  λ  = sin(π/14)       = {mpmath.nstr(lam, 16)}")
print(f"  λ⁴                   = {mpmath.nstr(lam4, 16)}")
print(f"  δ_QLC (primary 3/5)  = {mpmath.nstr(d_primary, 12)}  [{mpmath.nstr(err_primary*100,4)}% from 0.00147]")
print(f"  δ_QLC (alt 7/12)     = {mpmath.nstr(d_alt, 12)}  [{mpmath.nstr(err_alt*100,4)}% from 0.00147]")
print(f"  5%-criterion met     : {'YES (both)' if ok8 and ok9 else 'NO'}")
print(f"  Primary preferred    : {'YES' if ok10 else 'NO'}")
print("=" * 64)
print()
print(f"\n{'='*60}\nRESULT: {len(PASSES)} PASS / {len(FAILS)} FAIL")

if FAILS:
    print(f"  FAILED checks: {FAILS}")
    raise SystemExit(1)
