"""Exact numerical and structural checks for the 2026-10-23 vector rewrite.
Uses standard-library Fraction; does not require an external CAS. Universal
claims and abstract proofs still require the reasoning written in the notes.
"""
from fractions import Fraction as F
from itertools import combinations
from pathlib import Path
import json
import re
ROOT = Path(__file__).resolve().parents[3]
CHAPTER = ROOT / '基于考研的学习/数学一/在学练习/线性代数-第3讲-向量'
checks = []
warns = []
def check(name, value):
assert value, name
checks.append(name)
def mat(rows):
return [[F(v) for v in row] for row in rows]
def cols(*vectors):
return mat(zip(*vectors))
def transpose(a):
return [list(row) for row in zip(*a)]
def mul(a, b):
return [[sum(x*y for x, y in zip(row, column))
for column in zip(*b)] for row in a]
def apply(a, x):
return [sum(v*c for v, c in zip(row, x)) for row in a]
def rank(rows):
a = mat(rows)
p = 0
for j in range(len(a[0])):
k = next((i for i in range(p, len(a)) if a[i][j]), None)
if k is None:
continue
a[p], a[k] = a[k], a[p]
d = a[p][j]
a[p] = [x/d for x in a[p]]
for i in range(len(a)):
if i != p:
d = a[i][j]
a[i] = [x-d*y for x, y in zip(a[i], a[p])]
p += 1
if p == len(a):
break
return p
def det(a):
if len(a) == 1:
return a[0][0]
return sum((-1)**j * a[0][j] * det([r[:j]+r[j+1:] for r in a[1:]])
for j in range(len(a)))
def eye(n):
return mat([[int(i == j) for j in range(n)] for i in range(n)])
def gram(vectors):
a = cols(*vectors)
return mul(transpose(a), a)
def orthogonal(name, vectors, norms_squared):
check(name, gram(vectors) == mat([[norms_squared[i] if i == j else 0
for j in range(len(vectors))]
for i in range(len(vectors))]))
u, v, w = [1, 0, 1], [0, 1, 1], [1, 1, 2]
check('1.1 target', apply(cols(u, v), [2, 3]) == [2, 3, 5])
check('1.1 impossible target', rank(cols(u, v)) == 2 and rank(cols(u, v, [2, 3, 6])) == 3)
check('1.2 nontrivial zero relation', apply(cols(u, v, w), [1, 1, -1]) == [0]*3)
check('1.5 reference coefficient relation', apply([[1, 0, 1], [1, 1, 0], [0, -1, 1]], [1, -1, -1]) == [0]*3)
check('1.5 plus variant independent', det([[1, 0, 1], [1, 1, 0], [0, 1, 1]]) == 2)
a2024 = cols([-2, 1, -1, 1], [1, 1, 2, -2], [1, -2, -1, 1])
check('1.6 2024 relation', apply(a2024, [1, 1, 1]) == [0]*4)
for pair in combinations(range(3), 2):
check('1.6 2024 pair '+str(pair), rank([[row[i] for i in pair] for row in a2024]) == 2)
A = cols([1, 2, -1], [2, 4, -2], [0, 1, 1], [1, 3, 0])
check('2.1 rank and pivot columns', rank(A) == 2 and rank(cols([1, 2, -1], [0, 1, 1])) == 2)
check('2.1 a2 relation', apply(A, [-2, 1, 0, 0]) == [0]*3)
check('2.1 a4 relation', apply(A, [-1, 0, -1, 1]) == [0]*3)
A = cols([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 1, 1], [1, 0, 0, 1])
check('2.3 four dimensional rank', rank(A) == 3)
check('2.3 cycle relation', apply(A, [1, -1, 1, -1]) == [0]*4)
check('2.4 2017 rank', rank([[1, 0, 1], [1, 1, 2], [0, 1, 1]]) == 2)
for t in [-2, 0, F(3, 2), 5]:
check('3.1 family '+str(t), apply(cols(u, v, w), [2-t, 3-t, t]) == [2, 3, 5])
A = cols([1, 1, 1], [1, 2, 4], [1, 3, 9])
check('3.2 reference representation', apply(A, [2, -2, 1]) == [1, 1, 3] and det(A) != 0)
for a in range(-3, 6):
A = [[1, 2, 1], [2, 3, a+2], [1, a, -2]]
check('3.3 determinant '+str(a), det(A) == -(a-3)*(a+1))
augmented = [row+[b] for row, b in zip(A, [1, 3, 0])]
expected = (2, 2) if a == 3 else ((2, 3) if a == -1 else (3, 3))
check('3.3 classification '+str(a), (rank(A), rank(augmented)) == expected)
A = [[1, 2, 1], [2, 3, 5], [1, 3, -2]]
check('3.3 particular', apply(A, [3, -1, 0]) == [1, 3, 0])
check('3.3 homogeneous direction', apply(A, [-7, 3, 1]) == [0]*3)
# Five distinct evaluations suffice for the displayed degree-at-most-four
# polynomial determinant identities; the notes separately show their expansion.
for l in [-2, -1, 0, 1, 2]:
A = [[l, 1, 1], [1, l, 1], [1, 1, l]]
B = [[l, 1, 1], [1, l, l], [1, 1, l*l]]
check('4.3 first determinant '+str(l), det(A) == (l-1)**2*(l+2))
check('4.3 second determinant '+str(l), det(B) == (l-1)**2*(l+1)**2)
joined = [x+y for x, y in zip(A, B)]
check('4.3 equivalence branch '+str(l), (rank(A) == rank(B) == rank(joined)) == (l not in [-1, -2]))
U = cols([1, 0, 1], [0, 1, 1])
C = mat([[1, 1], [1, -1]])
check('5.3 new coordinates', apply(C, [F(5, 2), F(-1, 2)]) == [2, 3])
check('5.3 vector reconstruction', apply(mul(U, C), [F(5, 2), F(-1, 2)]) == [2, 3, 5])
A = cols([1, 2, 1], [1, 3, 2], [1, 3, 3])
D = mat([[0, 0, 2], [1, 0, -2], [0, 1, 1]])
C = mat([[1, 1, 0], [F(-1, 2), 0, 1], [F(1, 2), 0, 0]])
N = cols([1, 3, 2], [1, 3, 3], [1, 1, 1])
check('5.4 2019 parameters', apply(A, [2, -2, 1]) == [1, 1, 1])
check('5.4 2019 new basis', det(A) == 1 and det(D) == 2 and mul(A, D) == N)
check('5.4 2019 requested transition direction', mul(N, C) == A and mul(D, C) == eye(3))
check('5.5 coordinate conversion', apply(C, [1, 0, 2]) == [1, F(3, 2), F(1, 2)])
check('5.5 reconstruction', apply(N, [1, F(3, 2), F(1, 2)]) == [3, 8, 7])
vectors = [[1, 0, 1, 1], [-1, -1, 0, 1], [0, 1, -1, 1]]
orthogonal('6.2 adapted source orthogonal', vectors, [3, 3, 3])
k = [F(1, 3), -1, F(-1, 3)]
check('6.2 coefficient sum squares', sum(x*x for x in k) == F(11, 9))
g = apply(cols(*vectors), k)
check('6.2 projection value', g == [F(4, 3), F(2, 3), F(2, 3), -1])
residual = [F(b)-x for b, x in zip([1, 1, 1, -1], g)]
check('6.2 residual orthogonal', apply(transpose(cols(*vectors)), residual) == [0]*3)
orthogonal('6.3 2021 orthogonal', [[1, 0, 1], [0, 2, 0], [F(1, 2), 0, F(-1, 2)]], [2, 4, F(1, 2)])
check('6.3 2021 coefficient subtraction',
[F(a)-F(5, 2)*b-F(1, 2)*c for a, b, c in zip([3, 1, 2], [1, 0, 1], [0, 2, 0])]
== [F(1, 2), 0, F(-1, 2)])
orthogonal('6.5 orthogonal before normalization', [[1, 1, 0], [F(1, 2), F(-1, 2), 1], [F(-2, 3), F(2, 3), F(2, 3)]], [2, F(3, 2), F(4, 3)])
orthogonal('6.5 normalized numerators', [[1, 1, 0], [1, -1, 2], [-1, 1, 1]], [2, 6, 3])
A, B = cols([1, 1, 0], [0, 1, 1]), cols([1, 0, 1], [0, 1, 2])
check('7.2 first representation', apply(A, [1, -3]) == [1, -2, -3])
check('7.2 second representation', apply(B, [1, -2]) == [1, -2, -3])
A, B = cols([1, 2, 3], [2, 1, 1]), cols([2, 5, 9], [1, 0, 1])
check('7.3 2023 first representation', apply(A, [3, -1]) == [1, 5, 8])
check('7.3 2023 second representation', apply(B, [1, -1]) == [1, 5, 8])
check('7.3 ranks and intersection dimension', rank(A) == rank(B) == 2 and rank([x+y for x, y in zip(A, B)]) == 3)
A = cols([1, 0, 1, 0], [0, 1, 0, 1])
B = cols([1, 1, 0, 0], [0, 0, 1, 1])
check('7.4 four dimensional intersection', apply(A, [1, 1]) == apply(B, [1, 1]) == [1]*4)
check('7.4 four dimensional sum rank', rank([x+y for x, y in zip(A, B)]) == 3)
for a in [0, 1, 2]:
check('7.5 direct sum branch '+str(a), det([[1, 1, 2], [1, 0, 1], [0, 1, a]]) == 1-a)
check('8.2A reference relation', apply([[1, 1, 3], [1, -2, 2]], [-8, -1, 3]) == [0, 0])
A, B = [[1, 0, 0], [0, 1, 0]], [[1, 0], [0, 0], [0, 1]]
check('8.3B Sylvester sharp example', rank(A) == rank(B) == 2 and rank(mul(A, B)) == 1)
A = [[1, 0, 1], [0, 1, 1]]
check('8.4 Gram singular example', rank(A) == rank(mul(transpose(A), A)) == 2)
for file in sorted(CHAPTER.glob('*.md')):
text = file.read_text(encoding='utf-8')
check('structure balanced details '+file.name, text.count('') == text.count(' '))
check('structure summaries '+file.name, text.count('') == text.count('') == text.count(''))
# 字符数上限是项目约定(单文件 ≤ 1.3 万字符)。超限只记警告:它不影响
# 数值与链接的正确性,若直接 assert 会让后面几百项检查全部不执行。
if len(text) > 13000:
warns.append('file size %s: %d 字符 > 13000' % (file.name, len(text)))
check('structure no replacement chars '+file.name, '\ufffd' not in text)
check('structure math dollar parity '+file.name, text.count('$') % 2 == 0)
for target in re.findall(r'\]\(([^)]+)\)', text):
if not target.startswith(('http:', 'https:')):
check('link '+file.name+' -> '+target, (file.parent / target.split('#')[0]).exists())
report = {'passed': len(checks), 'arithmetic': 'fractions.Fraction',
'scope': 'Numerical examples, parameter branches, link and Markdown structure; abstract proofs manually reviewed',
'warnings': warns,
'checks': checks}
(Path(__file__).resolve().parent / 'vector_learning_checks.json').write_text(json.dumps(report, ensure_ascii=False, indent=2)+'\n', encoding='utf-8')
print(f'PASS: {len(checks)} exact numerical and structural checks')
for w in warns:
print('WARN:', w)