Add efficient O(nnz) isdiag for sparse matrices#672
Add efficient O(nnz) isdiag for sparse matrices#672ChrisRackauckas-Claude wants to merge 3 commits intoJuliaSparse:mainfrom
Conversation
Add a specialized `isdiag` method for `AbstractSparseMatrixCSC` that traverses the CSC structure directly in O(nnz) time, compared to the generic fallback which is O(n²) for an n×n matrix. This addresses performance issues in packages like OrdinaryDiffEq.jl where `isdiag` checks on large sparse mass matrices (e.g., 259k variables in DAE systems) caused initialization times of ~60 seconds. With this optimization, initialization drops to <1ms. Reference: SciML/OrdinaryDiffEq.jl#3011 Co-Authored-By: Chris Rackauckas <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #672 +/- ##
==========================================
- Coverage 84.15% 84.11% -0.04%
==========================================
Files 12 12
Lines 9301 9313 +12
==========================================
+ Hits 7827 7834 +7
- Misses 1474 1479 +5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
src/sparsematrix.jl
Outdated
| """ | ||
| function isdiag(A::AbstractSparseMatrixCSC) | ||
| m, n = size(A) | ||
| m != n && return false |
There was a problem hiding this comment.
This seems to be at odds with the generic implementation:
julia> isdiag(diagm(4,5,0 => ones(4)))
true| m != n && return false |
test/sparsematrix_ops.jl
Outdated
| @test !isdiag(sparse(Bidiagonal(1:4, 1:3, :L))) | ||
| @test !isdiag(sparse([1 2; 3 4])) | ||
|
|
||
| # Non-square matrices should return false |
The generic isdiag implementation in LinearAlgebra returns true for rectangular diagonal matrices (e.g., `diagm(4,5,0 => ones(4))`). This commit removes the square matrix check to be consistent with the generic implementation. Updated tests to verify: - Non-square diagonal matrices return true - Non-square non-diagonal matrices return false Co-Authored-By: Chris Rackauckas <[email protected]> Co-Authored-By: Claude Opus 4.5 <[email protected]>
|
Thanks for catching this @dkarrasch! I've removed the square matrix check so that Updated the tests to verify:
|
|
The documentation string is unnecessary, there are no behavioral changes here. |
Co-authored-by: Christopher Rackauckas <[email protected]>
|
Ah it only responds to reviews good to know :) |
|
I decided to make that change myself. You humans always try to take credit for my superior work. |
Summary
Add a specialized
isdiagmethod forAbstractSparseMatrixCSCthat traverses the CSC structure directly in O(nnz) time, compared to the generic fallback which is O(n²) for an n×n matrix.Changes:
isdiagfrom LinearAlgebraisdiag(A::AbstractSparseMatrixCSC)that checks diagonal property by traversing the sparse structureisdiagon sparse matricesMotivation
This addresses performance issues in packages like OrdinaryDiffEq.jl where
isdiagchecks on large sparse mass matrices caused severe initialization delays. For large DAE systems (e.g., 259k variables), this reduces initialization time from ~60 seconds to <1ms.Reference: SciML/OrdinaryDiffEq.jl#3011
Benchmarks
The efficient implementation runs in essentially constant time since it only needs to check the stored elements.
Test plan
isdiagtests intest/sparsematrix_ops.jl:isdiag🤖 Generated with Claude Code