Templatize SVD on N: support any Matrix<R,C> with stack-only buffers

Replace the fixed 5x5 SVD implementation with template <uint8_t N>
building blocks over Matrix<N,N> working buffers (N = max(rows, cols)),
removing the 5x5 size limit. Public API (SVD::SVD) is unchanged and the
whole path stays heap-free: peak stack is ~11*N^2 floats, budgeted in
the SVD.hpp header doc.

Fixes found while porting/validating the templated rewrite:
- QL.Identity() was a no-op (static factory returns by value); init
  the Householder accumulators with an explicit diagonal loop
- restore the QL column sign-flip in ExtractAndSortSingularValues for
  negative unsolved W diagonal entries (Householder sign flips)
- T = B^T B tridiagonal formula: T[i][i] = d[i]^2 + e[i-1]^2 only
  (e[i] contributes to T[i+1][i+1], not T[i][i])
- wide-matrix Vt assembly: Vt = QL^T must be filled over the full
  m x m (m = columns of A), not just the top n x n

Tests:
- matrix-tests: add large-size instantiation cases beyond the old limit
  (tall 7x5 N=7, square 6x6 N=6, wide 5x8 N=8 transpose path with full
  orthogonal 8x8 Vt, tall 6x4 near rank-deficient N=6 deflation path),
  all checked against numpy/scipy float32 references
- svd-build-blocks-tests: adapt Jacobi test to the Matrix<N,N> interface
- svd-reference-values.py: add the four new reference matrices
This commit is contained in:
2026-08-20 10:22:49 -04:00
parent f8221dd9db
commit ab0cea104c
5 changed files with 737 additions and 459 deletions
+11 -1
View File
@@ -1523,8 +1523,18 @@ TEST_CASE("SVD Building Block: JacobiEigenSymmetric", "[Matrix][SVD]") {
S_orig[i][j] = mats[c][i][j];
float evals[5] = {0};
// JacobiEigenSymmetric operates on Matrix<N,N> — copy the raw test
// data in, run the solver, copy the eigenvector matrix back out.
Matrix<5, 5> Tm{0};
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
Tm[i][j] = T[i][j];
Matrix<5, 5> Vm{0};
SVD::JacobiEigenSymmetric(Tm, ns[c], evals, Vm);
float V[5][5] = {{0}};
SVD::JacobiEigenSymmetric(T, ns[c], evals, V);
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
V[i][j] = Vm[i][j];
// 1. Sorted eigenvalues match scipy
float sorted[5] = {0};