"""Numerical checks for 'The Moving Root'.
K is R, C, H or O (dimension k); V = R + R + K with eta(X,Y) = x0 y0 - <X',Y'>; roots are pairs in K^2.
G(X) is the action of Paper V, Gb(X) = G(X^flat) its mirror; D = sum G(E_mu) d_mu, Dbar = sum Gb(E_mu) d_mu."""
import numpy as np
rng = np.random.default_rng(8)
ok = lambda a, b, tol=1e-7: np.allclose(a, b, rtol=0, atol=tol)
def conj(x):
    c = -x.copy(); c[0] = x[0]; return c
def mul(x, y):                                          # Cayley-Dickson product
    n = len(x)
    if n == 1: return x * y
    h = n // 2; a, b, c, d = x[:h], x[h:], y[:h], y[h:]
    return np.concatenate([mul(a, c) - mul(conj(d), b), mul(d, a) + mul(b, conj(c))])
def expm(M):
    s = 10; A = M / 2 ** s; E = np.eye(len(M)); t = np.eye(len(M))
    for j in range(1, 20): t = t @ A / j; E = E + t
    for _ in range(s): E = E @ E
    return E

def run(k):
    n = k + 2; I = np.eye(2 * k); E = np.eye(n); R = {}
    def act(X, p):
        x, y = p[:k], p[k:]; t, a, u = X[0], X[1], X[2:]
        return np.concatenate([(t + a) * x + mul(u, y), mul(conj(u), x) + (t - a) * y])
    G = lambda X: np.column_stack([act(X, b) for b in np.eye(2 * k)])
    flat = lambda X: np.concatenate([[-X[0]], X[1:]]); sig = lambda X: np.concatenate([[X[0]], -X[1:]])
    Gb = lambda X: G(flat(X)); eta = lambda X, Y: X[0] * Y[0] - X[1:] @ Y[1:]
    cut = lambda p: np.concatenate([[p[:k] @ p[:k] + p[k:] @ p[k:], p[:k] @ p[:k] - p[k:] @ p[k:]], 2 * mul(p[:k], conj(p[k:]))])
    GE = [G(E[m]) for m in range(n)]; GbE = [Gb(E[m]) for m in range(n)]
    X0 = rng.normal(size=n); h = 1e-5
    D = lambda F, X: sum(GE[m] @ (F(X + h * E[m]) - F(X - h * E[m])) / (2 * h) for m in range(n))      # by central differences
    Dbar = lambda F, X: sum(GbE[m] @ (F(X + h * E[m]) - F(X - h * E[m])) / (2 * h) for m in range(n))
    small = lambda v, scale=1.0: np.linalg.norm(v) < 1e-5 * (1 + scale)
    grad = lambda F, X: sum(np.linalg.norm((F(X + h * E[m]) - F(X - h * E[m])) / (2 * h)) for m in range(n))   # size of the derivatives
    rel = lambda v, F, X: np.linalg.norm(v) < 1e-6 * (1 + grad(F, X))                 # small relative to the field's own derivatives
    close = lambda u, w: np.linalg.norm(u - w) < 1e-7 * (1 + np.linalg.norm(w))

    # ---- only along its own cut
    Kt = rng.normal(size=n)
    p0 = rng.normal(size=2 * k); K = cut(p0); sv = np.linalg.svd(G(sig(K)))
    kern = sv[2][np.sum(sv[1] > 1e-9):]                                 # basis of the kernel of G(sigma K)
    R['plane wave: generic K admits no root; a cut K admits exactly the k-dimensional half of that cut'] = (
        np.linalg.matrix_rank(G(sig(Kt)), tol=1e-9) == 2 * k and len(kern) == k
        and all(ok(cut(a) / cut(a)[0], K / K[0]) for a in kern) and ok(G(K) @ kern.T, 2 * K[0] * kern.T)
        and ok(G(sig(K)) @ p0, 0 * p0))
    g = lambda s: np.exp(-s * s) + 0.3 * np.sin(2 * s)
    good = small(D(lambda X: g(eta(K, X)) * p0, X0), np.linalg.norm(K) * np.linalg.norm(p0))
    bad = np.linalg.norm(D(lambda X: g(eta(K, X)) * (p0 + rng.normal(size=2 * k)), X0)) > 1e-3          # a root of another cut, on this cut
    K2 = cut(rng.normal(size=2 * k)); pts5 = [rng.normal(size=n) for _ in range(5)]
    worst = lambda F: max(np.linalg.norm(D(F, Xp)) for Xp in pts5)                                    # largest residual over five places
    bad &= worst(lambda X: g(eta(K2, X)) * p0) > 1e-2                                                  # this root, on another cut
    bad &= worst(lambda X: g(eta(K + 0.2 * E[0], X)) * p0) > 1e-2                                      # this root, off the cone
    good &= small(D(lambda X: g(eta(-K, X)) * p0, X0), np.linalg.norm(K) * np.linalg.norm(p0))          # K = -kappa: profile reversed
    R['a root is carried by its own cut (with either sign of K) and by no other'] = good and bad

    # ---- the cut is the flow
    modes = [(cut(q), q, rng.normal(), rng.normal()) for q in rng.normal(size=(4, 2 * k))]
    psi = lambda X: sum(np.sin(w * eta(Kj, X) + ph) * q for Kj, q, w, ph in modes)
    dpsi = lambda X, m: sum(w * np.cos(w * eta(Kj, X) + ph) * eta(Kj, E[m]) * q for Kj, q, w, ph in modes)
    gen = lambda X: np.sin(X @ Kt) * p0 + np.cos(1.3 * X[1]) * modes[0][1]            # an arbitrary field, not a solution
    dgen = lambda X, m: (gen(X + h * E[m]) - gen(X - h * E[m])) / (2 * h)
    cX = cut(psi(X0))
    R['components of the cut are <psi, G(E_mu) psi>; every G(X) is symmetric'] = (
        ok(cX, [psi(X0) @ GE[m] @ psi(X0) for m in range(n)]) and ok(G(Kt), G(Kt).T))
    divgen = sum((cut(gen(X0 + h * E[m]))[m] - cut(gen(X0 - h * E[m]))[m]) / (2 * h) for m in range(n))
    R['divergence of the cut field = 2 <psi, D psi> for any field'] = abs(divgen - 2 * gen(X0) @ sum(GE[m] @ dgen(X0, m) for m in range(n))) < 1e-5
    divsol = sum((cut(psi(X0 + h * E[m]))[m] - cut(psi(X0 - h * E[m]))[m]) / (2 * h) for m in range(n))   # divergence of the cut field itself
    R['for a solution the cut field is conserved, and the flow has the size of the density'] = (
        ok(sum(GE[m] @ dpsi(X0, m) for m in range(n)), 0 * p0) and abs(divsol) < 1e-4 * (1 + cX[0]) and abs(divgen) > 1e-3
        and ok(np.linalg.norm(cX[1:]), cX[0]))

    # ---- the two halves are the two movers
    pv = rng.normal(size=k + 1); pv /= np.linalg.norm(pv); pE = np.concatenate([[0], pv]); Gp = G(pE)
    Pp, Pm = (I + Gp) / 2, (I - Gp) / 2
    R['G(p)^2 = 1 for an odd unit p; (1 +- G(p))/2 are the halves of the cut e+p and of its opposite'] = (
        ok(Gp @ Gp, I) and ok(Pp @ Pp, Pp) and ok(Pp @ Pm, 0 * I) and np.linalg.matrix_rank(Pp, tol=1e-9) == k
        and ok(Pp, G(E[0] + pE) / 2) and ok(Pm, G(E[0] - pE) / 2))
    a_ = rng.normal(size=2 * k); a3 = rng.normal(size=2 * k)
    fprof = lambda s_: g(s_) * a_ + np.sin(1.7 * s_) * a3 + np.cos(0.4 * s_) * p0        # a root-valued profile
    sol = lambda X: Pp @ fprof(pE @ X - X[0]) + Pm @ fprof(pE @ X + X[0])
    Xs = X0.copy(); Xs[0] = 0
    R['any root-valued profile splits into a half moving along p and a half moving along -p'] = (
        small(D(sol, X0), 10) and ok(sol(Xs), fprof(pE @ Xs)))
    R['a constant root has a current as large as its density, so its flow is not at rest'] = ok(np.linalg.norm(cut(p0)[1:]), p0 @ p0) and p0 @ p0 > 1e-6
    R['evolution form: d_0 psi = - sum_i G(E_i) d_i psi, since G(e) = 1'] = ok(GE[0], I) and ok(GbE[0], -I) and all(ok(GbE[i], GE[i]) for i in range(1, n))

    # ---- rest: the pair of a root and a mirror root
    m = 0.8
    def pair(Kc, a, b):
        ph = lambda X: eta(Kc, X)
        P = lambda X: np.cos(ph(X)) * a + np.sin(ph(X)) * b
        C = lambda X: G(sig(Kc)) @ (-np.sin(ph(X)) * a + np.cos(ph(X)) * b) / m      # chi = D psi / m
        return P, C
    solves = lambda P, C, X: small(D(P, X) - m * C(X), 5) and small(Dbar(C, X) - m * P(X), 5)
    Kin = rng.normal(size=n); Kin[0] = abs(Kin[0]) + 2 * np.linalg.norm(Kin[1:]); Kin *= m / np.sqrt(eta(Kin, Kin))
    a_, b_ = rng.normal(size=2 * k), rng.normal(size=2 * k)
    P, C = pair(Kin, a_, b_)
    Pn, Cn = pair(cut(rng.normal(size=2 * k)), a_, b_); Po, Co = pair(1.3 * Kin, a_, b_)
    R['a travelling pair solves exactly when the even square of its motion is m^2'] = (
        solves(P, C, X0) and not solves(Pn, Cn, X0) and not solves(Po, Co, X0))
    box = lambda F, X, hh=1e-3: ((F(X + hh * E[0]) - 2 * F(X) + F(X - hh * E[0]))
                                 - sum(F(X + hh * E[i]) - 2 * F(X) + F(X - hh * E[i]) for i in range(1, n))) / hh ** 2
    P0_, C0_ = pair(0 * E[0], 0 * a_, b_)
    R['the degenerate case K = 0, a = 0 solves for every m (hence K non-zero is required)'] = solves(P0_, C0_, X0) and ok(P0_(X0), 0 * a_)
    R['each of the pair satisfies (box + m^2) = 0'] = (
        np.linalg.norm(box(P, X0) + m * m * P(X0)) < 1e-4 * 20 and np.linalg.norm(box(C, X0) + m * m * C(X0)) < 1e-4 * 20)
    J = cut(P(X0)) + sig(cut(C(X0))); kap, kapm = cut(P(X0)), cut(C(X0))
    Jf = lambda X: cut(P(X)) + sig(cut(C(X)))
    divJ = sum((Jf(X0 + h * E[mu])[mu] - Jf(X0 - h * E[mu])[mu]) / (2 * h) for mu in range(n))
    ghost = np.linalg.norm(kap[1:]) * np.linalg.norm(kapm[1:]) + kap[1:] @ kapm[1:]          # gamma(v, -w)
    R['flow of a pair = cut of the root + opposite cut of the mirror root: conserved, even square = twice a ghost term'] = (
        abs(divJ) < 1e-4 and ok(eta(J, J), 2 * ghost) and eta(J, J) > -1e-9 and J[0] > 0 and ok(J[0], P(X0) @ P(X0) + C(X0) @ C(X0)))
    q1p = Pp @ rng.normal(size=2 * k); q2 = Pm @ rng.normal(size=2 * k)                  # cuts along +p and along -p
    Jc = cut(q1p) + sig(cut(q2)); vv, ww = cut(q1p)[1:], cut(q2)[1:]
    Jn = cut(q1p) + sig(cut(Pp @ rng.normal(size=2 * k)))
    R['the flow is on the cone exactly when the ghost term vanishes'] = (
        ok(np.linalg.norm(vv) * np.linalg.norm(ww) + vv @ ww, 0) and ok(eta(Jc, Jc), 0) and eta(Jn, Jn) > 1e-6)
    Pr, Cr = pair(m * E[0], a_, b_); t1 = np.array([0.6] + [0.0] * (n - 1)); t2 = t1 + (2 * np.pi / m) * E[0]
    R['at rest the pair turns into each other at rate m and returns after 2 pi / m'] = (
        solves(Pr, Cr, X0) and ok(Cr(t1), -np.sin(m * .6) * a_ + np.cos(m * .6) * b_) and ok(Pr(t2), Pr(t1)) and ok(Cr(t2), Cr(t1)))
    beta_ = lambda p, q: (cut(p + q) - cut(p) - cut(q)) / 2             # the pairing
    A_ = lambda t: cut(Pr(t * E[0])); B_ = lambda t: cut(Cr(t * E[0])); C_ = lambda t: beta_(Pr(t * E[0]), Cr(t * E[0]))
    dt = 1e-5; dd = lambda F_, t: (F_(t + dt) - F_(t - dt)) / (2 * dt); t0_ = 0.37
    Dd = lambda t: A_(t) - B_(t); dt2 = 1e-3
    R['closed record of a pair at rest: A\' = 2mC, B\' = -2mC, C\' = m(B - A); A - B oscillates at 2m'] = (
        ok(dd(A_, t0_), 2 * m * C_(t0_), 1e-6) and ok(dd(B_, t0_), -2 * m * C_(t0_), 1e-6) and ok(dd(C_, t0_), m * (B_(t0_) - A_(t0_)), 1e-6)
        and ok((Dd(t0_ + dt2) - 2 * Dd(t0_) + Dd(t0_ - dt2)) / dt2 ** 2, -4 * m * m * Dd(t0_), 1e-4)
        and ok((cut(Pr(t0_ * E[0])) + sig(cut(Cr(t0_ * E[0]))))[1:], Dd(t0_)[1:])
        and ok(A_(t0_ + np.pi / m), A_(t0_)) and ok(Pr((t0_ + np.pi / m) * E[0]), -Pr(t0_ * E[0])))
    tau_ = 0.71; c2, s2 = np.cos(2 * m * tau_), np.sin(2 * m * tau_)       # exact propagator of the closed system
    Dn = c2 * (A_(t0_) - B_(t0_)) + 2 * s2 * C_(t0_); Cn = c2 * C_(t0_) - 0.5 * s2 * (A_(t0_) - B_(t0_)); Sn = A_(t0_) + B_(t0_)
    R['the record (A, B, C) at a later time is a function of the record now'] = (
        ok(A_(t0_ + tau_), (Sn + Dn) / 2) and ok(B_(t0_ + tau_), (Sn - Dn) / 2) and ok(C_(t0_ + tau_), Cn))
    Pneg, Cneg = pair(m * E[0], -a_, -b_)                                # a different pair with the same record
    R['congruence: two different pairs with one record keep one record'] = (
        not ok(Pneg(t0_ * E[0]), Pr(t0_ * E[0])) and ok(cut(Pneg(t0_ * E[0])), A_(t0_))
        and ok(cut(Pneg((t0_ + tau_) * E[0])), A_(t0_ + tau_)) and ok(cut(Cneg((t0_ + tau_) * E[0])), B_(t0_ + tau_))
        and ok(beta_(Pneg((t0_ + tau_) * E[0]), Cneg((t0_ + tau_) * E[0])), C_(t0_ + tau_)))
    Pq_, Cq_ = pair(m * E[0], b_, -a_)                                   # the same pair a quarter loop later
    tot = lambda t: (cut(Pr(t * E[0])) + sig(cut(Cr(t * E[0]))) + cut(Pq_(t * E[0])) + sig(cut(Cq_(t * E[0]))))
    R['two pairs a quarter loop apart have currents that cancel, in every number system'] = all(
        ok(tot(t)[1:], 0 * E[0][1:]) and ok(tot(t)[0], 2 * (a_ @ a_ + b_ @ b_)) for t in (0.0, 0.4, 1.7))
    R['Encapsulation Domain: c(psi) + c(chi) is invariant for a pair at rest'] = ok(A_(t0_) + B_(t0_), A_(1.9) + B_(1.9)) and ok(A_(0.0) + B_(0.0), cut(a_) + cut(b_))
    if True:                                                            # the cuts alone do not determine their future (real entries, any K)
        ra = np.zeros(2 * k); ra[0] = 1; rb = np.zeros(2 * k); rb[k] = 1
        Pa, Ca = pair(m * E[0], ra, rb); Pb, Cb = pair(m * E[0], ra, -rb)
        tq = (np.pi / 4) / m
        pad = lambda v3: np.concatenate([v3, np.zeros(n - 3)])
        R['two pairs with the same cuts at one time and different cuts later'] = (
            ok(cut(Pa(0 * E[0])), cut(Pb(0 * E[0]))) and ok(cut(Ca(0 * E[0])), cut(Cb(0 * E[0]))) and ok(cut(Ca(0 * E[0])), pad([1, -1, 0]))
            and ok(cut(Pa(tq * E[0])), pad([1, 0, 1])) and ok(cut(Pb(tq * E[0])), pad([1, 0, -1])))
        tau = 0.53; oddA = lambda P_: cut(P_(tau * E[0]))[1:]
        R['after a time tau the two first cuts have odd parts (cos 2m tau, +- sin 2m tau)'] = (
            ok(oddA(Pa)[:2], [np.cos(2 * m * tau), np.sin(2 * m * tau)]) and ok(oddA(Pb)[:2], [np.cos(2 * m * tau), -np.sin(2 * m * tau)]))
    if k >= 2:                                                          # steady rest: the root runs the sphere of roots of its own cut
        x_, y_ = a_[:k], a_[k:]; u_ = 2 * mul(x_, conj(y_))
        def root_of_same_cut(xp):                                       # Paper IV, Theorem 4.3: y is fixed by x
            return np.concatenate([xp, conj(mul(conj(xp), u_) / (2 * xp @ xp))])
        hh_ = np.zeros(k); hh_[1:] = rng.normal(size=k - 1); hh_ /= np.linalg.norm(hh_)       # a unit with square -1
        b_s = root_of_same_cut(mul(x_, hh_)); Ps, Cs = pair(m * E[0], a_, b_s)
        beta = lambda p, q: (cut(p + q) - cut(p) - cut(q)) / 2
        steady = lambda P_, C_: max(np.linalg.norm((cut(P_(t * E[0])) + sig(cut(C_(t * E[0]))))[1:]) for t in np.linspace(0, 4, 9)) < 1e-9
        xq = rng.normal(size=k); xq *= np.linalg.norm(x_) / np.linalg.norm(xq); b_q = root_of_same_cut(xq)   # same cut, not perpendicular
        Pq, Cq = pair(m * E[0], a_, b_q)
        R['steady rest: b a root of the cut of a, perpendicular to a; then psi stays a root of that cut and the flow is 2|a|^2 e'] = (
            ok(cut(b_s), cut(a_)) and ok(a_ @ b_s, 0) and ok(beta(a_, b_s), 0 * E[0]) and solves(Ps, Cs, X0) and steady(Ps, Cs)
            and all(ok(cut(Ps(t * E[0])), cut(a_)) for t in (0.3, 1.1, 2.9))
            and ok(cut(Ps(X0)) + sig(cut(Cs(X0))), 2 * (a_ @ a_) * E[0])
            and ok(cut(b_q), cut(a_)) and abs(a_ @ b_q) > 1e-3 and not steady(Pq, Cq) and not steady(Pr, Cr)
            and all(ok(cut(Cs(t * E[0])), cut(a_)) and ok(Ps(t * E[0]) @ Cs(t * E[0]), 0) for t in (0.3, 1.1, 2.9)))
    if k == 1:                                                          # for R the flow of a pair at rest always oscillates
        worst = 1e9
        for _ in range(300):
            a1, b1 = rng.normal(size=2), rng.normal(size=2); P1, C1 = pair(m * E[0], a1, b1)
            osc = max(np.linalg.norm((cut(P1(t * E[0])) + sig(cut(C1(t * E[0]))))[1:]) for t in np.linspace(0, 4, 9))
            worst = min(worst, osc / (a1 @ a1 + b1 @ b1))
        R['for R no pair at rest has a steady flow'] = worst > 1e-3

    # ---- changes of view
    Xa, Ya = rng.normal(size=n), rng.normal(size=n); th = 0.4
    S = 0.25 * (Gb(Xa) @ G(Ya) - Gb(Ya) @ G(Xa)); Sp = 0.25 * (G(Xa) @ Gb(Ya) - G(Ya) @ Gb(Xa))
    Lt = np.column_stack([eta(Ya, b) * Xa - eta(Xa, b) * Ya for b in E]); Lam = Lt.T
    Rm, Rp, L = expm(th * S), expm(th * Sp), expm(th * Lam); Li = np.linalg.inv(L)
    Z = rng.normal(size=n)
    R['G(Z) R = R\' G(L^T Z) and Gb(Z) R\' = R Gb(L^T Z); R\' is the inverse transpose of R'] = (
        ok(G(Z) @ Rm, Rp @ G(L.T @ Z)) and ok(Gb(Z) @ Rp, Rm @ Gb(L.T @ Z)) and ok(Rp, np.linalg.inv(Rm).T))
    F = lambda X: Rm @ gen(Li @ X)
    chi0 = rng.normal(size=2 * k)
    R['the opposite cut of a mirror root turns with the view as a cut does'] = close(sig(cut(Rp @ chi0)), L @ sig(cut(chi0))) and close(cut(Rm @ chi0), L @ cut(chi0))
    R['D (R psi o L^-1) = R\' (D psi) o L^-1 for any field'] = rel(D(F, X0) - Rp @ D(gen, Li @ X0), F, X0)
    psi2 = lambda X: Rm @ psi(Li @ X)
    R['changes of view carry solutions to solutions, and the cut field turns with the view'] = (
        rel(D(psi2, X0), psi2, X0) and close(cut(psi2(X0)), L @ cut(psi(Li @ X0))))
    P2 = lambda X: Rm @ Pr(Li @ X); C2 = lambda X: Rp @ Cr(Li @ X)
    o = L @ E[0]
    R['a pair at rest, seen from another view, is a pair travelling along the observer o = L e'] = (
        rel(D(P2, X0) - m * C2(X0), P2, X0) and rel(Dbar(C2, X0) - m * P2(X0), C2, X0) and close(eta(o, o), 1.0)
        and close(P2(X0), np.cos(m * eta(o, X0)) * (Rm @ a_) + np.sin(m * eta(o, X0)) * (Rm @ b_)))
    return R

