"""Exact examples for H1 and Q1 on squares, stars, a cube and a cylinder.

Download quad_lattice.py alongside this file. Run with Python 3 and SymPy.
"""
import sympy as sp

from quad_lattice import adler_pair, cross_ratio, cube, h1, transport


def square_and_cube():
    e, v = sp.symbols("epsilon v")
    zero, one, three = map(sp.Integer, (0, 1, 3))
    assert sp.cancel(h1(zero, one, v, 8, 5) - 3/(v-1)) == 0
    assert sp.cancel(h1(one, three, 3-e, 2, 7) - (1+5/e)) == 0
    upper = cube(h1, zero, one, three, 7+e, 2, 5, 11)
    assert all(sp.cancel(z + 3 + 24/e) == 0 for z in upper)
    lower = (h1(zero, one, three, 2, 5), h1(zero, one, 7, 2, 11),
             h1(zero, three, 7, 5, 11))
    assert set(lower) == {-sp.Rational(3, 2)}
    print("Cube: all upper routes give -3-24/epsilon; at zero all lower values are -3/2.")


def q1_edge_and_star():
    x, u, v, z, p, q, delta = sp.symbols("x u v z p q delta")
    face = p*(x-v)*(u-z) - q*(x-u)*(v-z) + delta**2*p*q*(p-q)
    h = face*sp.diff(face, v, z) - sp.diff(face, v)*sp.diff(face, z)
    assert sp.expand(h + q*(p-q)*((x-u)**2-delta**2*p**2)) == 0
    edge = face.subs({x: 0, u: 2, p: 2, q: 1, delta: 1})
    assert sp.expand(edge - 2*(v-1)*(z-1)) == 0

    neighbors = list(map(sp.sympify, [2, sp.I, -2, -sp.I]))
    labels = (4, -1, 4, -1)
    short_legs = [label/neighbor for label, neighbor in zip(labels, neighbors)]
    changed = [sp.simplify(label/(leg+1)) for label, leg in zip(labels, short_legs)]
    assert changed == [sp.Rational(4, 3), (-1+sp.I)/2, -4, (-1-sp.I)/2]
    for j in range(4):
        k = (j+1) % 4
        diagonal = neighbors[j] + neighbors[k]
        expected = sp.Rational(labels[j], labels[k])
        for row in (neighbors, changed):
            assert cross_ratio(0, row[j], diagonal, row[k]) == expected
    print("Q1: edge factors as 2(v-1)(z-1); both star reconstructions pass four faces.")


def auxiliary_seed_and_transform():
    w = sp.symbols("w")
    b = transport(3, 2, 7, 0)*transport(1, 3, 2, 0)
    c = transport(-1, 2, 2, 0)*transport(1, -1, 7, 0)
    assert b.det() == c.det() == 14
    seed = sp.Matrix([w, 1])
    match = sp.Matrix.hstack(b*seed, c*seed).det()
    assert sp.expand(match + w**2-11*w+16) == 0
    assert sp.gcd(match, (w-3)*(w+1)*(w-5)*(w-6)) == 1
    assert sp.discriminant(match, w) == 57

    def base(n, m):
        return n + 2*m

    def transformed(n, m):
        ratio = sp.Rational((-2)**n * (-5)**m, 7)
        return base(n, m) + 3*(1+ratio)/(1-ratio)

    for n in range(4):
        for m in range(4):
            x, t = base(n, m), transformed(n, m)
            east, north = transformed(n+1, m), transformed(n, m+1)
            assert (x-east)*(base(n+1, m)-t) == 8
            assert (x-north)*(base(n, m+1)-t) == 5
            assert (t-transformed(n+1, m+1))*(east-north) == 3
    assert transformed(1, 1) == -14
    print("H1: off-shell seed match discriminant 57; explicit transform passes 16 plaquettes.")


def periodic_reduction_and_yang_baxter_map():
    h, d = sp.symbols("h d", nonzero=True)
    pair = (h, -h)
    for expected in [(-h, -d/h), (-d/h, d/h), (d/h, h), (h, -h)]:
        a, b = pair
        pair = (b, sp.cancel(d/b-b-a))
        assert pair == expected
    x, x1, x2, a, b = sp.symbols("X X_1 X_2 a b")
    x12 = h1(x, x1, x2, a, b)
    edge_pair = (x12-x2, x1-x12)
    output = adler_pair(x1-x, x-x2, a, b)
    assert all(sp.cancel(s-t) == 0 for s, t in zip(edge_pair, output))
    print("H1: T^4 is identity on the reducible level; directed edges reproduce Adler's map.")


def three_site_cylinder():
    a, z = sp.symbols("A z")
    m = transport(3, 0, a, 0)*transport(1, 3, a, 0)*transport(0, 1, a, 0)
    assert sp.expand(m.det()) == -a**3
    assert sp.expand(m.trace()**2-4*m.det()) == 4*(a**3+9)
    result = m*sp.Matrix([z, 1])
    fixed = z*result[1] - result[0]
    assert sp.expand(fixed.subs(a, -2) + (z-1)*(5*z-7)) == 0
    row = [sp.Rational(7, 5)]
    for left, right in [(0, 1), (1, 3), (3, 0)]:
        next_value = left + sp.Rational(-2)/(row[-1]-right)
        assert (left-next_value)*(right-row[-1]) == -2
        row.append(next_value)
    assert row == [sp.Rational(7, 5), -5, sp.Rational(5, 4), sp.Rational(7, 5)]
    print("Cylinder: discriminant 4(A^3+9); finite row at A=-2:", row)


if __name__ == "__main__":
    square_and_cube()
    q1_edge_and_star()
    auxiliary_seed_and_transform()
    periodic_reduction_and_yang_baxter_map()
    three_site_cylinder()
