Skip to content

Efficient sparse of RectDiagonal and OffDiagonal - #459

Open
dlfivefifty wants to merge 2 commits into
masterfrom
sparse-offdiagonal
Open

dlfivefifty wants to merge 2 commits into
masterfrom
sparse-offdiagonal

Conversation

@dlfivefifty

Copy link
Copy Markdown
Member

sparse(A) / SparseMatrixCSC(A) for A a RectDiagonal (with any band, not only fills), an OffDiagonal, or a Diagonal of a fill now builds colptr/rowval/nzval directly from the band. It runs in O(n + length(band)) instead of using SparseArrays' generic AbstractMatrix fallback, which visits all m×n entries.

julia> A = FillArrays.OffDiagonal(rand(5000), 1);

julia> @time sparse(A);   # master: 0.0198 s, 30 allocations
  0.000016 seconds (10 allocations: 144.234 KiB)
  • This replaces the RectOrDiagonalFill method, which built the matrix from getindex_value(v)*I and so only covered constant bands. It now uses one method for Union{Diagonal{<:Any,<:AbstractFillVector}, RectDiagonal, OffDiagonal}, built on the _band/_bandindex/_bandlength helpers from src/offdiagonal.jl.
  • As for SparseMatrixCSC(::Diagonal) in SparseArrays, zeros in the band are not stored (uses SparseArrays._isnotzero). The old method also dropped them for a Zeros band, so this keeps the existing behaviour.
  • SparseMatrixCSC{Tv}(A) keeps using eltype(axes(A,1)) as the index type, as before.
  • A RectDiagonal whose diag is longer than min(m, n) (e.g. RectDiagonal([1,2,3], 3, 1)) only stores the visible entries.

Tests run testsparsediag on a range of RectDiagonal/OffDiagonals: super- and sub-diagonals, rectangular, empty bands, bands containing zeros, Fill/Zeros bands, and slices of Eye. They also check that the CSC structure is identical to sparse(Matrix(A)). The full test suite passes on Julia 1.10 and 1.13.

🤖 Generated with Claude Code

`SparseMatrixCSC` / `sparse` of a `RectDiagonal`, `OffDiagonal` or `Diagonal`
of a fill now builds the CSC arrays directly from the band, instead of going
through the generic fallback that visits every entry. As for `Diagonal`,
zeros in the band are not stored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3f8b7e9) to head (fd8331b).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #459   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           11        11           
  Lines         1316      1362   +46     
=========================================
+ Hits          1316      1362   +46     

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

It was previously only covered through the old sparse conversion of
RectDiagonal fills.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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