Repository navigation
v0.3.0 — factorizations and least squares
Two new tools, both carrying the residual they actually measured.
matrix_decompose (kind: lu | qr | cholesky)
lu— LU with partial pivoting:P·A = L·U(unit lowerL, upperU). Returns the row permutationp(row i ofP·Ais rowp[i]ofA), the swap count, the determinant sign, the determinant implied byU, andmax |P·A − L·U|. Singular matrices are still factored and flagged withsingular: trueinstead of silently returning wrong factors.qr— thin QR with a positive diagonal inR, which makes the factorization unique:A = Q·R,Qhas orthonormal columns, plusmax |A − Q·R|andmax |QᵀQ − I|. Linearly dependent columns are an error naming the column — no orthonormal completion is invented. Rows must be ≥ columns.cholesky—A = L·Lᵀfor symmetric positive definite input, withmax |A − L·Lᵀ|and the determinant implied byL. An asymmetric matrix is rejected with the offending entry and the asymmetry size; an indefinite one with the failing pivot.
least_squares
For tall systems (m ≥ n), returns the x minimizing ‖A·x − b‖, the minimum residual norm itself, and max |Aᵀ(A·x − b)| — zero confirms the answer really is the minimizer. Solved through the thin QR factorization rather than the normal equations (which square the condition number). Rank-deficient or underdetermined input is an error: the minimizer is not unique, so none is invented.
least_squares(a=[[1,1],[1,2],[1,3]], b=[1,2,2])
→ x1 = 0.6666666667, x2 = 0.5
min ‖A·x − b‖ = 0.4082482905, max |Aᵀ(A·x − b)| = 0
Quality
- 128 → 156 tests; line coverage 94.48% → 96.08% (baseline ratcheted up).
- The oracle
test/oracle/anchors.pygained a section D that re-derives every anchor independently: exact rational Gaussian elimination for LU / Cholesky / least squares (via the normal equations) and classical Gram–Schmidt for QR — a different algorithm from the implementation's modified Gram–Schmidt. Its--checkassertsL·U = P·A,QᵀQ = I,L·Lᵀ = A, that the LU determinant equals the exact determinant, and that the least-squares residual is orthogonal to the columns. - Writing those self-checks caught one wrong expectation of mine immediately (the sign convention of the residual vector), which is exactly what the oracle is for.
中文摘要:新增 matrix_decompose(lu:P·A = L·U,奇异矩阵如实标记;qr:薄分解 A = Q·R,R 对角为正;cholesky:A = L·Lᵀ,仅对称正定)与 least_squares(超定最小二乘:解、最小残差范数、max |Aᵀ(A·x − b)|;秩亏或欠定直接报错)。每种分解都回报实测重构残差,不编造正交补、不挑一个解充数。测试 128 → 156,行覆盖 94.48% → 96.08%,随仓 oracle 增 D 区(精确有理数实现 + 经典 Gram–Schmidt,与实现算法不同)。