"""Check of A2-10 for n = 2: X_g(F) = {y in GL_2(E): y sigma(y) = 1}, g.y = g y sigma(g)^{-1}.
(T_lam * phi_0)(y) = #{gK in K t^lam K/K : g^{-1} y sigma(g) in GL_2(O_E)}.
Representatives of the K-orbit of type (c,-c): y_c = [[0, t^c],[t^{-c}, 0]].
Reuses the exact arithmetic of herm_audit.py.
"""
import os
here = os.path.dirname(os.path.abspath(__file__))
src = open(os.path.join(here, "herm_audit.py")).read().split("def run(")[0]
exec(src)


def sigma_mat(A):
    return [[A[i][j].conj() for j in range(2)] for i in range(2)]


def inv_upper(F, g):
    pass


def run(q, eps):
    F = make_field(q, eps)
    print(f"=== q = {q}")
    for lam in [(0, 0), (1, 0), (1, 1), (2, 0), (3, 0)]:
        ginvs = coset_inverses(F, q, lam)
        vals = {}
        for c in range(0, 5):
            y = [[LP(F), mono(F, c)], [mono(F, -c), LP(F)]]
            cnt = 0
            for gi in ginvs:
                # g = gi^{-1}; need g^{-1} y sigma(g) = gi * y * sigma(gi)^{-1}.
                # sigma(gi)^{-1} = sigma(gi^{-1}) = sigma(g); compute g from gi (upper triangular, monomial diag)
                # gi = [[t^a, u],[0, t^b]]  ->  g = [[t^-a, -u t^{-a-b}],[0, t^-b]]
                a = gi[0][0].val(); b = gi[1][1].val()
                u = gi[0][1]
                g = [[mono(F, -a), (-u) * mono(F, -a - b)], [LP(F), mono(F, -b)]]
                Y = matmul(matmul(gi, y), sigma_mat(g))
                if is_unimodular(Y):
                    cnt += 1
            if cnt:
                vals[c] = cnt
        print(f"  T_{lam} * phi_0 on labels (c,-c): {vals}")


run(3, 2)
