Skip to content

sparse: fix LU factorizations of symmetric matrices - #229

Draft
klamike wants to merge 1 commit into
JuliaLinearAlgebra:masterfrom
klamike:sparse-lu-symmetric-storage
Draft

klamike wants to merge 1 commit into
JuliaLinearAlgebra:masterfrom
klamike:sparse-lu-symmetric-storage

Conversation

@klamike

@klamike klamike commented Nov 14, 2026 •

Copy link
Copy Markdown

AASparseMatrix stores a symmetric (Hermitian) SparseMatrixCSC as one triangle, with the symmetric attributes. libSparse's LU factorizations don't read that storage correctly, so factor(K, :lu) on a symmetric K returned a factorization with wrong solutions and no error:

n = 30
K = spdiagm(-1 => ones(n-1), 0 => [(-1)^i * (2 + i/n) for i in 1:n], 1 => ones(n-1),
            -5 => fill(0.3, n-5), 5 => fill(0.3, n-5))
norm(K * AA.solve(AA.factor(K, :lu), b) - b)    # 1.55, for :lu, LUSPP and LUUnpivoted alike
norm(K * AA.solve(AA.factor(K, :qr), b) - b)    # 1e-15; Cholesky and LDLT are correct too

This includes positive definite matrices (residual 1.1 for a tridiagonal one) and lu(AASparseMatrix(K)).

An LU factorization of a matrix stored as one triangle now factorizes a copy with both triangles stored, and refactor! and lu! expand the new matrix the same way.

Tests

The tests cover real and complex Hermitian matrices, indefinite and positive definite:

  • :lu, LUSPP and LUUnpivoted through factor;
  • lu(::AASparseMatrix);
  • refactor! and lu!;
  • symmetric storage given through the ATT_* attributes.

They fail on master.

This PR is independent of the factorization-options stack (#228, #230, #231) and of #232 (symmetric matrices from one triangle). Whichever merges second needs a rebase: they change the same factor! code and the end of the test file. The conflicts are mechanical; merged with #231 (and so the whole stack) the sparse tests pass (1262/1262), and so they do merged with #232 (1118/1118).

AASparseMatrix stores a symmetric (Hermitian) SparseMatrixCSC as one
triangle with the symmetric attributes. libSparse's LU factorizations do not
read that storage correctly: factor(K, :lu) on a symmetric K, even a
positive definite one, returned a factorization whose solutions are wrong
(residual 1.5 on a 30x30 matrix, for :lu, LUSPP and LUUnpivoted alike),
without any error. QR, Cholesky and LDLT are correct.

An LU factorization of a matrix stored as one triangle now factorizes a
copy with both triangles stored, and refactor!/lu! expand the new matrix the
same way.

Assisted-by: Claude Code:claude-opus-5-5
@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.75%. Comparing base (c819416) to head (f373e5f).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #229      +/-   ##
==========================================
+ Coverage   91.73%   91.75%   +0.01%     
==========================================
  Files          11       11              
  Lines        4697     4706       +9     
==========================================
+ Hits         4309     4318       +9     
  Misses        388      388              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@klamike

klamike commented Nov 14, 2026

Copy link
Copy Markdown
Author

Should probably just get rid of the detection in AASparseMatrix constructor and rely on Symmetric/Hermitian tags. But that is breaking.

This branch has not been deployed

No deployments
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