Skip to content

sparse: in-place refactor! through nonzeros and refactor!(f) - #231

Draft
klamike wants to merge 9 commits into
JuliaLinearAlgebra:masterfrom
klamike:sparse-symmetric-inplace
Draft

klamike wants to merge 9 commits into
JuliaLinearAlgebra:masterfrom
klamike:sparse-symmetric-inplace

Conversation

@klamike

@klamike klamike commented Nov 14, 2026 •

Copy link
Copy Markdown

Stacked on #230 (check = false), which is stacked on #228 (factorization options); merge them in that order. Until then, this PR's diff includes both. This PR's own change is the last commit.

The one-triangle Symmetric/Hermitian constructor that was part of this PR is now its own PR, #232.

  • nonzeros(A::AASparseMatrix) returns the matrix's own value storage, which can be updated in place. For a symmetric matrix stored as one triangle, the values are those of that triangle, in the order of nonzeros(tril(S)) (or triu).
  • refactor!(f), without a matrix, refactorizes from the current values of the matrix f holds: the one it was built from, or the last one passed to refactor!. It takes the same options as refactor!(f, A).
A = AA.AASparseMatrix(Symmetric(K, :L))     # with #232; or AASparseMatrix(K) for LU
f = AA.factor(A, :ldlt)
for k in 1:iters
    copyto!(nonzeros(A), new_values(k))     # same pattern, in the order of nonzeros(tril(K))
    AA.refactor!(f)
    AA.solve!(f, rhs)
end

Tests

The new tests cover:

  • the stored values of symmetric (given explicitly through the attributes, so independent of sparse: symmetric storage from Symmetric/Hermitian, no structure detection in AASparseMatrix #232) and unsymmetric matrices, and that transpose(A) shares them;
  • in-place updates followed by refactor!(f), through LDLᵀ and LU, with options, and after refactor!(f, B);
  • complex Hermitian, through Cholesky. libSparse's complex Hermitian LDLᵀ refactorization can crash, which is why the existing test of it runs in a subprocess.

factor, factor! and refactor! take the keywords order, scaling,
pivot_tolerance and zero_tolerance, and so do the LinearAlgebra spellings
(lu/cholesky/qr/ldlt on AASparseMatrix, lu!/cholesky!/ldlt!). The options are
stored on the AAFactorization once libSparse has applied them, so later calls
reuse them. The defaults are libSparse's, as before.

libSparse's in-place refactorization ignores the numeric options it is
given and keeps those of the original factorization (a zero tolerance of
1e3 changes nothing). When the numeric options change, refactor! therefore
computes a new numeric factorization from the existing symbolic one, through
the public SparseFactor(SparseOpaqueSymbolicFactorization, SparseMatrix,
SparseNumericFactorOptions) entry point, which this wraps.

Options are validated: pivot_tolerance must be in [0, 0.5] (libSparse
clamps it silently), zero_tolerance nonnegative. factor! on a computed
factorization does nothing, so options passed to it throw; refactor!
rejects order and SparseScalingHungarianScalingAndOrdering, which belong to
the symbolic factorization.

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

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #231      +/-   ##
==========================================
+ Coverage   91.73%   91.79%   +0.05%     
==========================================
  Files          11       11              
  Lines        4697     4727      +30     
==========================================
+ Hits         4309     4339      +30     
  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.

…options

With a zero tolerance above every pivot, libSparse reports a different
number of zero pivots on macOS 15 on Intel (60 for a 30x30 matrix, and
(1, 18, 1) for a complex one) than on Apple silicon (all of them), so the
tests check that there are zero pivots rather than how many.

Also test that a refactorization with new options that fails throws and
leaves the factorization as it was.

Assisted-by: Claude Code:claude-opus-5-5
@klamike
klamike force-pushed the sparse-symmetric-inplace branch 3 times, most recently from b25980f to 3bdb34b Compare November 14, 2026 22:14
@klamike klamike changed the title sparse: symmetric matrices from one triangle, in-place refactor! sparse: in-place refactor! through nonzeros and refactor!(f) Nov 14, 2026
The workspace variants of SparseSolve are wrapped since JuliaLinearAlgebra#199, with a
caller-owned buffer (solve_workspace_size).

Assisted-by: Claude Code:claude-opus-5-5
@klamike
klamike force-pushed the sparse-symmetric-inplace branch from 3bdb34b to 86c824c Compare November 15, 2026 20:58
Assisted-by: Claude Code:claude-opus-5-5
factor, factor! and refactor! take check::Bool = true, as LinearAlgebra's
factorizations do, and so do the LinearAlgebra spellings. With
check = false, a failed numeric factorization is kept instead of thrown,
issuccess(f) reports the outcome, and refactor! reuses its symbolic
factorization once the values change. Solving with a failed factorization,
or calling factor! on it with check = true, throws. Argument errors
(including libSparse's parameter errors) throw regardless of check.

Only Cholesky, unpivoted LDLT and QR detect failure; the pivoting LDLT
and LU factorizations succeed on singular matrices, as documented.

refactor! also used to throw on a failed refactorization while keeping the
previous Ok status, although libSparse had already overwritten the numeric
factors, so a caller that caught the error could solve with garbage; it now
stores the failed status. lu!/cholesky!/ldlt! check the kind of a failed
factorization too.

Assisted-by: Claude Code:claude-opus-5-5
After a failed numeric factorization, libSparse's record of the numeric
options is not valid (numeric_options read garbage), and an in-place
refactorization does not restore it, although that refactorization uses the
recorded options. refactor! now refactorizes in place only from a successful
factorization; recovering from a failed one computes a new numeric
factorization from the symbolic one, with the stored options.

Also:
- Drop the parameter-error branch after SparseFactor(symbolic, ...):
  libSparse reports parameter errors through the reportError callback,
  which throws, so it could not be reached.
- Don't @ref LinearAlgebra.issuccess from docstrings: it is not in the
  manual, which failed the documentation build.

Assisted-by: Claude Code:claude-opus-5-5
Assisted-by: Claude Code:claude-opus-5-5
Assisted-by: Claude Code:claude-opus-5-5
nonzeros(A::AASparseMatrix) returns the matrix's own value storage, and
refactor!(f) without a matrix refactorizes from the current values of the matrix
f holds.

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

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