#!/usr/bin/env python3
"""End-to-end check of the decomposition used in the note, including rectangular labels.

Builds the explicit rank-one terms of the proposition in the note (row form when s <= 0, column form when s > 0), for x,v in L(h) and
y,u in L(E), and verifies that their sum is z_v [x=v][y=u] entry by entry, over Z (p=0) or F_p. A row term is
F1(x,y) F2(u,v), a column term is G1(x,u) G2(y,v). Since T = sum of terms, the non-commutative polynomial
ID_{n,m} = sum T[x,y,u,v] x_x y_y x_v y_u is the corresponding sum of products, so this also certifies the
circuit decomposition. Terms that vanish identically on the labels are not counted.

Usage: python check_explicit.py p h E
"""
import sys
from check_pieces import Ring, Ops, a_of, a_bracket, dims


def run(p, h, E):
    ring = Ring(p)
    ops = Ops(ring)
    Lh = ring.labels(h)
    LE = ring.labels(E)
    n, m = len(Lh), len(LE)
    if not n or not m:
        print(f"p={p} h={h} E={E}: empty label set")
        return None
    polys = {x: a_of(x, ring) for x in Lh}
    levels = {}
    for L in range(0, 2 * (E + h) + 1):
        levels[L] = ring.labels(L)
    terms = 0
    total = {}
    for s in range(-(E + h), E + h + 1):
        for i in range(0, 2 * h + 1):
            if s <= 0:
                L = E + s - h + i
                if L < 0 or L not in levels:
                    continue
                for sigma in levels[L]:
                    F1 = {}
                    for x in Lh:
                        ai = a_bracket(polys[x], h, i, ring)
                        for y in LE:
                            c = ops.act(ai, s - h + i, {y: 1}).get(sigma, 0)
                            if c:
                                F1[(x, y)] = (-1) ** i * c
                    F2 = {}
                    for v in Lh:
                        w = ops.act({v: 1}, h - s - i, {sigma: 1})
                        for u, c in w.items():
                            if u in LE:
                                F2[(u, v)] = c
                    if F1 and F2:
                        terms += 1
                        for (x, y), c1 in F1.items():
                            for (u, v), c2 in F2.items():
                                k = (x, y, u, v)
                                total[k] = total.get(k, 0) + c1 * c2
            else:
                L = E + h - s - i
                if L < 0 or L not in levels:
                    continue
                for sigma in levels[L]:
                    G1 = {}
                    for x in Lh:
                        ai = a_bracket(polys[x], h, i, ring)
                        w = ops.act(ai, s - h + i, {sigma: 1})
                        for u, c in w.items():
                            if u in LE:
                                G1[(x, u)] = (-1) ** i * c
                    G2 = {}
                    for v in Lh:
                        for y in LE:
                            c = ops.act({v: 1}, h - s - i, {y: 1}).get(sigma, 0)
                            if c:
                                G2[(y, v)] = c
                    if G1 and G2:
                        terms += 1
                        for (x, u), c1 in G1.items():
                            for (y, v), c2 in G2.items():
                                k = (x, y, u, v)
                                total[k] = total.get(k, 0) + c1 * c2
    ok = True
    for x in Lh:
        for v in Lh:
            for y in LE:
                for u in LE:
                    want = ring.z(v) if (x == v and y == u) else 0
                    if ring.reduce(total.get((x, y, u, v), 0) - want) != 0:
                        ok = False
    d = dims(ring, 2 * (E + h))
    print(f"p={p} h={h} E={E}: n={n} m={m}; nonzero rank-one terms = {terms}; n^2 m^2 = {(n*m)**2}; "
          f"dim V'_(E+h) = {d[E+h]}; identity exact: {ok}", flush=True)
    return ok


if __name__ == '__main__':
    p, h, E = map(int, sys.argv[1:4])
    r = run(p, h, E)
    sys.exit(0 if r is not False else 1)
