"""Local quad equations, cube consistency, transport and the (2,1) H1 reduction.

Run with Python 3 and SymPy. All comparisons are exact rational identities,
or exact values on the indicated nondegenerate chart.
"""
import sympy as sp


def h1(x, u, v, p, q):
    return x - (p - q) / (u - v)


def cross_update(x, u, v, p, q):
    return sp.cancel((p*(x-v)*u - q*(x-u)*v) / (p*(x-v) - q*(x-u)))


def cross_ratio(x, u, z, v):
    return sp.cancel((x-u)*(z-v) / ((u-z)*(v-x)))


def cube(update, x, a, b, c, p, q, r):
    ab = update(x, a, b, p, q)
    ac = update(x, a, c, p, r)
    bc = update(x, b, c, q, r)
    return (update(a, ab, ac, q, r), update(b, ab, bc, p, r),
            update(c, ac, bc, p, q))


def transport(x, u, p, spectral):
    return sp.Matrix([[x, p - spectral - x*u], [1, -u]])


def adler_matrix(x, p, spectral):
    return sp.Matrix([[x, x*x + p - spectral], [1, x]])


def adler_pair(x, y, p, q):
    delta = (p-q)/(x+y)
    return sp.cancel(y-delta), sp.cancel(x+delta)


def reorder(values, parameters, order):
    values = list(values)
    for i, j in order:
        values[i], values[j] = adler_pair(values[i], values[j], parameters[i], parameters[j])
    return values


def verify():
    x, a, b, c, p, q, r, spectral = sp.symbols("x a b c p q r lambda")
    for update in (h1, cross_update):
        top = cube(update, x, a, b, c, p, q, r)
        assert all(sp.cancel(v-top[0]) == 0 for v in top[1:])
        assert x not in sp.factor(top[0]).free_symbols
    data = list(map(sp.Integer, (0, 1, 3, 6, 2, 5, 11)))
    print("H1 top:", cube(h1, *data))
    x0, a0, b0, c0, p0, q0, r0 = data
    broken = h1(a0, h1(x0, a0, b0, p0, q0), h1(x0, a0, c0, p0, r0), q0, r0+1)
    assert broken == sp.Rational(73, 3)
    print("One upper-face label changed:", broken)

    z = h1(x, a, b, p, q)
    left = transport(a, z, q, spectral)*transport(x, a, p, spectral)
    right = transport(b, z, p, spectral)*transport(x, b, q, spectral)
    assert (left-right).applyfunc(sp.cancel) == sp.zeros(2)
    assert sp.expand(transport(x, a, p, spectral).det()) == spectral-p

    # Complex field values on a planar rectangular face and its Darboux transform.
    i = sp.I
    rectangle = (sp.S.Zero, sp.Integer(2), 2+i, i)
    v = 1+i
    v1 = cross_update(0, 2, v, 4, 2)
    v2 = cross_update(0, i, v, -1, 2)
    v12 = cross_update(v, v1, v2, 4, -1)
    assert set(cube(cross_update, sp.S.Zero, sp.Integer(2), i, v, 4, -1, 2)) == {-1}
    assert cross_ratio(*rectangle) == cross_ratio(v, v1, v12, v2) == -4
    assert cross_ratio(0, 2, v1, v) == 2 and cross_ratio(0, i, v2, v) == -sp.S.Half
    print("Darboux face:", [sp.simplify(u) for u in (v, v1, v12, v2)])

    u, v = adler_pair(x, a, p, q)
    lhs = adler_matrix(u, p, spectral)*adler_matrix(v, q, spectral)
    rhs = adler_matrix(a, q, spectral)*adler_matrix(x, p, spectral)
    assert (lhs-rhs).applyfunc(sp.cancel) == sp.zeros(2)
    order = [(0, 1), (0, 2), (1, 2)]
    result = reorder([x, a, b], [p, q, r], order)
    reverse = reorder([x, a, b], [p, q, r], reversed(order))
    assert all(sp.cancel(s-t) == 0 for s, t in zip(result, reverse))
    print("Adler triple:", reorder(list(map(sp.Integer, [1,2,4])), [2,5,11], order))

    d = p-q
    h, k = sp.symbols("h k")
    step = (k, d/k-k-h)
    integral = (h+k)*(d-h*k)
    assert sp.cancel(integral.subs({h:step[0], k:step[1]}, simultaneous=True)-integral) == 0
    assert sp.Matrix(step).jacobian([h,k]).det() == 1
    monodromy = (transport(x+h+k, x, q, spectral)*transport(x+h, x+h+k, p, spectral)
                 * transport(x, x+h, p, spectral))
    assert sp.expand(monodromy.trace()-integral) == 0
    print("Staircase trace:", integral, "determinant:", (spectral-p)**2*(spectral-q))


if __name__ == "__main__":
    verify()
