Download continuous_factorization.py from PureOne/qaenthrix-eve: direct link, hf CLI and curl.
- Browser
- Download file 8.54 kB
-
https://huggingface.co/datasets/PureOne/qaenthrix-eve/resolve/main/continuous_factorization.py
- Command line
-
hf download hf://datasets/PureOne/qaenthrix-eve/continuous_factorization.py
-
curl -L -o continuous_factorization.py https://huggingface.co/datasets/PureOne/qaenthrix-eve/resolve/main/continuous_factorization.py
8.54 kB
| """Continuous Gramian factors, subspace references, and canonical transport. | |
| Analytic statements appear in MANUSCRIPT.md. Numerical rank and blind-locus | |
| tolerances are explicit and are not certificates of exact algebraic rank. | |
| """ | |
| from itertools import combinations | |
| from math import factorial | |
| import numpy as np | |
| def _frame(w, tolerance=1e-10): | |
| w = np.asarray(w, dtype=complex) | |
| if w.ndim != 2 or not 0 < w.shape[1] <= w.shape[0]: | |
| raise ValueError("reference must be an n-by-r frame, 1 <= r <= n") | |
| if not np.allclose(w.conj().T @ w, np.eye(w.shape[1]), | |
| atol=tolerance, rtol=tolerance): | |
| raise ValueError("reference columns must be orthonormal") | |
| return w | |
| def _projector(p, tolerance=1e-10): | |
| p = np.asarray(p, dtype=complex) | |
| if p.ndim != 2 or p.shape[0] != p.shape[1]: | |
| raise ValueError("projector must be square") | |
| if not np.allclose(p, p.conj().T, atol=tolerance, rtol=tolerance): | |
| raise ValueError("projector must be Hermitian") | |
| if not np.allclose(p @ p, p, atol=tolerance, rtol=tolerance): | |
| raise ValueError("projector must be idempotent") | |
| return p | |
| def _positive_power(a, power, tolerance=1e-12): | |
| values, vectors = np.linalg.eigh((a + a.conj().T) / 2) | |
| if len(values) == 0 or values[0] <= tolerance: | |
| raise ValueError("restricted operator is not positive at the declared tolerance") | |
| return (vectors * values**power) @ vectors.conj().T | |
| def repair_factor(g, c, rank_tolerance=1e-12): | |
| """Repair a factor when its Gram error is smaller than G's positive gap. | |
| Returns (D, diagnostics), with D*D=G up to floating point error and the | |
| same row count as C. Eigenvalues <= rank_tolerance are treated as zero. | |
| The implementation uses a support frame; the mathematical formula is | |
| independent of the choice of that frame. | |
| """ | |
| g = np.asarray(g, dtype=complex) | |
| c = np.asarray(c, dtype=complex) | |
| if g.ndim != 2 or g.shape[0] != g.shape[1]: | |
| raise ValueError("Gramian must be square") | |
| if c.ndim != 2 or c.shape[1] != len(g): | |
| raise ValueError("factor must have n columns") | |
| if not np.allclose(g, g.conj().T, atol=rank_tolerance, rtol=rank_tolerance): | |
| raise ValueError("Gramian must be Hermitian") | |
| values, u = np.linalg.eigh(g) | |
| if values[0] < -rank_tolerance: | |
| raise ValueError("Gramian must be positive semidefinite") | |
| keep = values > rank_tolerance | |
| error = float(np.linalg.norm(c.conj().T @ c - g, 2)) | |
| if not np.any(keep): | |
| return np.zeros_like(c), {"rank": 0, "gram_error": error, | |
| "rank_tolerance": rank_tolerance} | |
| u, values = u[:, keep], values[keep] | |
| gap = float(values[0]) | |
| if error >= gap: | |
| raise ValueError("Gram error must be strictly smaller than the positive gap") | |
| a = c @ u | |
| b = a.conj().T @ a | |
| inv_sqrt = _positive_power(b, -.5, rank_tolerance) | |
| d = ((a @ inv_sqrt) * np.sqrt(values)) @ u.conj().T | |
| correction_bound = error / (np.sqrt(gap) + np.sqrt(gap-error)) | |
| return d, {"rank": len(values), "gram_error": error, "positive_gap": gap, | |
| "support_correction_bound": correction_bound, | |
| "total_correction_bound": np.sqrt(error) + correction_bound, | |
| "rank_tolerance": rank_tolerance} | |
| def reference_frame(p, w, blind_tolerance=1e-12): | |
| """Polar frame of P W, with exact-model margin sigma_min(P W).""" | |
| p, w = _projector(p), _frame(w) | |
| if p.shape[0] != w.shape[0] or int(round(np.trace(p).real)) != w.shape[1]: | |
| raise ValueError("projector and reference must have equal rank r") | |
| left, singular, right = np.linalg.svd(p @ w, full_matrices=False) | |
| margin = float(singular[-1]) | |
| if margin <= blind_tolerance: | |
| raise ValueError("reference is blind at the declared numerical tolerance") | |
| return left @ right, margin | |
| def reference_factor(p, w, weight, blind_tolerance=1e-12): | |
| if weight < 0: | |
| raise ValueError("weight must be nonnegative") | |
| frame, margin = reference_frame(p, w, blind_tolerance) | |
| return np.sqrt(weight) * frame.conj().T, margin | |
| def nearest_blind_projector(p, w, blind_tolerance=1e-12): | |
| """Construct a rank-r blind projector at distance sigma_min(P W). | |
| Requires 1 <= r < n. At numerically zero margin P itself is returned. | |
| The equal-subspace case (margin one) replaces one vector by a vector | |
| in the orthogonal complement. | |
| """ | |
| p, w = _projector(p), _frame(w) | |
| n, r = w.shape | |
| if not r < n or int(round(np.trace(p).real)) != r: | |
| raise ValueError("construction requires equal ranks with 1 <= r < n") | |
| values, vectors = np.linalg.eigh(w.conj().T @ p @ w) | |
| delta = float(np.sqrt(max(0., values[0]))) | |
| if delta <= blind_tolerance: | |
| return p.copy(), delta | |
| ref = w @ vectors[:, 0] | |
| e = p @ ref / delta | |
| if 1-delta**2 <= blind_tolerance: | |
| evals, evecs = np.linalg.eigh(p) | |
| z = evecs[:, np.argmin(evals)] | |
| else: | |
| complement = (ref-delta*e) / np.sqrt(1-delta**2) | |
| z = np.sqrt(1-delta**2)*e - delta*complement | |
| blind = p - np.outer(e, e.conj()) + np.outer(z, z.conj()) | |
| return (blind + blind.conj().T)/2, delta | |
| def jet_reference(n, r, t): | |
| """Orthonormal derivative-evaluation frame for real t, polynomials deg<n.""" | |
| if not 1 <= r <= n or not np.isfinite(t) or not np.isreal(t): | |
| raise ValueError("require 1 <= r <= n and a finite real node") | |
| raw = np.zeros((n, r), dtype=float) | |
| for j in range(n): | |
| for k in range(min(j+1, r)): | |
| raw[j, k] = factorial(j)/factorial(j-k) * float(t)**(j-k) | |
| left, singular, right = np.linalg.svd(raw, full_matrices=False) | |
| if singular[-1] <= np.finfo(float).eps * singular[0]: | |
| raise ValueError("jet frame is numerically rank deficient; rescale the basis/nodes") | |
| return left @ right | |
| def wronskian_atlas(n, r, nodes=None): | |
| """Explicit minimal atlas of r(n-r)+1 fixed rank-r reference frames. | |
| Default nodes are Chebyshev nodes in [-1,1]. Coverage is analytic; | |
| this choice is not asserted to optimize conditioning. | |
| """ | |
| if not 1 <= r < n: | |
| raise ValueError("require 1 <= r < n") | |
| count = r*(n-r)+1 | |
| if nodes is None: | |
| nodes = np.cos(np.pi*(np.arange(count)+.5)/count) | |
| nodes = np.asarray(nodes) | |
| if nodes.shape != (count,) or len(np.unique(nodes)) != count: | |
| raise ValueError("provide exactly r(n-r)+1 distinct real nodes") | |
| return [jet_reference(n, r, t) for t in nodes] | |
| def coordinate_atlas(n, r): | |
| """All binomial(n,r) coordinate frames; certified margin >= binomial^-1/2.""" | |
| if not 1 <= r < n: | |
| raise ValueError("require 1 <= r < n") | |
| eye = np.eye(n, dtype=complex) | |
| return [eye[:, indices] for indices in combinations(range(n), r)] | |
| def select_reference_frame(p, references, blind_tolerance=1e-12): | |
| """Select a largest-margin chart; ties can cause a discontinuous switch.""" | |
| p = _projector(p) | |
| refs = [_frame(w) for w in references] | |
| if not refs or any(w.shape != refs[0].shape for w in refs): | |
| raise ValueError("references must be a nonempty list of equal-size frames") | |
| margins = [np.linalg.svd(p @ w, compute_uv=False)[-1] for w in refs] | |
| index = int(np.argmax(margins)) | |
| frame, margin = reference_frame(p, refs[index], blind_tolerance) | |
| return index, frame, margin | |
| def subspace_transport(p, q, blind_tolerance=1e-12): | |
| """Canonical partial isometry Q -> P for equal-rank transverse subspaces.""" | |
| p, q = _projector(p), _projector(q) | |
| values, basis = np.linalg.eigh(q) | |
| u = basis[:, values > .5] | |
| if len(u.T) != int(round(np.trace(p).real)) or len(u.T) == 0: | |
| raise ValueError("projectors must have the same positive rank") | |
| b = u.conj().T @ p @ u | |
| if np.linalg.eigvalsh(b)[0] <= blind_tolerance**2: | |
| raise ValueError("orthogonal component prevents invertible comparison") | |
| return p @ u @ _positive_power(b, -.5, blind_tolerance**2) @ u.conj().T | |
| def subspace_holonomy(projectors, initial_frame, blind_tolerance=1e-12): | |
| """U(r) matrix for T(P0<-P1)...T(Plast<-P0) in the initial frame.""" | |
| ps = [_projector(p) for p in projectors] | |
| f = _frame(initial_frame) | |
| if len(ps) < 2 or not np.allclose(f @ f.conj().T, ps[0]): | |
| raise ValueError("cycle needs at least two vertices and a frame for P0") | |
| product = np.eye(len(ps[0]), dtype=complex) | |
| for j, p in enumerate(ps): | |
| product = product @ subspace_transport(p, ps[(j+1) % len(ps)], blind_tolerance) | |
| return f.conj().T @ product @ f | |