"""Exact examples for H1, homogeneous H3 and the Hirota–Miwa equation.

Run with Python 3 and SymPy.
"""
import sympy as sp


def h1_cube():
    z, e = sp.symbols("z epsilon")

    def update(x, a, b, p, q):
        return sp.cancel(sp.solve((x-z)*(a-b) - (p-q), z)[0])

    a, b, c = 1, 2, 3+e
    ab = update(0, a, b, 1, 2)
    ac = update(0, a, c, 1, 3)
    bc = update(0, b, c, 2, 3)
    upper = (update(a, ab, ac, 2, 3), update(b, ab, bc, 1, 3),
             update(c, ac, bc, 1, 2))
    assert upper == (-2/e,)*3
    assert [v.subs(e, 1) for v in (ab, ac, bc)] == [-1, -sp.Rational(2, 3), -sp.Rational(1, 2)]
    assert update(a, ab, ac.subs(e, 1), 2, 4) == -5
    low = [v.subs(e, 0) for v in (ab, ac, bc)]
    assert low == [-1, -1, -1]
    for x, u, v, p, q in ((1, low[0], low[1], 2, 3), (2, low[0], low[2], 1, 3),
                          (3, low[1], low[2], 1, 2)):
        assert (x-z)*(u-v) != p-q
    print("H1: three predictions -2/epsilon; changed upper face gives -5 at epsilon=1.")


def h3_face():
    x, a, b, z, c, e, p, q, scale = sp.symbols("x a b z c epsilon p q scale")
    face = p*(x*a+b*z) - q*(x*b+a*z)
    scaled = face.subs({v: scale*v for v in (x, a, b, z)}, simultaneous=True)
    assert sp.expand(scaled - scale**2*face) == 0
    assert sp.expand(face.subs(q, p) - p*(a-b)*(x-z)) == 0
    fixed = face.subs({p: 2, q: 3})
    assert fixed.subs({x: 1, a: 1, b: 2, z: 4}) == 0
    assert fixed.subs({x: 2, a: 2, b: 3, z: 5}) == -10
    assert fixed.subs({a: 2, b: 3}) == -5*x
    regular = sp.solve(fixed.subs({a: 2, b: 3+e}), z)[0]
    assert sp.simplify(regular - x*(5+3*e)/(2*e)) == 0
    assert sp.limit(regular.subs(x, 1), e, 0, dir="+") == sp.oo
    assert sp.limit(regular.subs(x, c*e), e, 0) == 5*c/2
    print("H3: paths x=0 and x=2 epsilon select finite limits 0 and 5.")


def hirota_miwa():
    a, r, s, t = sp.symbols("A R S T")

    def products(field, n=0, m=0, l=0):
        return (field(n+1, m+1, l)*field(n, m, l+1),
                field(n, m+1, l+1)*field(n+1, m, l),
                field(n+1, m, l+1)*field(n, m+1, l))

    def residual(triple):
        first, second, third = triple
        return -first - 2*second + 3*third

    def field(n, m, l):
        return 1 + a*r**n*s**m*t**l

    linear = (3*r-2*s-1)*t - (r*s+2*r-3*s)
    assert sp.expand(residual(products(field)) - a*linear) == 0
    assert linear.subs({r: 3, s: 4}) == -6
    assert linear.subs({r: 1, s: 1}) == 0
    assert linear.subs({r: 3, s: 2, t: 1}) == -2

    def wave(n, m, l):
        return 1 + sp.Integer(3)**n * sp.Integer(2)**m * sp.Rational(3, 2)**l

    def gauged(n, m, l):
        return sp.Integer(2)**n * sp.Integer(3)**m * sp.Integer(5)**l * wave(n, m, l)

    assert products(wave) == (sp.Rational(35, 2), 16, sp.Rational(33, 2))
    for n, m, l in ((0, 0, 0), (-2, 3, -1), (3, -1, 2)):
        assert residual(products(wave, n, m, l)) == residual(products(gauged, n, m, l)) == 0
    for sample in (wave, gauged):
        ratio = sample(0, 0, 0)*sample(1, 1, 0)/(sample(1, 0, 0)*sample(0, 1, 0))
        assert ratio == sp.Rational(7, 6)
    print("Hirota–Miwa: T=3/2, free and impossible cases, and gauged face ratio 7/6.")


if __name__ == "__main__":
    h1_cube()
    h3_face()
    hirota_miwa()
