Repository navigation
Propagate NaN and Inf to the unstored entries when scaling a sparse array by a Diagonal - #1129
Closed
ViralBShah wants to merge 2 commits into
Closed
ViralBShah wants to merge 2 commits into
ViralBShah wants to merge 2 commits into
Conversation
…, `ldiv!` and `rdiv!` Scaling a sparse matrix by a number or a `Diagonal` into a destination gave it the pattern of the source before converting the values to its eltype, so an `InexactError` left the destination with the new pattern and uninitialized values. When the conversion can fail, the values are now converted first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rray by a `Diagonal` Products, solves and in-place scaling of a sparse matrix or vector by a `Diagonal` scaled only the stored entries, so an unstored zero times `Inf` or `NaN` stayed zero where dense gives `NaN`. The affected columns or rows are now stored in full first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## vs/scale-into-dest-validate #1129 +/- ##
===============================================================
- Coverage 94.69% 94.63% -0.07%
===============================================================
Files 16 16
Lines 10318 10395 +77
===============================================================
+ Hits 9771 9837 +66
- Misses 547 558 +11 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #1121.
Scaling a sparse array by a
Diagonalscaled only its stored entries, so an entry that is not stored stayed zero where dense computes0 * InforNaN \ 0, which isNaN. #1101 fixed this for a scalar; this does it for aDiagonal, in*,\,/,rmul!,lmul!,ldiv!,rdiv!, and 3- and 5-argumentmul!, for matrices, their adjoints and transposes, andDiagonal * xandldiv!(D, x)for sparse vectors.Before
After
_fullscaledfinds the columns (or rows) in whichop(0, d[j])(orop(d[i], 0), withαfolded in formul!) is not zero and that are not already stored in full. When there are any,_storeinfullstores them in full, the new entries as stored zeros, and the unchanged kernels then scale those zeros as dense does. A fixed-pattern destination goes through_copyto_fixed!, so it throws with the destination unchanged when the result needs an entry it does not store, and a result too large for the index type throws before anything is written. When every diagonal entry scales zero to zero, the only extra cost is oneopper diagonal entry, so theOpCounttests now countnnz + nproducts. An eltype without azerokeeps scaling only its stored entries, as in #1101. I swept sparse against dense locally, withInf,-InfandNaN(and complex ones) in the first or last diagonal entry, real and complexAwith an empty row and a stored zero, and α, β in {(2, 0), (2, 3), (1, 1), (0, 2), (Inf, 0.5), (NaN, 0.5)}. Every result matches dense apart from the sign of a zero.Timings at nnz = 10^6 (n = 10^5, finite
D, minimum of 30 runs; allocations are unchanged):A * DD * Armul!(A, D)lmul!(D, A)mul!(C, A, D, 2, 0)D * A * DPotential breakage: this changes what working code returns. A product or solve with a
Diagonalthat holdsInforNaNnow givesNaNin the affected columns or rows, as dense does, and stores those columns or rows in full. In-place scaling of a fixed-pattern array whose pattern does not hold them now throws anArgumentError. Themul!docstring gains a sentence on this.Left out: a non-finite
βin 5-argumentmul!, which scales the zeros ofCrather than those ofA. A denseCgivesNaNthere too.🤖 Generated with Claude Code