"""
verify_P153.py
Computation for Addendum 153: Does B_{F₄}(H_s, H_s) = π·α⁻¹?

Checks whether the Killing form of F₄ evaluated on the G₂ short coroot H_s
equals BREATH_PERIOD = π·α⁻¹ ≈ 430.806.

Copyright: Léon Fernando Vlegels. License: MIT. 2026-05-15.
"""

import math
import numpy as np

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}")

ALPHA_INV = 4*math.pi**3 + math.pi**2 + math.pi
BREATH_PERIOD = math.pi * ALPHA_INV  # π·α⁻¹ ≈ 430.806

# F₄ root system in ℝ⁴ (standard coordinates)
# Short roots (length 1): all permutations of (±1, 0, 0, 0) — 8 roots
# and (±1/2, ±1/2, ±1/2, ±1/2) — 16 roots → 24 short positive roots total?
# Actually F₄: 48 roots total = 24 short + 24 long
# Short roots (length 1): ±e_i (8) + (1/2)(±1,±1,±1,±1) (16) = 24
# Long roots (length √2): ±e_i ± e_j for i<j (24)

short_roots = []
# ±e_i
for i in range(4):
    v = [0,0,0,0]; v[i] = 1; short_roots.append(v[:])
    v[i] = -1; short_roots.append(v[:])
# (1/2)(±1,±1,±1,±1)
for s0 in [1,-1]:
    for s1 in [1,-1]:
        for s2 in [1,-1]:
            for s3 in [1,-1]:
                short_roots.append([s0/2, s1/2, s2/2, s3/2])

long_roots = []
for i in range(4):
    for j in range(i+1,4):
        for si in [1,-1]:
            for sj in [1,-1]:
                v = [0,0,0,0]; v[i]=si; v[j]=sj
                long_roots.append(v[:])

print(f"Short roots: {len(short_roots)}, Long roots: {len(long_roots)}")
check(1, "F4 short root count = 24", len(short_roots) == 24)
check(2, "F4 long root count = 24", len(long_roots) == 24)

# G₂ ⊂ F₄: G₂ acts on the imaginary octonions (7-dim).
# In the F₄ root space ℝ⁴, the G₂ short coroot H_s is embedded as a specific vector.
# From P151: H_s in G₂ corresponds to the simple coroot α₁∨ where α₁ is the short simple root.
# In F₄ with simple roots α₁(short),α₂(short),α₃(long),α₄(long),
# the G₂ subalgebra is generated by {α₁,α₂} in the B₃⊂F₄ chain, or by the long-root B₃.
# Standard embedding: G₂ ⊂ B₃ ⊂ F₄, where B₃ is generated by {α₂,α₃,α₄}.
# The G₂ short coroot in F₄ coordinates: H_s = (0,1,-1,0) or similar —
# need to use explicit F₄ simple roots.

# F₄ simple roots in ℝ⁴ (Bourbaki convention):
# α₁ = e₂ - e₃  (short)
# α₂ = e₃ - e₄  (short)
# α₃ = e₄       (short)
# α₄ = (1/2)(e₁ - e₂ - e₃ - e₄)  (short)
# Wait — standard F₄ Bourbaki:
# α₁ = e₂ - e₃, α₂ = e₃ - e₄, α₃ = e₄, α₄ = (e₁-e₂-e₃-e₄)/2

# The G₂ ⊂ F₄ embedding: G₂ is generated by the long roots of F₄ that are short for B₃.
# Alternatively: G₂ is the fixed-point subalgebra of the order-3 automorphism of D₄.
# In the F₄ root system the G₂ roots are identified as follows:
# Use the embedding G₂ ↪ F₄ via the 7-dimensional representation.

# Simplest approach: compute B_{F₄}(H,H) for H = coroot of the short simple root of G₂.
# The G₂ short coroot in F₄ basis: coroot of α_short(G₂) = 2α_short/|α_short|²
# In the standard G₂ ⊂ B₃ ⊂ F₄ embedding, the G₂ short simple root corresponds to α₁(B₃) = e₁-e₂.
# Its coroot is H_s = e₁ - e₂ (since |α|²=2 for long roots of B₃ which are short of G₂).

# Let's compute for H = e₁ - e₂ = (1,-1,0,0):
H_s = np.array([1,-1,0,0])

# B_F4(H_s,H_s) = sum over all roots α of <α,H_s>²  (using standard inner product)
all_roots = short_roots + long_roots
B_F4 = sum(np.dot(np.array(r), H_s)**2 for r in all_roots)

# Also try the G₂ subalgebra alone (the 12 G₂ roots) — should give 48 from P151
# The G₂ roots embedded in F₄ for the B₃⊃G₂ case are the long roots of B₃:
# {±e_i ± e_j} restricted to i,j ∈ {1,2,3} — that's 12 roots
g2_roots = [[r[0],r[1],r[2],r[3]] for r in long_roots if r[3]==0 and (abs(r[0])+abs(r[1])+abs(r[2]))==2]
B_G2_check = sum(np.dot(np.array(r), H_s)**2 for r in g2_roots)

print(f"H_s = {H_s}")
print(f"B_F4(H_s,H_s) = {B_F4}")
print(f"B_G2_check = {B_G2_check}  (expect 48 from P151)")
print(f"BREATH_PERIOD = π·α⁻¹ = {BREATH_PERIOD:.8f}")
print(f"Gap G1: (432 - BREATH_PERIOD)/BREATH_PERIOD = {(432-BREATH_PERIOD)/BREATH_PERIOD*100:.4f}%")
print(f"B_F4 vs BREATH_PERIOD: diff = {B_F4 - BREATH_PERIOD:.6f}, relative = {(B_F4-BREATH_PERIOD)/BREATH_PERIOD*100:.4f}%")
print(f"G1 closed by F4? {abs(B_F4 - BREATH_PERIOD) < 0.01}")

# Additional analysis: what integer/expression does B_F4 equal?
print(f"\n--- Additional analysis ---")
print(f"B_F4 = {B_F4}")
print(f"B_F4 as integer? {int(round(B_F4))} (diff from int: {B_F4 - round(B_F4):.2e})")
print(f"B_F4 / B_G2 (P151 = 48) = {B_F4 / 48:.6f}")
print(f"B_F4 / 432 = {B_F4 / 432:.6f}")
print(f"B_F4 / BREATH_PERIOD = {B_F4 / BREATH_PERIOD:.6f}")

# Decompose by short vs long F₄ roots
B_F4_short = sum(np.dot(np.array(r), H_s)**2 for r in short_roots)
B_F4_long = sum(np.dot(np.array(r), H_s)**2 for r in long_roots)
print(f"\nDecomposition:")
print(f"  B_F4 from short F₄ roots: {B_F4_short}")
print(f"  B_F4 from long  F₄ roots: {B_F4_long}")
print(f"  B_F4_long / B_F4_short = {B_F4_long / B_F4_short:.4f}")

print(f"\n{'='*60}\nRESULT: {PASS} PASS / {FAIL} FAIL")
