"""Cartan-embedding labels versus orbit labels for the pair model of GL_2/(GL_1 x GL_1).

Pair model base point x_0 = (U_0, V_0), U_0 = <e_1 + e_2>, V_0 = <e_2> (O-split for O^2).
Involution theta = Ad(eps_0), eps_0 = +1 on U_0, -1 on V_0 (the only involution with
fixed group Stab(x_0)), Cartan embedding tau(g x_0) = g eps_0 g^{-1} eps_0^{-1}.
Point x_a = t^{(a,0)} x_0 has orbit label a (node A5-4).  If (a) of node A5-8 held,
tau(x_a) would lie in K t^{(a,-a)} K, i.e. have elementary divisors (p^{-a}, p^{a}).
Over F = Q_ell, ell prime, uniformizer ell, compute the elementary-divisor valuations of
tau(x_a).  Exact rational arithmetic.

Usage: python3 -I pair_cartan.py
"""
from fractions import Fraction as Fr


def mul(A, B):
    return [[sum(A[i][k] * B[k][j] for k in range(2)) for j in range(2)] for i in range(2)]


def vp(x, ell):
    if x == 0:
        return 10 ** 9
    x = Fr(x)
    v = 0
    n, d = x.numerator, x.denominator
    while n % ell == 0:
        n //= ell; v += 1
    while d % ell == 0:
        d //= ell; v -= 1
    return v


def eldiv(A, ell):
    d1 = min(vp(A[i][j], ell) for i in range(2) for j in range(2))
    det = A[0][0] * A[1][1] - A[0][1] * A[1][0]
    return (d1, vp(det, ell) - d1)


for ell in (2, 3, 5):
    eps = [[Fr(1), Fr(0)], [Fr(2), Fr(-1)]]  # eps e_1 = e_1 + 2 e_2, eps e_2 = -e_2
    for a in (1, 2, 3):
        t = [[Fr(ell) ** a, Fr(0)], [Fr(0), Fr(1)]]
        tinv = [[Fr(ell) ** (-a), Fr(0)], [Fr(0), Fr(1)]]
        tau = mul(mul(mul(t, eps), tinv), eps)   # eps^{-1} = eps
        print('ell=%d a=%d  orbit label a=%d, expected Cartan divisors (%d,%d), tau(x_a) divisors %s'
              % (ell, a, a, -a, a, eldiv(tau, ell)))
