Skip to content

sparse: symmetric storage from Symmetric/Hermitian, no structure detection in AASparseMatrix - #232

Draft
klamike wants to merge 2 commits into
JuliaLinearAlgebra:masterfrom
klamike:sparse-symmetric-wrappers
Draft

klamike wants to merge 2 commits into
JuliaLinearAlgebra:masterfrom
klamike:sparse-symmetric-wrappers

Conversation

@klamike

@klamike klamike commented Nov 14, 2026 •

Copy link
Copy Markdown

Breaking, so the version is bumped to 0.9.0. Based on master, independent of the other open sparse PRs. It touches the same factor! code and the end of test/sparse_tests.jl as #229 and #231, so whichever merges second needs a small rebase; the conflicts are mechanical, and merged with #231 (and so the whole #228 → #230 → #231 stack) the sparse tests pass (1335/1335) and the docs build.

Symmetric and Hermitian matrices from one triangle

AASparseMatrix(Symmetric(S, :L)) (or :U, or Hermitian) stores only that triangle, with the symmetric (Hermitian) attributes, as libSparse expects for Cholesky and LDLᵀ. It doesn't test the matrix for symmetry, so a lower triangle can be wrapped as is, in one copy. Until now, this needed the undocumented ATT_* flags.

  • As with Symmetric, the other triangle of the parent is ignored.
  • As with Hermitian, so is the imaginary part of the diagonal. Otherwise multiplication would use it while the factorization ignored it.
  • Other element types are converted, as in AASparseMatrix(::SparseMatrixCSC).
L = tril(S)                                 # the lower triangle of a symmetric matrix
A = AA.AASparseMatrix(Symmetric(L, :L))
f = AA.factor(A, :ldlt)

factor(A) picks LDLᵀ for a complex symmetric (not Hermitian) matrix stored as one triangle. Until now it picked LU, which libSparse computes incorrectly on that storage (#229).

AASparseMatrix(::SparseMatrixCSC) stores the matrix as it is

Until now, the constructor tested every matrix (ishermitian, istril, istriu), stored a symmetric (Hermitian) one as its lower triangle, and tagged a triangular one. In Julia, the structure comes from the type (Symmetric, Hermitian), and only factorize-style functions test the values. The detection also:

  • made the storage depend on the values: two matrices with the same pattern, one of them symmetric by chance, had different stored nonzeros, so refactor! from one to the other failed;
  • put LU on symmetric storage, which libSparse computes incorrectly (sparse: fix LU factorizations of symmetric matrices #229), whenever a symmetric matrix was factored with lu;
  • read every value on every construction.

Now the constructor stores the matrix as given, untagged unless attributes are passed. Symmetric storage comes from Symmetric/Hermitian or the attributes.

The SparseMatrixCSC conveniences (factor, solve, AAFactorization, and refactor!) keep testing the matrix, like factorize. They store a Hermitian (or real symmetric) matrix as its lower triangle only for the factorizations that use it: Cholesky, LDLᵀ, and :auto, which then picks Cholesky. LU and QR get the full storage, so factor(S, :lu) on a symmetric S is now correct without #229. refactor!(f, S) stores S as the matrix f holds is stored.

Code that relied on the detection, such as cholesky(AASparseMatrix(S)) for a symmetric S, now needs cholesky(AASparseMatrix(Symmetric(S, :L))), or factor(S, :cholesky). The docs say so in a compat note, and the docs examples and existing tests that relied on it are updated.

Tests

The new tests cover:

  • both triangles, Hermitian, Int32 indices and element-type conversion, checked by round trip, by the stored values, by multiplication, and by solving;
  • the ignored parent triangle;
  • the plain constructor storing the matrix as it is, and tagging only through attributes;
  • the storage chosen by factor, solve and AAFactorization for a SparseMatrixCSC: one triangle for Cholesky, LDLᵀ and :auto, the full matrix for LU and QR; refactor! with a SparseMatrixCSC on both triangles and on LU;
  • complex Hermitian through Cholesky (:auto) and LDLᵀ, and a non-real Hermitian diagonal;
  • complex symmetric matrices (macOS 26+): LDLᵀ on one triangle through :auto, and through the SparseMatrixCSC conveniences, LU on the full storage for :auto and one triangle for :ldlt.

AASparseMatrix(Symmetric(S, uplo)) and AASparseMatrix(Hermitian(S, uplo)) store
only that triangle, with the symmetric (Hermitian) attributes. factor(A) picks
LDLT for a complex symmetric (not Hermitian) matrix stored as one triangle, as
libSparse's LU does not read that storage correctly.

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.69%. Comparing base (c819416) to head (c57ba06).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #232      +/-   ##
==========================================
- Coverage   91.73%   91.69%   -0.05%     
==========================================
  Files          11       11              
  Lines        4697     4720      +23     
==========================================
+ Hits         4309     4328      +19     
- Misses        388      392       +4     

☔ 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.

The constructor no longer tests the matrix for symmetric or triangular
structure. Symmetric storage comes from Symmetric/Hermitian or the attributes.
Like factorize, the SparseMatrixCSC conveniences (factor, solve,
AAFactorization, refactor!) still test the matrix, and store a Hermitian (or
real symmetric) one as its lower triangle for Cholesky, LDLT and :auto only,
so LU and QR get the full storage. Breaking, so the version is 0.9.0.

Assisted-by: Claude Code:claude-opus-5-5
@klamike klamike changed the title sparse: symmetric and Hermitian matrices from one triangle sparse: symmetric storage from Symmetric/Hermitian, no structure detection in AASparseMatrix Nov 15, 2026

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