"""Independent exact arithmetic checks for the exterior kernel-feedback candidate. These finite checks are an implementation audit; the accompanying prose review contains the universal arguments. SymPy performs all calculations over Q. """ from itertools import combinations from random import Random import json import sympy as s rng = Random(202610071) def dot(x, y): return (x.T * y)[0] def norm2(x): return dot(x, x) def wedge(x, a): return x * a.T - a * x.T def matrix_columns(cols, d): return s.Matrix.hstack(*cols) if cols else s.zeros(d, 0) def split(A, x): d = len(x) K = matrix_columns(A.nullspace(), d) P = K * (K.T * K).inv() * K.T if K.cols else s.zeros(d) w = P * x u = x - w h = (A + P).inv() * u return K, P, u, w, h def pf(A): if A.rows == 0: return s.Integer(1) if A.rows == 2: return A[0, 1] out = s.Integer(0) for j in range(1, A.rows): ii = [i for i in range(1, A.rows) if i != j] out += (-1) ** (j + 1) * A[0, j] * pf(A.extract(ii, ii)) return out def one_step(A, x, a): d = len(x) assert A + A.T == s.zeros(d) assert A * a == s.zeros(d, 1) K, P, u, w, h = split(A, x) B, y = A + wedge(x, a), x + a L, Q, v, z, hh = split(B, y) independent = matrix_columns([w, a], d).rank() == 2 if independent: pred = matrix_columns(A.col_join(w.T).col_join(a.T).nullspace(), d) expected_rho = norm2(u) + norm2(w + a) assert B.rank() == A.rank() + 2 assert z == s.zeros(d, 1) else: pred = K - h * (a.T * K) expected_rho = norm2(u) + norm2(h) * dot(a, w + a) ** 2 / (1 + norm2(h) * norm2(a)) assert B.rank() == A.rank() assert B * pred == s.zeros(d, pred.cols) assert pred.rank() == L.cols assert norm2(v) == s.cancel(expected_rho) assert norm2(v) >= norm2(u) for k in range(1, d // 2 + 1): old = [pf(A.extract(ii, ii)) for ii in combinations(range(d), 2 * k)] new = [pf(B.extract(ii, ii)) for ii in combinations(range(d), 2 * k)] cross = sum(p * (q - p) for p, q in zip(old, new)) assert cross == 0 assert sum(q * q for q in new) == sum(p * p for p in old) + sum((q - p) ** 2 for p, q in zip(old, new)) C, t = A + wedge(x, -w), u KK, PP, uu, ww, hhh = split(C, t) assert ww == s.zeros(d, 1) assert norm2(uu) == norm2(u) return B, y, independent def random_state(d, rank): while True: C = s.Matrix(d, rank, lambda i, j: rng.randint(-2, 2)) if C.rank() == rank: break J = s.zeros(rank) for i in range(0, rank, 2): J[i, i + 1], J[i + 1, i] = 1, -1 A = C * J * C.T x = s.Matrix([rng.randint(-2, 2) for i in range(d)]) return A, x results = {"rational_one_step_cases": 0, "independent_cases": 0, "dependent_cases": 0, "maxrank_paths": 0} for d in range(1, 8): for rank in range(0, d + 1, 2): for repeat in range(3): A, x = random_state(d, rank) K, P, u, w, h = split(A, x) controls = [s.zeros(d, 1), -w] if K.cols: controls.append(K * s.Matrix([rng.randint(-2, 2) for _ in range(K.cols)])) for a in controls: B, y, independent = one_step(A, x, a) results["rational_one_step_cases"] += 1 results["independent_cases" if independent else "dependent_cases"] += 1 # The construction also covers states with w forced to zero. for start_x in (x, u): AA, xx = A, start_x KK, PP, uu, ww, hh = split(AA, xx) m = KK.cols // 2 expected = 0 if m == 0 else 2 * m - (0 if ww == s.zeros(d, 1) else 1) count = 0 while AA.rank() < 2 * (d // 2): KK, PP, uu, ww, hh = split(AA, xx) if ww == s.zeros(d, 1): a = KK[:, 0] else: a = next(KK[:, i] for i in range(KK.cols) if matrix_columns([ww, KK[:, i]], d).rank() == 2) AA, xx, independent = one_step(AA, xx, a) count += 1 assert count == expected results["maxrank_paths"] += 1 # Separation family, including exact signs of lambda. for lam in map(s.Rational, [-5, -2, -1, 1, 2, 5]): x = s.Matrix([1, 1, 0]) A = s.Matrix([[0, lam, 0], [-lam, 0, 0], [0, 0, 0]]) a = s.Matrix([0, 0, 1]) B, y, independent = one_step(A, x, a) assert B * s.Matrix([1, -1, lam]) == s.zeros(3, 1) K, P, u, w, h = split(B, y) assert norm2(u) == 2 + 2 / (lam * lam + 2) results["separation_lambdas"] = 6 # Dormant block replacement: run the same legal exact words in both systems. for weight in (1, 2, 3): J = s.Matrix([[0, 1], [-1, 0]]) A = s.diag(J, weight * J, s.zeros(3)) B = s.diag(J, (weight + 5) * J, s.zeros(3)) x = s.Matrix([1, 2, 0, 0, 1, -1, 0]) y = x.copy() for step in range(5): K, P, u, w, h = split(A, x) L, Q, v, z, hh = split(B, y) assert P == Q assert x == y a = K * s.Matrix([rng.randint(-2, 2) for _ in range(K.cols)]) if K.cols else s.zeros(7, 1) assert a[2] == a[3] == 0 A, x = A + wedge(x, a), x + a B, y = B + wedge(y, a), y + a assert A[2:4, 2:4] == weight * J assert B[2:4, 2:4] == (weight + 5) * J results["dormant_replacement_paths"] = 3 # General three-dimensional accessibility: no projection preparation needed. bb, cc, zz, pp, qq, rr = s.symbols("b c z p q r", real=True) xx, kk = s.Matrix([bb, 0, zz]), s.Matrix([0, 0, cc]) for tt in (pp, qq, rr): xx, kk = s.expand(xx + tt * kk), s.expand(kk + tt * xx.cross(kk)) expected_endpoint = s.Matrix([ bb + bb * cc**2 * pp**2 * qq * rr + bb * cc * zz * pp * qq * rr, -bb * cc * (pp * qq + pp * rr + qq * rr), zz + cc * (pp + qq + rr - bb**2 * pp * qq * rr), ]) assert s.simplify(xx - expected_endpoint) == s.zeros(3, 1) jacobian_at_point = xx.jacobian((pp, qq, rr)).subs({pp: 1, qq: 1, rr: 2}) assert s.factor(jacobian_at_point.det()) == -2 * bb**2 * cc**4 * (bb**2 + 1) results["symbolic_general_accessibility_checks"] = 1 print(json.dumps(results, indent=2))