Datasets:
Formats:
json
Languages:
English
Size:
< 1K
Tags:
mathematics
signed-area
history-dependent-dynamics
reachability
linear-algebra
reproducible-research
Download code/verify_avenyra.py from PureOne/avenyra-area-feedback: direct link, hf CLI and curl.
- Browser
- Download file 18.1 kB
-
https://huggingface.co/datasets/PureOne/avenyra-area-feedback/resolve/main/code/verify_avenyra.py
- Command line
-
hf download hf://datasets/PureOne/avenyra-area-feedback/code/verify_avenyra.py
-
curl -L -o verify_avenyra.py https://huggingface.co/datasets/PureOne/avenyra-area-feedback/resolve/main/code/verify_avenyra.py
18.1 kB
| #!/usr/bin/env python3 | |
| """Exact rational and symbolic checks for Avenyra Area Feedback, version 1.0.0. | |
| Requires Python 3 and SymPy (tested with SymPy 1.14.0). | |
| Run from this release directory: python verify_avenyra.py | |
| This script checks a finite family of rational examples and verifies specified | |
| polynomial and differential identities symbolically. Universal reachability, | |
| topological, and limiting arguments are proved in the editable manuscript | |
| Avenyra_Area_Feedback_v1.0.0.tex. These checks do not replace those arguments | |
| and do not establish originality, physical realizability, or computational | |
| advantage. | |
| """ | |
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from itertools import combinations | |
| from math import comb | |
| from random import Random | |
| import sympy as sp | |
| class State: | |
| x: sp.Matrix | |
| A: sp.Matrix | |
| def d(self): | |
| return self.x.rows | |
| def unit(d, i): | |
| result = sp.zeros(d, 1) | |
| result[i] = 1 | |
| return result | |
| def sq(v): | |
| return sp.expand((v.T * v)[0]) if v.rows else sp.Integer(0) | |
| def wedge(x, a): | |
| return x * a.T - a * x.T | |
| def null_matrix(A): | |
| basis = A.nullspace() | |
| return sp.Matrix.hstack(*basis) if basis else sp.zeros(A.cols, 0) | |
| def projector(columns): | |
| if not columns.cols: | |
| return sp.zeros(columns.rows) | |
| return columns * (columns.T * columns).inv() * columns.T | |
| def split(state): | |
| K = null_matrix(state.A) | |
| P = projector(K) | |
| w = P * state.x | |
| u = state.x - w | |
| # A+P is invertible: it restricts to A on im(A) and to I on ker(A). | |
| h = (state.A + P).inv() * u if state.d else sp.zeros(0, 1) | |
| return K, P, u, w, h | |
| def rho2(state): | |
| return sq(split(state)[2]) | |
| def energy(A): | |
| return sp.expand(-sp.Rational(1, 2) * sp.trace(A * A)) | |
| def step(state, a): | |
| assert a.shape == state.x.shape | |
| assert state.A.T == -state.A | |
| assert state.A * a == sp.zeros(state.d, 1), "inadmissible input" | |
| return State(state.x + a, state.A + wedge(state.x, a)) | |
| def same_subspace(B, C): | |
| return B.rows == C.rows and B.rank() == C.rank() == B.row_join(C).rank() | |
| def exterior_wedge(left, right): | |
| """Coordinate exterior product; keys are increasing index tuples.""" | |
| out = {} | |
| for I, c in left.items(): | |
| for J, d in right.items(): | |
| if set(I).intersection(J): | |
| continue | |
| inversions = sum(i > j for i in I for j in J) | |
| index = tuple(sorted(I + J)) | |
| out[index] = out.get(index, 0) + (-1) ** inversions * c * d | |
| return {I: sp.expand(c) for I, c in out.items() if c != 0} | |
| def exterior_vector(x): | |
| return {(i,): x[i] for i in range(x.rows) if x[i] != 0} | |
| def exterior_bivector(A): | |
| return {(i, j): A[i, j] for i in range(A.rows) | |
| for j in range(i + 1, A.cols) if A[i, j] != 0} | |
| def exterior_norm2(form): | |
| return sp.expand(sum(c * c for c in form.values())) | |
| def divided_powers(A): | |
| alpha = exterior_bivector(A) | |
| powers = [{(): sp.Integer(1)}] | |
| for j in range(1, A.rows // 2 + 1): | |
| power = exterior_wedge(powers[-1], alpha) | |
| powers.append({I: sp.cancel(c / j) for I, c in power.items()}) | |
| return powers | |
| def check_exterior(before, a, after): | |
| t = sp.Symbol("t") | |
| p = sp.Poly(before.A.charpoly(t).as_expr(), t) | |
| q = sp.Poly(after.A.charpoly(t).as_expr(), t) | |
| old_powers = divided_powers(before.A) | |
| new_powers = divided_powers(after.A) | |
| xa = exterior_wedge(exterior_vector(before.x), exterior_vector(a)) | |
| for j, (old, new) in enumerate(zip(old_powers, new_powers)): | |
| old_norm = exterior_norm2(old) | |
| new_norm = exterior_norm2(new) | |
| assert p.coeff_monomial(t ** (before.d - 2 * j)) == old_norm | |
| assert q.coeff_monomial(t ** (before.d - 2 * j)) == new_norm | |
| if j: | |
| added = exterior_wedge(old_powers[j - 1], xa) | |
| assert new_norm == old_norm + exterior_norm2(added) | |
| def check_move(before, a, include_exterior=True): | |
| after = step(before, a) | |
| K, P, u, w, h = split(before) | |
| K_after = null_matrix(after.A) | |
| independent = sp.Matrix.hstack(w, a).rank() == 2 | |
| # Exact kernel formula, with no floating-point nullspace thresholds. | |
| gate_matrix = wedge(w, a) * K | |
| coefficient_kernel = null_matrix(gate_matrix) | |
| Z = K * coefficient_kernel | |
| predicted_kernel = (sp.eye(before.d) - h * a.T) * Z | |
| assert same_subspace(K_after, predicted_kernel) | |
| assert after.A.rank() == before.A.rank() + (2 if independent else 0) | |
| old_rho = sq(u) | |
| if independent: | |
| predicted_rho = old_rho + sq(w + a) | |
| assert K_after.T * after.x == sp.zeros(K_after.cols, 1) | |
| else: | |
| dot = (a.T * (w + a))[0] if before.d else sp.Integer(0) | |
| predicted_rho = old_rho + sq(h) * dot ** 2 / (1 + sq(h) * sq(a)) | |
| assert sp.cancel(rho2(after) - predicted_rho) == 0 | |
| assert rho2(after) >= old_rho | |
| # Attainment of the exact future floor by the projection-canceling move. | |
| attaining = step(before, -w) | |
| assert attaining.x == u | |
| assert rho2(attaining) == old_rho | |
| assert sq(attaining.x) == old_rho | |
| c = (before.x.T * a)[0] if before.d else sp.Integer(0) | |
| D = sq(before.x) * sq(a) - c * c | |
| assert energy(after.A) == energy(before.A) + D | |
| assert sq(after.A * after.x) == sq(before.A * before.x) + sq(after.x) * D | |
| # Algebraic inversion exists, but is legal exactly for a zero wedge. | |
| reverse_legal = after.A * (-a) == sp.zeros(before.d, 1) | |
| assert reverse_legal == (D == 0) | |
| if reverse_legal: | |
| recovered = step(after, -a) | |
| assert recovered.x == before.x and recovered.A == before.A | |
| if include_exterior: | |
| check_exterior(before, a, after) | |
| return after | |
| def check_orthogonal_launch(max_d=8): | |
| cases = 0 | |
| for d in range(max_d + 1): | |
| state = State(sp.zeros(d, 1), sp.zeros(d)) | |
| for n in range(d + 1): | |
| expected_A = sp.zeros(d) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| expected_A[i, j] = 1 | |
| expected_A[j, i] = -1 | |
| assert state.A == expected_A | |
| assert state.A.rank() == 2 * (n // 2) | |
| expected_rho = 0 if n == 0 else ( | |
| sp.Integer(n) if n % 2 == 0 else sp.Integer(n) - sp.Rational(1, n) | |
| ) | |
| assert rho2(state) == expected_rho | |
| powers = divided_powers(state.A) | |
| for j, power in enumerate(powers): | |
| assert exterior_norm2(power) == (comb(n, 2*j) if 2*j <= n else 0) | |
| t = sp.Symbol("t") | |
| reduced = sp.expand(((t + 1)**n + (t - 1)**n) / 2) | |
| assert sp.expand(state.A.charpoly(t).as_expr() - t**(d-n) * reduced) == 0 | |
| cases += 1 | |
| if n < d: | |
| state = check_move(state, unit(d, n), include_exterior=False) | |
| return cases | |
| def check_general_cases(): | |
| rng = Random(741103) | |
| count = 0 | |
| for d in range(7): | |
| for r in range(0, d + 1, 2): | |
| for repetition in range(3): | |
| A = sp.zeros(d) | |
| for j in range(r // 2): | |
| A[2*j, 2*j+1] = j + 1 | |
| A[2*j+1, 2*j] = -(j + 1) | |
| S = sp.eye(d) | |
| if d >= 2: | |
| for _ in range(3): | |
| i, j = rng.sample(range(d), 2) | |
| shear = sp.eye(d) | |
| shear[i, j] = rng.choice([-1, 1]) | |
| S = shear * S | |
| A = S * A * S.T | |
| x = sp.Matrix([rng.randint(-2, 2) for _ in range(d)]) if d else sp.zeros(0, 1) | |
| state = State(x, A) | |
| K = null_matrix(A) | |
| coefficients = sp.Matrix([rng.randint(-2, 2) for _ in range(K.cols)]) if K.cols else sp.zeros(0, 1) | |
| a = K * coefficients | |
| check_move(state, a) | |
| count += 1 | |
| return count | |
| def maximal_rank_history(d, initially_charged): | |
| x = unit(d, 0) if initially_charged and d else sp.zeros(d, 1) | |
| state = State(x, sp.zeros(d)) | |
| move_count = 0 | |
| while state.A.rank() < 2 * (d // 2): | |
| K, _, _, w, _ = split(state) | |
| basis = [K[:, j] for j in range(K.cols)] | |
| if w == sp.zeros(d, 1): | |
| a = basis[0] | |
| else: | |
| a = next(b for b in basis if sp.Matrix.hstack(w, b).rank() == 2) | |
| state = check_move(state, a, include_exterior=False) | |
| move_count += 1 | |
| m = d // 2 | |
| expected = 0 if m == 0 else 2*m - int(initially_charged) | |
| assert move_count == expected | |
| assert state.A.rank() == 2*m | |
| return move_count | |
| def check_examples(): | |
| # Two-dimensional halt. | |
| s = State(sp.zeros(2, 1), sp.zeros(2)) | |
| s = check_move(s, unit(2, 0)) | |
| s = check_move(s, unit(2, 1)) | |
| assert s.A.det() == 1 and rho2(s) == 2 | |
| assert null_matrix(s.A).cols == 0 | |
| # Same current x and gate, different next gate. | |
| results = [] | |
| for order in [(0, 1), (1, 0)]: | |
| s = State(sp.zeros(3, 1), sp.zeros(3)) | |
| for i in order: | |
| s = check_move(s, unit(3, i)) | |
| assert s.x == sp.Matrix([1, 1, 0]) | |
| assert same_subspace(null_matrix(s.A), unit(3, 2)) | |
| results.append(check_move(s, unit(3, 2))) | |
| assert same_subspace(null_matrix(results[0].A), sp.Matrix([1, -1, 1])) | |
| assert same_subspace(null_matrix(results[1].A), sp.Matrix([-1, 1, 1])) | |
| assert not same_subspace(null_matrix(results[0].A), null_matrix(results[1].A)) | |
| assert rho2(results[0]) == sp.Rational(8, 3) | |
| attaining = check_move(results[0], -sp.Rational(1, 3) * sp.Matrix([1, -1, 1])) | |
| assert attaining.x == sp.Matrix([sp.Rational(2, 3), sp.Rational(4, 3), sp.Rational(2, 3)]) | |
| assert sq(attaining.x) == sp.Rational(8, 3) | |
| # Different memory magnitudes, equal current x, gate, and distance floor. | |
| for lam in [sp.Integer(-1), sp.Integer(1), sp.Integer(2)]: | |
| s = State(sp.zeros(3, 1), sp.zeros(3)) | |
| s = check_move(s, lam * unit(3, 0)) | |
| s = check_move(s, (1-lam) * unit(3, 0) + unit(3, 1)) | |
| assert s.x == sp.Matrix([1, 1, 0]) and rho2(s) == 2 | |
| assert same_subspace(null_matrix(s.A), unit(3, 2)) | |
| s = check_move(s, unit(3, 2)) | |
| assert same_subspace(null_matrix(s.A), sp.Matrix([1, -1, lam])) | |
| assert rho2(s) == 2 + sp.Rational(2, lam**2 + 2) | |
| # Dormant blocks with different spectra produce the same future behavior. | |
| d = 5 | |
| x = unit(d, 0) + unit(d, 1) | |
| active = wedge(unit(d, 0), unit(d, 1)) | |
| dormant = wedge(unit(d, 3), unit(d, 4)) | |
| left = State(x, active + dormant) | |
| right = State(x, active + 7*dormant) | |
| for _ in range(4): | |
| KL, KR = null_matrix(left.A), null_matrix(right.A) | |
| assert same_subspace(KL, KR) | |
| a = KL[:, 0] | |
| assert a[3] == a[4] == 0 | |
| left = check_move(left, a, include_exterior=False) | |
| right = check_move(right, a, include_exterior=False) | |
| assert left.x == right.x | |
| assert right.A - left.A == 6*dormant | |
| assert same_subspace(null_matrix(left.A), null_matrix(right.A)) | |
| def check_failure_modes(): | |
| # Removing the admissibility condition allows complete cancellation. | |
| e1, e2 = unit(2, 0), unit(2, 1) | |
| A, x, a = wedge(e1, e2), e1, -e2 | |
| assert A*a != sp.zeros(2, 1) | |
| assert A + wedge(x, a) == sp.zeros(2) | |
| # Legal finite jumps need not remain legal under uniform subdivision. | |
| s = State(e1, sp.zeros(2)) | |
| halfway = step(s, e2/2) | |
| assert halfway.A * (e2/2) != sp.zeros(2, 1) | |
| # Indefinite metric counterexample: a legal update drops rank 2 to 0. | |
| G = sp.diag(-1, 1, 1) | |
| a, x = sp.Matrix([1, 1, 0]), sp.Matrix([0, 0, 1]) | |
| signed_wedge = x * a.T * G - a * x.T * G | |
| A = -signed_wedge | |
| assert A.T * G + G * A == sp.zeros(3) | |
| assert A.rank() == 2 and A*a == sp.zeros(3, 1) | |
| assert A + signed_wedge == sp.zeros(3) | |
| def check_spectral_reduction(): | |
| t = sp.Symbol("t") | |
| # Canonical rank-preserving example: two nonzero skew blocks and one kernel direction. | |
| for birth in [False, True]: | |
| d = 6 if birth else 5 | |
| A = sp.zeros(d) | |
| A[0, 1], A[1, 0] = 2, -2 | |
| A[2, 3], A[3, 2] = 3, -3 | |
| x = 3*unit(d, 0) + 4*unit(d, 2) | |
| a = 2*unit(d, 4) | |
| if birth: | |
| x += 5*unit(d, 5) | |
| diagonal = sp.diag(4, 9, 0) | |
| v = sp.Matrix([6, 8, 10]) | |
| zero_power = d-6 | |
| else: | |
| x += 5*unit(d, 4) | |
| diagonal = sp.diag(4, 9) | |
| v = sp.Matrix([6, 8]) | |
| zero_power = d-4 | |
| result = check_move(State(x, A), a) | |
| H = diagonal + v*v.T | |
| expected = t**zero_power * (t*t*sp.eye(H.rows) + H).det() | |
| assert sp.expand(result.A.charpoly(t).as_expr() - expected) == 0 | |
| def check_three_step_accessibility(): | |
| p, q, r = sp.symbols("p q r", real=True) | |
| x, k = sp.Matrix([1, 1, 0]), sp.Matrix([0, 0, 1]) | |
| for parameter in [p, q, r]: | |
| next_x = x + parameter*k | |
| next_k = k + parameter*x.cross(k) | |
| x, k = next_x.applyfunc(sp.expand), next_k.applyfunc(sp.expand) | |
| expected = sp.Matrix([ | |
| 1+p*q+p*r+q*r+p*p*q*r, | |
| 1-p*q-p*r-q*r+p*p*q*r, | |
| p+q+r-2*p*q*r, | |
| ]) | |
| assert x == expected | |
| jacobian = x.jacobian([p, q, r]) | |
| expected_det = 2*p*(q-r)*(2*p*p*q*r+p*p-p*q-p*r+2*q*r) | |
| assert sp.expand(jacobian.det()-expected_det) == 0 | |
| witness = {p: 1, q: 1, r: 2} | |
| assert x.subs(witness) == sp.Matrix([8, -2, 0]) | |
| assert jacobian.det().subs(witness) == -12 | |
| assert jacobian.subs(witness) == sp.Matrix([[7, 5, 3], [1, -1, -1], [-3, -3, -1]]) | |
| def check_general_three_dimensional_accessibility(): | |
| """Check the exact polynomial identities used by the 3D classification. | |
| The inverse function theorem and the invariant-line induction are arguments | |
| in the manuscript. The code verifies their algebraic premises, including | |
| the general canonical state with arbitrary axial displacement z. | |
| """ | |
| b, c = sp.symbols("b c", positive=True) | |
| z, p, q, r = sp.symbols("z p q r", real=True) | |
| x, k = sp.Matrix([b, 0, z]), sp.Matrix([0, 0, c]) | |
| states = [(x, k)] | |
| for parameter in [p, q, r]: | |
| next_x = x + parameter*k | |
| next_k = k + parameter*x.cross(k) | |
| x, k = next_x.applyfunc(sp.expand), next_k.applyfunc(sp.expand) | |
| states.append((x, k)) | |
| expected = sp.Matrix([ | |
| b + b*c*c*p*p*q*r + b*c*z*p*q*r, | |
| -b*c*(p*q+p*r+q*r), | |
| z + c*(p+q+r-b*b*p*q*r), | |
| ]).applyfunc(sp.expand) | |
| assert x == expected | |
| assert states[2][0] == sp.Matrix([b, -b*c*p*q, z+c*(p+q)]).applyfunc(sp.expand) | |
| assert all(position[0] == b for position, _ in states[:3]) | |
| jacobian = x.jacobian([p, q, r]) | |
| witness = {p: 1, q: 1, r: 2} | |
| expected_jacobian = sp.Matrix([ | |
| [2*b*c*(2*c+z), 2*b*c*(c+z), b*c*(c+z)], | |
| [-3*b*c, -3*b*c, -2*b*c], | |
| [c*(1-2*b*b), c*(1-2*b*b), c*(1-b*b)], | |
| ]) | |
| assert (jacobian.subs(witness)-expected_jacobian).applyfunc(sp.expand) == sp.zeros(3) | |
| assert sp.factor(jacobian.det().subs(witness)) == -2*b*b*c**4*(1+b*b) | |
| # Degenerate b=0: the position stays on the fixed kernel line. | |
| for position, axial in states: | |
| assert position.subs(b, 0)[0:2, 0] == sp.zeros(2, 1) | |
| assert axial.subs(b, 0) == sp.Matrix([0, 0, c]) | |
| def check_ode_boundary_identities(): | |
| """Symbolic differential checks used in the nonclosed-Reach example. | |
| This verifies conserved quantities and the local Taylor derivative. | |
| It does not numerically approximate the ODE, prove existence/convergence, | |
| or replace the finite-versus-limiting reachability proof. | |
| """ | |
| X = sp.Matrix(sp.symbols("x1:4", real=True)) | |
| K = sp.Matrix(sp.symbols("k1:4", real=True)) | |
| t = sp.Symbol("t", real=True) | |
| variables = list(X) + list(K) | |
| field = list(K) + list(X.cross(K)) | |
| def lie(expression): | |
| return sp.expand(sum(sp.diff(expression, variable)*velocity | |
| for variable, velocity in zip(variables, field))) | |
| assert lie(sq(K)) == 0 | |
| assert sp.expand(lie((X.T*K)[0]) - sq(K)) == 0 | |
| assert sp.expand(lie(sq(X)) - 2*(X.T*K)[0]) == 0 | |
| U = X-t*K | |
| first = U.applyfunc(lambda expression: sp.expand(sp.diff(expression, t)+lie(expression))) | |
| second = first.applyfunc(lambda expression: sp.expand(sp.diff(expression, t)+lie(expression))) | |
| initial = dict(zip(variables, [1, 1, 0, 0, 0, 1])) | |
| initial[t] = 0 | |
| assert first.subs(initial) == sp.zeros(3, 1) | |
| assert second.subs(initial) == sp.Matrix([-1, 1, 0]) | |
| # Substitution of the ODE's proved scalar identities gives ||U(t)||^2=2. | |
| assert sp.expand((2+t*t)-2*t*t+t*t) == 2 | |
| def main(): | |
| general = check_general_cases() | |
| launch = check_orthogonal_launch() | |
| schedule_counts = {} | |
| for d in range(9): | |
| schedule_counts[d] = ( | |
| maximal_rank_history(d, False), | |
| maximal_rank_history(d, True), | |
| ) | |
| check_examples() | |
| check_failure_modes() | |
| check_spectral_reduction() | |
| check_three_step_accessibility() | |
| check_general_three_dimensional_accessibility() | |
| check_ode_boundary_identities() | |
| print("PASS: exact kernel, rank, rho, reverse, balance, and exterior identities") | |
| print(f"PASS: {general} arbitrary rational state/input cases") | |
| print(f"PASS: {launch} orthogonal-launch states in dimensions 0 through 8") | |
| print("PASS: exact minimum rank-birth schedules (uncharged, charged):") | |
| print(schedule_counts) | |
| print("PASS: d=2 halt; d=3 hidden histories; scaled memory; sharp floor attainment") | |
| print("PASS: four common moves with distinct dormant invertible blocks") | |
| print("PASS: ungated cancellation, indefinite-metric failure, subdivision obstruction") | |
| print("PASS: reduced rank-one PSD spectral formulas in two canonical cases") | |
| print("PASS: symbolic three-step endpoint formula and Jacobian determinant -12") | |
| print("PASS: general 3D endpoint classification identities; determinant -2*b^2*c^4*(1+b^2)") | |
| print("PASS: symbolic ODE balances and boundary derivative u''(0)=(-1,1,0)") | |
| print("SCOPE: finite exact examples and stated symbolic identities only") | |
| print("PROOFS: Avenyra_Area_Feedback_v1.0.0.tex") | |
| if __name__ == "__main__": | |
| main() | |