results = {}
for k in (1, 2, 4, 8):
    for name, val in run(k).items(): results.setdefault(name, []).append(val)

# ---- translations from the tangent product (dimension 5)
d = 5; e = np.eye(d)[0]
def prod(c):
    def p(x, y):
        a, b = x @ e, y @ e; xp, yp = x - a * e, y - b * e
        return (a * b + c * (xp @ yp)) * e + a * yp + b * xp
    return p
x, y, z = (rng.normal(size=d) for _ in range(3)); odd = lambda v: v - (v @ e) * e
good = True
for c in (-1, 0, 1, 0.4):
    p = prod(c); xo, yo, zo = odd(x), odd(y), odd(z)
    good &= ok(p(p(x, y), z) - p(x, p(y, z)), c * ((xo @ yo) * zo - (yo @ zo) * xo))
results['associator of the product with square c is c(<x\',y\'> z\' - <y\',z\'> x\'): zero exactly for the tangent product'] = [
    good and ok(prod(0)(prod(0)(x, y), z), prod(0)(x, prod(0)(y, z))) and not ok(prod(1)(prod(1)(x, y), z), prod(1)(x, prod(1)(y, z)))]
v, w = odd(rng.normal(size=d)), odd(rng.normal(size=d)); pt = e + w
results['multiplying by e + v in the tangent product translates the sheet a = 1 by v; translations add'] = [
    ok(prod(0)(pt, e + v), pt + v) and ok(prod(0)(e + v, e + w), e + v + w) and ok(prod(0)(x, e + v), x + (x @ e) * v)]
