Datasets:
Formats:
json
Languages:
English
Size:
< 1K
Tags:
recursive-self-improvement
artificial-intelligence
homotopy-theory
statistical-experiments
quantum-error-correction
reproducible-research
Download research/src/exact.py from PureOne/Xhorielun-Sigma: direct link, hf CLI and curl.
- Browser
- Download file 2.96 kB
-
https://huggingface.co/datasets/PureOne/Xhorielun-Sigma/resolve/main/research/src/exact.py
- Command line
-
hf download hf://datasets/PureOne/Xhorielun-Sigma/research/src/exact.py
-
curl -L -o exact.py https://huggingface.co/datasets/PureOne/Xhorielun-Sigma/resolve/main/research/src/exact.py
2.96 kB
| """Small exact rational matrix kernel. Python standard library only. | |
| Matrices are rectangular lists of rows. Empty dimensions are not represented; | |
| chain examples use explicit positive-dimensional spaces. | |
| """ | |
| from fractions import Fraction as Q | |
| def matrix(rows): | |
| out = [[Q(x) for x in row] for row in rows] | |
| if out and any(len(row) != len(out[0]) for row in out): | |
| raise ValueError('Ragged matrix') | |
| return out | |
| def zeros(m, n): | |
| return [[Q(0) for _ in range(n)] for _ in range(m)] | |
| def eye(n): | |
| return [[Q(i == j) for j in range(n)] for i in range(n)] | |
| def shape(a): | |
| return len(a), len(a[0]) if a else 0 | |
| def transpose(a): | |
| return [list(row) for row in zip(*a)] | |
| def add(a, b): | |
| if shape(a) != shape(b): | |
| raise ValueError('Addition shape mismatch') | |
| return [[x + y for x, y in zip(ar, br)] for ar, br in zip(a, b)] | |
| def scale(c, a): | |
| return [[Q(c) * x for x in row] for row in a] | |
| def sub(a, b): | |
| return add(a, scale(-1, b)) | |
| def mul(a, b): | |
| ma, na = shape(a); mb, nb = shape(b) | |
| if na != mb: | |
| raise ValueError(f'Multiplication shape mismatch: {shape(a)} {shape(b)}') | |
| return [[sum((a[i][k] * b[k][j] for k in range(na)), Q(0)) | |
| for j in range(nb)] for i in range(ma)] | |
| def block(rows): | |
| """Assemble a rectangular block array; caller supplies zero blocks.""" | |
| out = [] | |
| for blocks in rows: | |
| heights = {len(b) for b in blocks} | |
| if len(heights) != 1: | |
| raise ValueError('Block row height mismatch') | |
| for i in range(next(iter(heights))): | |
| out.append([x for b in blocks for x in b[i]]) | |
| return matrix(out) | |
| def rref(a): | |
| a = matrix(a); m, n = shape(a); pivot_cols = []; i = 0 | |
| for j in range(n): | |
| k = next((k for k in range(i, m) if a[k][j]), None) | |
| if k is None: | |
| continue | |
| a[i], a[k] = a[k], a[i] | |
| pivot = a[i][j] | |
| a[i] = [x / pivot for x in a[i]] | |
| for k in range(m): | |
| if k != i and a[k][j]: | |
| c = a[k][j] | |
| a[k] = [x - c*y for x, y in zip(a[k], a[i])] | |
| pivot_cols.append(j); i += 1 | |
| if i == m: | |
| break | |
| return a, pivot_cols | |
| def rank(a): | |
| return len(rref(a)[1]) | |
| def inverse(a): | |
| n, m = shape(a) | |
| if n != m: | |
| raise ValueError('Inverse requires square matrix') | |
| aug = [ar + ir for ar, ir in zip(a, eye(n))] | |
| reduced, piv = rref(aug) | |
| if piv[:n] != list(range(n)): | |
| raise ValueError('Singular matrix') | |
| return [row[n:] for row in reduced] | |
| def nullspace(a): | |
| rr, piv = rref(a); n = shape(a)[1] | |
| free = [j for j in range(n) if j not in piv] | |
| vecs = [] | |
| for j in free: | |
| v = [Q(0)] * n; v[j] = Q(1) | |
| for i, p in enumerate(piv): | |
| v[p] = -rr[i][j] | |
| vecs.append(v) | |
| return vecs | |
| def iszero(a): | |
| return all(x == 0 for row in a for x in row) | |
| def to_strings(a): | |
| return [[str(x) for x in row] for row in a] | |