-
Notifications
You must be signed in to change notification settings - Fork 69
Add efficient O(nnz) isdiag for sparse matrices #672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add efficient O(nnz) isdiag for sparse matrices #672
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 <accounts@chrisrackauckas.com>
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above.
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 <accounts@chrisrackauckas.com> Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
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 <accounts@chrisrackauckas.com>
|
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