"""
verify_P174_U3G2_branching.py
Addendum 174: G₂ → A₂ = SU(3) weight decomposition and θ_C = π/14.

Ten assertions covering:
  1.  G₂ Cartan matrix entries [[2,-1],[-3,2]]
  2.  |Φ_{G₂}| = 12; 6 short, 6 long
  3.  rank(G₂) = rank(A₂) = 2
  4.  Dimension check: 1 + 3 + 3 = 7
  5.  θ_C = π/14; sin(π/14) ≈ 0.22252 to 6 sig figs
  6.  G₂ root fan fundamental step = π/6 = 30°
  7.  π/14 = (π/6) × (3/7)  [root-fan × dimension-ratio identity]
  8.  Coxeter number h = 6; |Φ| = rank × h = 12
  9.  |Φ_{A₂}| = 6 ≤ |Φ_{G₂}| = 12
 10.  |sin(π/14) - 0.222520934| < 1e-6  [six-figure validation criterion]

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 not ok and detail:
        print(f"          {detail}")


# ---------------------------------------------------------------------------
# 1. G₂ Cartan matrix entries
# ---------------------------------------------------------------------------
# Convention: α₁ short, α₂ long.
# A₁₁ = 2, A₁₂ = 2(α₁·α₂)/|α₂|² = 2(-3/2)/3 = -1
# A₂₁ = 2(α₂·α₁)/|α₁|² = 2(-3/2)/1 = -3, A₂₂ = 2

a1 = (mpmath.mpf(1), mpmath.mpf(0))
a2 = (mpmath.mpf(-3) / 2, sq3 / 2)

a1_sq = a1[0] ** 2 + a1[1] ** 2   # = 1
a2_sq = a2[0] ** 2 + a2[1] ** 2   # = 3
dot12 = a1[0] * a2[0] + a1[1] * a2[1]  # = -3/2

A11 = mpmath.mpf(2)
A12 = 2 * dot12 / a2_sq   # should be -1
A21 = 2 * dot12 / a1_sq   # should be -3
A22 = mpmath.mpf(2)

ok1 = (A11 == 2 and mpmath.almosteq(A12, -1, 1e-50)
       and mpmath.almosteq(A21, -3, 1e-50) and A22 == 2)
report(
    "1. G₂ Cartan matrix = [[2,-1],[-3,2]]",
    ok1,
    f"A12={float(A12):.6f}, A21={float(A21):.6f}",
)


# ---------------------------------------------------------------------------
# 2. |Φ_{G₂}| = 12; 6 short roots, 6 long roots
# ---------------------------------------------------------------------------
# Positive roots
pos_roots = [
    a1,
    a2,
    (a1[0] + a2[0],     a1[1] + a2[1]),     # α₁+α₂
    (2*a1[0] + a2[0],   2*a1[1] + a2[1]),   # 2α₁+α₂
    (3*a1[0] + a2[0],   3*a1[1] + a2[1]),   # 3α₁+α₂
    (3*a1[0] + 2*a2[0], 3*a1[1] + 2*a2[1]), # 3α₁+2α₂
]

short_roots = [(x, y) for (x, y) in pos_roots if mpmath.almosteq(x**2 + y**2, 1, 1e-50)]
long_roots  = [(x, y) for (x, y) in pos_roots if mpmath.almosteq(x**2 + y**2, 3, 1e-50)]

# Total roots including negatives
n_roots       = 2 * len(pos_roots)       # = 12
n_short_total = 2 * len(short_roots)     # = 6
n_long_total  = 2 * len(long_roots)      # = 6

ok2 = (n_roots == 12 and n_short_total == 6 and n_long_total == 6)
report(
    "2. |Φ_{G₂}|=12 (6 short + 6 long)",
    ok2,
    f"n_roots={n_roots}, short={n_short_total}, long={n_long_total}",
)


# ---------------------------------------------------------------------------
# 3. rank(G₂) = rank(A₂) = 2
# ---------------------------------------------------------------------------
rank_G2 = 2   # two simple roots α₁, α₂
rank_A2 = 2   # A₂ has simple roots β₁ = α₂, β₂ = 3α₁+α₂ (both in ℝ²)

ok3 = (rank_G2 == 2 and rank_A2 == 2)
report("3. rank(G₂) = rank(A₂) = 2", ok3)


# ---------------------------------------------------------------------------
# 4. Dimension check: 1 + 3 + 3 = 7
# ---------------------------------------------------------------------------
dim_singlet = 1
dim_triplet = 3
dim_antitriplet = 3
dim_7 = dim_singlet + dim_triplet + dim_antitriplet

ok4 = (dim_7 == 7)
report(f"4. dim(1) + dim(3) + dim(3̄) = {dim_7} = 7", ok4)


# ---------------------------------------------------------------------------
# 5. θ_C = π/14; sin(π/14) ≈ 0.22252 to 6 sig figs
# ---------------------------------------------------------------------------
theta_C = pi / 14
sin_thetaC = mpmath.sin(theta_C)

# 6 sig figs: 0.22252 → |sin(π/14) - 0.22252| < 5e-6
ok5 = abs(sin_thetaC - mpmath.mpf('0.22252')) < mpmath.mpf('5e-6')
report(
    f"5. sin(π/14) ≈ 0.22252 to 6 sig figs  [actual: {mpmath.nstr(sin_thetaC, 12)}]",
    ok5,
    f"sin(π/14) = {float(sin_thetaC):.10f}",
)


# ---------------------------------------------------------------------------
# 6. G₂ root fan fundamental step = π/6
# ---------------------------------------------------------------------------
# The 12 roots are uniformly distributed with step 2π/12 = π/6.
root_fan_step = 2 * pi / 12          # = π/6
pi_over_6     = pi / 6

ok6 = mpmath.almosteq(root_fan_step, pi_over_6, 1e-50)
report(
    f"6. G₂ root fan step = π/6 = 30°  [= 2π/12, uniform over 12 roots]",
    ok6,
    f"step = {float(root_fan_step):.8f} rad = {float(root_fan_step*180/pi):.4f}°",
)


# ---------------------------------------------------------------------------
# 7. π/14 = (π/6) × (3/7)
# ---------------------------------------------------------------------------
dim_ratio = mpmath.mpf(3) / mpmath.mpf(7)     # dim(3) / dim(7)
rhs = pi_over_6 * dim_ratio

ok7 = mpmath.almosteq(theta_C, rhs, 1e-50)
report(
    f"7. π/14 = (π/6) × (3/7)  [root-fan step × dim(3)/dim(7)]",
    ok7,
    f"π/14={float(theta_C):.10f}, (π/6)(3/7)={float(rhs):.10f}",
)


# ---------------------------------------------------------------------------
# 8. Coxeter number h = 6; |Φ_{G₂}| = rank × h = 2 × 6 = 12
# ---------------------------------------------------------------------------
h_G2              = 6
coxeter_root_count = rank_G2 * h_G2    # = 12

ok8 = (h_G2 == 6 and coxeter_root_count == 12 and coxeter_root_count == n_roots)
report(
    f"8. Coxeter h=6; |Φ|=rank×h={coxeter_root_count}=12",
    ok8,
    f"rank={rank_G2}, h={h_G2}, product={coxeter_root_count}, actual n_roots={n_roots}",
)


# ---------------------------------------------------------------------------
# 9. |Φ_{A₂}| = 6 ≤ |Φ_{G₂}| = 12
# ---------------------------------------------------------------------------
# The A₂ root system inside G₂ consists of the 6 long roots of G₂.
# A₂ has rank 2, h(A₂) = 3, so |Φ_{A₂}| = 2 × 3 = 6.
h_A2          = 3
n_roots_A2    = rank_A2 * h_A2    # = 6

ok9 = (n_roots_A2 == 6 and n_roots_A2 <= n_roots)
report(
    f"9. |Φ_{{A₂}}|={n_roots_A2} ≤ |Φ_{{G₂}}|={n_roots}  (A₂ embedded via long roots)",
    ok9,
)


# ---------------------------------------------------------------------------
# 10. Validation criterion: |sin(π/14) - 0.222520934| < 1e-6
# ---------------------------------------------------------------------------
reference_value = mpmath.mpf('0.222520934')
deviation = abs(sin_thetaC - reference_value)

ok10 = deviation < mpmath.mpf('1e-6')
report(
    f"10. |sin(π/14) - 0.222520934| = {mpmath.nstr(deviation, 4)} < 1e-6",
    ok10,
    f"sin(π/14) = {mpmath.nstr(sin_thetaC, 15)}, ref = 0.222520934",
)


# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
print()
print(f"\n{'='*60}\nRESULT: {len(PASSES)} PASS / {len(FAILS)} FAIL")
if FAILS:
    print(f"  FAILED checks: {FAILS}")
    raise SystemExit(1)
