Skip to content

Replace Brandts sorting with LAPACK DTRSEN, stabilize Gram-Schmidt - #64

Closed
Marius1311 wants to merge 1 commit into
mainfrom
feature/scipy-sorted-schur
Closed

Marius1311 wants to merge 1 commit into
mainfrom
feature/scipy-sorted-schur

Conversation

@Marius1311

Copy link
Copy Markdown
Collaborator
  • Replace 624-line Python Brandts sorting (_sort_real_schur.py) with a single LAPACK DTRSEN call in sorted_brandts_schur(). Same Bai-Demmel algorithm, compiled Fortran instead of Python loops. Fixes subspace angle failures for n >= 100.

  • Replace single-pass Modified Gram-Schmidt in _gram_schmidt_mod() with Householder QR (np.linalg.qr, LAPACK DGEQRF). Single-pass MGS lost orthogonality for ill-conditioned inputs (kappa up to 1e16). QR is backward stable regardless of conditioning. Same O(nm^2) complexity, faster in practice (compiled LAPACK vs Python loops).

  • Replace fragile constant-vector detection (element-wise constancy check with tight tolerance) with cosine similarity against sqrt(eta). The old check failed when Schur vectors were only approximately constant due to numerical noise from different eigenvalue orderings.

  • Recompute R in _do_schur() after re-orthogonalization to maintain the Schur relation in the new basis.

  • Update tests accordingly.

- Replace 624-line Python Brandts sorting (_sort_real_schur.py) with a
  single LAPACK DTRSEN call in sorted_brandts_schur(). Same Bai-Demmel
  algorithm, compiled Fortran instead of Python loops. Fixes subspace
  angle failures for n >= 100.

- Replace single-pass Modified Gram-Schmidt in _gram_schmidt_mod() with
  Householder QR (np.linalg.qr, LAPACK DGEQRF). Single-pass MGS lost
  orthogonality for ill-conditioned inputs (kappa up to 1e16). QR is
  backward stable regardless of conditioning. Same O(nm^2) complexity,
  faster in practice (compiled LAPACK vs Python loops).

- Replace fragile constant-vector detection (element-wise constancy
  check with tight tolerance) with cosine similarity against sqrt(eta).
  The old check failed when Schur vectors were only approximately
  constant due to numerical noise from different eigenvalue orderings.

- Recompute R in _do_schur() after re-orthogonalization to maintain the
  Schur relation in the new basis.

- Update tests accordingly.
@Marius1311

Copy link
Copy Markdown
Collaborator Author

Closing; findings for each part (details in #70):

  • trsen instead of Brandts: a single trsen call moves the top k to the front but doesn't order them, and GPCCA slices X[:, :m] for every m. That's why this PR had to change the expected kopt/minChi. A full sort with trexc matches Brandts on 24 metastable matrices (n = 100–1000), but LAPACK refuses to swap Brandts' own test matrix M_4 (complex pairs 1e-5 apart), which the Python sort handles. No Brandts failure showed up on Linux for n up to 1000, and brandts returns a non-invariant subspace on macOS arm64 #68 turned out to be pair order (Handle conjugate pairs in either order (Accelerate, #68) #75). So Brandts stays.
  • MGS → Householder QR: the input is the orthonormal Schur basis with column 0 replaced by √η, so it is well conditioned. I found no case where MGS loses orthogonality.
  • Cosine search for √η, recompute R: real for periodic chains with non-uniform η. There the returned (X, R) violates P X = X R. This goes into a separate PR.

@Marius1311 Marius1311 closed this Nov 13, 2026
@Marius1311
Marius1311 deleted the feature/scipy-sorted-schur branch November 13, 2026 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant