From 831ac6c353b3986b656c352171c1a9e037a4d980 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 28 Oct 2026 20:13:47 +0000 Subject: [PATCH 1/2] Orthonormalise the cross-approximation bases through QR before the SVD Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MFB6QLC6geNXiEorQzmJ6r --- src/mariepy/implicit.py | 34 ++++++++++++++++++++-------------- tests/test_implicit.py | 26 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/mariepy/implicit.py b/src/mariepy/implicit.py index 1341205..3aed760 100644 --- a/src/mariepy/implicit.py +++ b/src/mariepy/implicit.py @@ -648,10 +648,8 @@ def sample(cells: torch.Tensor, dofs: torch.Tensor | None) -> torch.Tensor: dofs = torch.sort( torch.randperm(n_coil, generator=generator)[:rank].to(grid.device) ).values - body_basis, _, _ = torch.linalg.svd(sample(region_cells, dofs), full_matrices=False) - coil_basis, _, _ = torch.linalg.svd( - sample(cells, None).conj().transpose(0, 1), full_matrices=False - ) + body_basis = _left_singular_vectors(sample(region_cells, dofs)) + coil_basis = _left_singular_vectors(sample(cells, None).conj().transpose(0, 1)) def fit() -> tuple: rows = _row_numbers(grid, cells, n_components) @@ -679,21 +677,16 @@ def fit() -> tuple: if fresh_dofs.numel() == 0 and fresh_cells.numel() == 0: break if fresh_dofs.numel(): - body_basis, _, _ = torch.linalg.svd( - torch.cat([body_basis, sample(region_cells, fresh_dofs)], dim=1), - full_matrices=False, + body_basis = _left_singular_vectors( + torch.cat([body_basis, sample(region_cells, fresh_dofs)], dim=1) ) dofs = torch.sort(torch.cat([dofs, fresh_dofs])).values if fresh_cells.numel(): - coil_basis, _, _ = torch.linalg.svd( + coil_basis = _left_singular_vectors( torch.cat( - [ - coil_basis, - sample(fresh_cells, None).conj().transpose(0, 1), - ], + [coil_basis, sample(fresh_cells, None).conj().transpose(0, 1)], dim=1, - ), - full_matrices=False, + ) ) cells = torch.sort(torch.cat([cells, fresh_cells])).values @@ -771,6 +764,19 @@ def _settled( return bool(tol * torch.linalg.vector_norm(now[:rank]) > reached) +def _left_singular_vectors(matrix: torch.Tensor) -> torch.Tensor: + """Give the left singular vectors of ``matrix``, strongest first. + + The matrix is reduced by QR first, so the singular value decomposition runs + on its small triangular factor. A basis extended by columns it already + spans has many singular values at one and many near zero, which the divide + and conquer driver can fail to converge on when given the tall matrix whole. + """ + q, r = torch.linalg.qr(matrix) + u, _, _ = torch.linalg.svd(r, full_matrices=False) + return q @ u + + def _outside(candidate: torch.Tensor, held: torch.Tensor) -> torch.Tensor: """Those of ``candidate`` that ``held`` does not already carry.""" fresh = torch.unique(candidate) diff --git a/tests/test_implicit.py b/tests/test_implicit.py index 398e188..60b5d23 100644 --- a/tests/test_implicit.py +++ b/tests/test_implicit.py @@ -326,3 +326,29 @@ def test_the_cross_approximation_follows_the_tolerance_it_is_given(device): left[tol] = implicit._missed(operator, coupling, coil_side, 2) assert left[tol] <= 10 * tol assert left[1e-4] < left[1e-2] + + +def test_left_singular_vectors_of_an_extended_basis_match_the_direct_decomposition( + device, +): + generator = torch.Generator().manual_seed(0) + tall = torch.randn(400, 12, dtype=torch.complex128, generator=generator) + basis = torch.linalg.qr(tall).Q.to(device) + extended = torch.cat( + [ + basis, + basis[:, :5] + @ torch.randn(5, 6, dtype=torch.complex128, generator=generator).to(device), + ], + dim=1, + ) + vectors = implicit._left_singular_vectors(extended) + direct, _, _ = torch.linalg.svd(extended, full_matrices=False) + assert vectors.shape == direct.shape + assert torch.allclose( + vectors.conj().transpose(0, 1) @ vectors, + torch.eye(vectors.shape[1], dtype=vectors.dtype, device=device), + atol=1e-10, + ) + span = vectors[:, :12] @ vectors[:, :12].conj().transpose(0, 1) + assert torch.allclose(span @ basis, basis, atol=1e-10) From cf87763ae20207162029a8c4b3bdf74a9b06f43a Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 28 Oct 2026 20:19:45 +0000 Subject: [PATCH 2/2] Fall back to the Gram eigendecomposition when the basis SVD does not converge Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MFB6QLC6geNXiEorQzmJ6r --- src/mariepy/implicit.py | 17 +++++++++----- tests/test_implicit.py | 49 +++++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/mariepy/implicit.py b/src/mariepy/implicit.py index 3aed760..ad4a944 100644 --- a/src/mariepy/implicit.py +++ b/src/mariepy/implicit.py @@ -767,13 +767,20 @@ def _settled( def _left_singular_vectors(matrix: torch.Tensor) -> torch.Tensor: """Give the left singular vectors of ``matrix``, strongest first. - The matrix is reduced by QR first, so the singular value decomposition runs - on its small triangular factor. A basis extended by columns it already - spans has many singular values at one and many near zero, which the divide - and conquer driver can fail to converge on when given the tall matrix whole. + A basis extended by columns it already spans has many singular values at one + and many near zero, which LAPACK's divide and conquer SVD can fail to + converge on, depending on the CPU. The matrix is reduced by QR first, and if + the SVD of its triangular factor still fails, the vectors come from the + eigendecomposition of that factor's Gram matrix instead. The two agree on + every vector whose singular value stands above the square root of machine + precision, which is all the cross approximation keeps. """ q, r = torch.linalg.qr(matrix) - u, _, _ = torch.linalg.svd(r, full_matrices=False) + try: + u, _, _ = torch.linalg.svd(r, full_matrices=False) + except torch.linalg.LinAlgError: + _, u = torch.linalg.eigh(r @ r.conj().transpose(0, 1)) + u = u.flip(-1) return q @ u diff --git a/tests/test_implicit.py b/tests/test_implicit.py index 60b5d23..186ae97 100644 --- a/tests/test_implicit.py +++ b/tests/test_implicit.py @@ -328,27 +328,34 @@ def test_the_cross_approximation_follows_the_tolerance_it_is_given(device): assert left[1e-4] < left[1e-2] -def test_left_singular_vectors_of_an_extended_basis_match_the_direct_decomposition( - device, -): +def _extended_basis(device): generator = torch.Generator().manual_seed(0) tall = torch.randn(400, 12, dtype=torch.complex128, generator=generator) - basis = torch.linalg.qr(tall).Q.to(device) - extended = torch.cat( - [ - basis, - basis[:, :5] - @ torch.randn(5, 6, dtype=torch.complex128, generator=generator).to(device), - ], - dim=1, + basis = torch.linalg.qr(tall).Q + spanned = basis[:, :5] @ torch.randn( + 5, 6, dtype=torch.complex128, generator=generator ) - vectors = implicit._left_singular_vectors(extended) - direct, _, _ = torch.linalg.svd(extended, full_matrices=False) - assert vectors.shape == direct.shape - assert torch.allclose( - vectors.conj().transpose(0, 1) @ vectors, - torch.eye(vectors.shape[1], dtype=vectors.dtype, device=device), - atol=1e-10, - ) - span = vectors[:, :12] @ vectors[:, :12].conj().transpose(0, 1) - assert torch.allclose(span @ basis, basis, atol=1e-10) + return basis.to(device), torch.cat([basis, spanned], dim=1).to(device) + + +def _assert_strongest_vectors_span(vectors, basis): + assert vectors.shape == (basis.shape[0], 18) + gram = vectors.conj().transpose(0, 1) @ vectors + assert torch.allclose(gram, torch.eye(18, dtype=gram.dtype, device=gram.device)) + strongest = vectors[:, :12] + projected = strongest @ (strongest.conj().transpose(0, 1) @ basis) + assert torch.allclose(projected, basis, atol=1e-10) + + +def test_an_extended_basis_keeps_its_span_in_its_strongest_singular_vectors(device): + basis, extended = _extended_basis(device) + _assert_strongest_vectors_span(implicit._left_singular_vectors(extended), basis) + + +def test_a_failed_svd_still_gives_the_strongest_singular_vectors(device, monkeypatch): + def unconverged(*args, **kwargs): + raise torch.linalg.LinAlgError("linalg.svd: The algorithm failed to converge") + + basis, extended = _extended_basis(device) + monkeypatch.setattr(torch.linalg, "svd", unconverged) + _assert_strongest_vectors_span(implicit._left_singular_vectors(extended), basis)