Download verify_continuous.py from PureOne/qaenthrix-eve: direct link, hf CLI and curl.
- Browser
- Download file 14.3 kB
-
https://huggingface.co/datasets/PureOne/qaenthrix-eve/resolve/main/verify_continuous.py
- Command line
-
hf download hf://datasets/PureOne/qaenthrix-eve/verify_continuous.py
-
curl -L -o verify_continuous.py https://huggingface.co/datasets/PureOne/qaenthrix-eve/resolve/main/verify_continuous.py
14.3 kB
| """Independent exact and numerical checks of the arbitrary-rank extension.""" | |
| from fractions import Fraction | |
| from itertools import combinations, permutations | |
| from math import factorial, prod, comb, sqrt | |
| import numpy as np | |
| from continuous_factorization import ( | |
| repair_factor, reference_frame, reference_factor, nearest_blind_projector, | |
| wronskian_atlas, coordinate_atlas, select_reference_frame, jet_reference, | |
| subspace_transport, subspace_holonomy, | |
| ) | |
| def _det(matrix): | |
| a = [[Fraction(x) for x in row] for row in matrix] | |
| value = Fraction(1) | |
| for k in range(len(a)): | |
| pivot = next((j for j in range(k, len(a)) if a[j][k]), None) | |
| if pivot is None: | |
| return Fraction(0) | |
| if pivot != k: | |
| a[k], a[pivot] = a[pivot], a[k] | |
| value = -value | |
| z = a[k][k] | |
| value *= z | |
| for j in range(k+1, len(a)): | |
| multiplier = a[j][k]/z | |
| for l in range(k+1, len(a)): | |
| a[j][l] -= multiplier*a[k][l] | |
| return value | |
| def _multiply(a, b): | |
| result = [Fraction(0)]*(len(a)+len(b)-1) | |
| for j, x in enumerate(a): | |
| for k, y in enumerate(b): | |
| result[j+k] += x*y | |
| return result | |
| def _derivative(a, k): | |
| return [Fraction(a[j])*(factorial(j)//factorial(j-k)) | |
| for j in range(k, len(a))] or [Fraction(0)] | |
| def _wronskian(columns): | |
| r = len(columns) | |
| degree = r*(len(columns[0])-1)-r*(r-1)//2 | |
| result = [Fraction(0)]*(degree+1) | |
| for p in permutations(range(r)): | |
| sign = (-1)**sum(p[j] > p[k] for j in range(r) for k in range(j+1, r)) | |
| term = [Fraction(1)] | |
| for k in range(r): | |
| term = _multiply(term, _derivative(columns[p[k]], k)) | |
| for k, x in enumerate(term): | |
| result[k] += sign*x | |
| while len(result) > 1 and result[-1] == 0: | |
| result.pop() | |
| return result | |
| def _evaluate(a, t): | |
| return sum(x*Fraction(t)**k for k, x in enumerate(a)) | |
| def _exact_wronski_checks(): | |
| monomials, evaluations = 0, 0 | |
| for n in range(2, 9): | |
| for r in range(1, n): | |
| d = r*(n-r) | |
| for degrees in combinations(range(n), r): | |
| leading = prod(degrees[j]-degrees[i] | |
| for i in range(r) for j in range(i+1, r)) | |
| power = sum(degrees)-r*(r-1)//2 | |
| assert 0 <= power <= d and leading > 0 | |
| nonzero = False | |
| for t in range(d+1): | |
| rows = [[0 if k > j else factorial(j)//factorial(j-k)*t**(j-k) | |
| for j in degrees] for k in range(r)] | |
| observed = _det(rows) | |
| expected = leading*Fraction(t)**power | |
| assert observed == expected | |
| nonzero |= observed != 0 | |
| evaluations += 1 | |
| assert nonzero | |
| monomials += 1 | |
| rng = np.random.default_rng(30001004) | |
| polynomials = 0 | |
| for _ in range(64): | |
| n = int(rng.integers(2, 7)); r = int(rng.integers(1, min(n, 5))) | |
| matrix = rng.integers(-3, 4, size=(n, r)) | |
| # An identity block gives an exact independence certificate. | |
| matrix[:r, :] = np.eye(r, dtype=int) | |
| columns = [[int(x) for x in matrix[:, j]] for j in range(r)] | |
| coefficients = _wronskian(columns) | |
| assert any(coefficients) and len(coefficients)-1 <= r*(n-r) | |
| nonzero = False | |
| for t in range(r*(n-r)+1): | |
| exact = _det([[_evaluate(_derivative(col, k), t) | |
| for col in columns] for k in range(r)]) | |
| assert exact == _evaluate(coefficients, t) | |
| nonzero |= exact != 0 | |
| assert nonzero | |
| polynomials += 1 | |
| return {"exact_monomial_subspaces": monomials, | |
| "exact_monomial_jet_determinants": evaluations, | |
| "exact_integer_polynomial_subspaces": polynomials} | |
| def _random_frame(rng, n, r): | |
| a = rng.normal(size=(n, r))+1j*rng.normal(size=(n, r)) | |
| return np.linalg.qr(a)[0][:, :r] | |
| def _rotation(rng, n, scale): | |
| h = rng.normal(size=(n, n))+1j*rng.normal(size=(n, n)) | |
| h = (h+h.conj().T)/2 | |
| values, vectors = np.linalg.eigh(h) | |
| return (vectors*np.exp(1j*scale*values)) @ vectors.conj().T | |
| def run(): | |
| rng = np.random.default_rng(30001004) | |
| maxima = {k: 0. for k in ["repaired_gram_error", "reference_gram_error", | |
| "blind_distance_error", "transport_error", "decoder_error", | |
| "cauchy_binet_error"]} | |
| counts = {k: 0 for k in ["repair_cases", "reference_cases", "blind_witnesses", | |
| "inside_radius_cases", "wronskian_atlas_cases", "coordinate_atlas_cases", | |
| "transport_cycles", "noisy_decoder_cases", "inverse_margin_cases", | |
| "gap_boundary_rejections", "structured_blind_families"]} | |
| observed_atlas_margin = 1. | |
| for n in range(2, 9): | |
| for r in range(1, n): | |
| wronski, coordinates = wronskian_atlas(n, r), coordinate_atlas(n, r) | |
| assert len(wronski) == r*(n-r)+1 | |
| assert len(coordinates) == comb(n, r) | |
| for _ in range(8): | |
| u, w = _random_frame(rng, n, r), _random_frame(rng, n, r) | |
| p = u @ u.conj().T | |
| values = rng.uniform(.5, 1.5, r) | |
| g = (u*values) @ u.conj().T | |
| out = _random_frame(rng, r+2, r) | |
| c0 = (out*np.sqrt(values)) @ u.conj().T | |
| perturbation = rng.normal(size=c0.shape)+1j*rng.normal(size=c0.shape) | |
| perturbation *= .01/np.linalg.norm(perturbation, 2) | |
| c = c0+perturbation | |
| d, info = repair_factor(g, c) | |
| error = float(np.linalg.norm(d.conj().T @ d-g, 2)) | |
| maxima["repaired_gram_error"] = max(maxima["repaired_gram_error"], error) | |
| assert error < 2e-12 and d.shape == c.shape | |
| assert np.linalg.norm(d-c@p, 2) <= info["support_correction_bound"]+2e-12 | |
| assert np.linalg.norm(d-c, 2) <= info["total_correction_bound"]+2e-12 | |
| counts["repair_cases"] += 1 | |
| frame, delta = reference_frame(p, w) | |
| factor, _ = reference_factor(p, w, .7) | |
| referr = float(np.linalg.norm(factor.conj().T@factor-.7*p, 2)) | |
| maxima["reference_gram_error"] = max(maxima["reference_gram_error"], referr) | |
| assert referr < 2e-11 | |
| counts["reference_cases"] += 1 | |
| blind, margin = nearest_blind_projector(p, w) | |
| assert np.linalg.norm(blind@blind-blind, 2) < 1e-10 | |
| assert abs(np.trace(blind).real-r) < 1e-10 | |
| assert np.linalg.svd(blind@w, compute_uv=False)[-1] < 1e-10 | |
| distance_error = abs(np.linalg.norm(p-blind, 2)-delta) | |
| maxima["blind_distance_error"] = max(maxima["blind_distance_error"], float(distance_error)) | |
| assert distance_error < 1e-10 and abs(margin-delta) < 1e-10 | |
| counts["blind_witnesses"] += 1 | |
| rotation = _rotation(rng, n, delta/100) | |
| q = rotation @ p @ rotation.conj().T | |
| eta = np.linalg.norm(q-p, 2) | |
| assert eta < delta | |
| frame2, delta2 = reference_frame(q, w) | |
| assert delta2 >= delta-eta-1e-12 | |
| bound = 2*np.linalg.norm((q-p)@w, 'fro')/(delta+delta2) | |
| assert np.linalg.norm(frame2-frame, 'fro') <= bound+2e-11 | |
| counts["inside_radius_cases"] += 1 | |
| _, _, chart_margin = select_reference_frame(p, wronski) | |
| assert chart_margin > 1e-12 | |
| observed_atlas_margin = min(observed_atlas_margin, chart_margin) | |
| counts["wronskian_atlas_cases"] += 1 | |
| _, _, chart_margin = select_reference_frame(p, coordinates) | |
| determinants = [abs(np.linalg.det(cw.conj().T @ u))**2 for cw in coordinates] | |
| cb_error = abs(sum(determinants)-1) | |
| maxima["cauchy_binet_error"] = max(maxima["cauchy_binet_error"], float(cb_error)) | |
| assert cb_error < 2e-12 and chart_margin+2e-12 >= 1/sqrt(comb(n,r)) | |
| counts["coordinate_atlas_cases"] += 1 | |
| # The noisy completion is checked on arbitrary complex inputs. | |
| weight = .6 | |
| visible = np.eye(n)+(np.sqrt(1-weight)-1)*p | |
| cf = np.sqrt(weight)*u.conj().T | |
| noise = rng.normal(size=cf.shape)+1j*rng.normal(size=cf.shape) | |
| noise *= .005/np.linalg.norm(noise, 2) | |
| cf += noise | |
| repaired, _ = repair_factor(weight*p, cf) | |
| completion = np.vstack([visible, repaired]) | |
| x = rng.normal(size=n)+1j*rng.normal(size=n) | |
| decoder_error = float(np.linalg.norm(completion.conj().T @ completion@x-x)) | |
| maxima["decoder_error"] = max(maxima["decoder_error"], decoder_error) | |
| assert decoder_error < 2e-11 | |
| unrepaired = np.vstack([visible, cf]) | |
| eps = np.linalg.norm(cf.conj().T@cf-weight*p, 2) | |
| decoder = np.linalg.solve(unrepaired.conj().T@unrepaired, unrepaired.conj().T) | |
| assert np.linalg.norm(decoder, 2) <= 1/np.sqrt(1-eps)+2e-12 | |
| counts["noisy_decoder_cases"] += 1 | |
| qframe, sframe = _random_frame(rng, n, r), _random_frame(rng, n, r) | |
| q, s = qframe@qframe.conj().T, sframe@sframe.conj().T | |
| transport = subspace_transport(p, q) | |
| te = max(np.linalg.norm(transport.conj().T@transport-q, 2), | |
| np.linalg.norm(transport@transport.conj().T-p, 2)) | |
| maxima["transport_error"] = max(maxima["transport_error"], float(te)) | |
| assert te < 2e-10 | |
| hol = subspace_holonomy([p,q,s], u) | |
| assert np.linalg.norm(hol.conj().T@hol-np.eye(r), 2) < 5e-10 | |
| gauge = _random_frame(rng, r, r) | |
| assert np.allclose(subspace_holonomy([p,q,s], u@gauge), | |
| gauge.conj().T@hol@gauge, atol=1e-10) | |
| global_rotation = _random_frame(rng, n, n) | |
| rotated = subspace_transport(global_rotation@p@global_rotation.conj().T, | |
| global_rotation@q@global_rotation.conj().T) | |
| assert np.allclose(rotated, global_rotation@transport@global_rotation.conj().T, | |
| atol=1e-10) | |
| counts["transport_cycles"] += 1 | |
| # Explicit inverse-margin family: one direction varies, r-1 remain fixed. | |
| w = np.eye(n, dtype=complex)[:, :r] | |
| v = np.eye(n, dtype=complex)[:, r] | |
| for delta in [.0001, .003, .04, .3, .8]: | |
| phase = .7 | |
| last0 = np.sqrt(1-delta**2)*v+delta*w[:, -1] | |
| last1 = np.sqrt(1-delta**2)*v+delta*np.exp(1j*phase)*w[:, -1] | |
| e0 = np.column_stack([w[:, :-1],last0]); e1 = np.column_stack([w[:, :-1],last1]) | |
| p, q = e0@e0.conj().T, e1@e1.conj().T | |
| f0,_ = reference_frame(p,w); f1,_ = reference_frame(q,w) | |
| ratio = np.linalg.norm(f0-f1,'fro')/np.linalg.norm(p-q,'fro') | |
| assert abs(ratio*delta-1/np.sqrt(2)) < 1e-9 | |
| counts["inverse_margin_cases"] += 1 | |
| equal = w@w.conj().T | |
| blind, delta = nearest_blind_projector(equal,w) | |
| assert abs(delta-1)<1e-12 and abs(np.linalg.norm(equal-blind,2)-1)<1e-12 | |
| counts["blind_witnesses"] += 1 | |
| # Equality at the positive-gap threshold permits losing a support direction. | |
| g = np.diag([1.]*r+[0.]*(n-r)).astype(complex) | |
| c = np.eye(n, dtype=complex)[:r-1] | |
| try: | |
| repair_factor(g,c) | |
| except ValueError: | |
| counts["gap_boundary_rejections"] += 1 | |
| else: | |
| raise AssertionError("gap equality must not be accepted") | |
| # A fixed m<n scanner family has an explicit blind support direction. | |
| m = n-1 | |
| h = np.eye(n,dtype=complex)[:m] | |
| support = np.column_stack([np.eye(n)[:, -1],np.eye(n)[:, :r-1]]) | |
| p = support@support.conj().T | |
| c = h@p | |
| assert np.linalg.norm(c@np.eye(n)[:, -1]) == 0 | |
| assert abs(np.linalg.norm(c.conj().T@c-p,2)-1)<1e-12 | |
| counts["structured_blind_families"] += 1 | |
| zero,_ = repair_factor(np.zeros((3,3)),np.ones((0,3))) | |
| assert zero.shape == (0,3) | |
| # Four distinct real jet charts have a common blind two-plane in C^4. | |
| a = (5+sqrt(73))/6; b = (-5+sqrt(73))/2 | |
| raw = np.array([[-a,0],[0,-b],[1,0],[0,1]],dtype=complex) | |
| u = np.linalg.qr(raw)[0][:,:2]; p = u@u.conj().T | |
| for t in [-2,-1,1,2]: | |
| assert np.linalg.svd(p@jet_reference(4,2,t),compute_uv=False)[-1]<1e-12 | |
| assert np.linalg.svd(p@jet_reference(4,2,0),compute_uv=False)[-1]>.1 | |
| # Noncommuting U(2) cycle phases, in one common base frame. | |
| base = np.eye(3,dtype=complex)[:,:2]; p0=base@base.conj().T | |
| def graph(z): | |
| frame=np.linalg.qr(np.vstack([np.eye(2),np.array(z)]))[0][:,:2] | |
| return frame@frame.conj().T | |
| pa,pb,pc = graph([[.6,0]]),graph([[0,.6]]),graph([[0,.6j]]) | |
| h1=subspace_holonomy([p0,pa,pb],base) | |
| h2=subspace_holonomy([p0,pa,pc],base) | |
| commutator=float(np.linalg.norm(h1@h2-h2@h1,2)) | |
| ha,hb=10*sqrt(34)/59,9/59 | |
| assert np.allclose(h1,[[ha,hb],[-hb,ha]],atol=1e-12) | |
| assert np.allclose(h2,[[ha,1j*hb],[1j*hb,ha]],atol=1e-12) | |
| assert Fraction(3400,3481)+Fraction(81,3481)==1 | |
| assert abs(commutator-162/3481)<1e-12 | |
| return {"status":"PASS", "seed":30001004, **_exact_wronski_checks(), **counts, | |
| "maximum_errors":maxima, | |
| "sampled_minimum_wronskian_atlas_margin":float(observed_atlas_margin), | |
| "wronskian_margin_scope":"sample statistic, not a uniform certificate", | |
| "four_reference_blind_counterexample":"Gr(2,4), nodes -2,-1,1,2", | |
| "noncommuting_holonomy_commutator_norm":commutator, | |
| "exact_holonomy_commutator_norm":"162/3481", | |
| "topological_scope":"finite checks do not prove the global lower bounds"} | |
| if __name__ == '__main__': | |
| import json | |
| print(json.dumps(run(),indent=2)) | |