def prod2(c):
    return lambda x_, y_: np.array([x_[0] * y_[0] + c * x_[1] * y_[1], x_[0] * y_[1] + y_[0] * x_[1]])
x2, y2, z2 = (rng.normal(size=2) for _ in range(3))
results['in dimension 2 every product of the family is associative'] = [all(
    ok(prod2(c)(prod2(c)(x2, y2), z2), prod2(c)(x2, prod2(c)(y2, z2))) for c in (-1, 0, 1, 0.4))]
b1, b2 = np.eye(d)[1], np.eye(d)[2]; good = True
for c in (-1, 0, 1, 0.4, -2.5):
    M = lambda b: np.column_stack([prod(c)(col, b) for col in np.eye(d)])                  # multiply by the odd unit b
    form = np.diag([1.0] + [-c] * (d - 1)); th = 0.7
    good &= ok(M(b1).T @ form + form @ M(b1), 0 * form) and ok(M(b1) @ M(b2) - M(b2) @ M(b1), c * (np.outer(b1, b2) - np.outer(b2, b1)))
    r_ = np.sqrt(abs(c))
    out = (e + th * b1 if c == 0 else (np.cosh(r_ * th) * e + np.sinh(r_ * th) / r_ * b1 if c > 0 else np.cos(r_ * th) * e + np.sin(r_ * th) / r_ * b1))
    good &= ok(expm(th * M(b1)) @ e, out)
    if c == 0: good &= ok(expm(th * M(b1)) @ pt, pt + th * b1) and ok(M(b1) @ M(b1), 0 * form)
results['multiplication by an odd unit generates turns (c<0), boosts (c>0) and translations (c=0); brackets are c times a turn'] = [good]

for name, vals in results.items(): print(('PASS ' if all(vals) else 'FAIL ') + name + ('' if len(vals) == 1 else f'  [{len(vals)} number systems]'))
print('all checks pass' if all(all(v) for v in results.values()) else 'SOME CHECKS FAIL')
