Repository navigation
Add OffDiagonal, returned when slicing Diagonal fills and RectDiagonal - #454
Merged
Merged
Conversation
…gonal `OffDiagonal(band, k, axes)` is a matrix whose only nonzero entries lie on its `k`-th diagonal, stored as `band`. Indexing a `Diagonal` of an `AbstractFill`, any `RectDiagonal`, or an `OffDiagonal` by unit ranges or colons now returns an `OffDiagonal` instead of a dense `Matrix`, so e.g. `Eye(5)[2:4, 1:3]` stays lazy and allocation-free. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #454 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 10 11 +1
Lines 1259 1316 +57
=========================================
+ Hits 1259 1316 +57 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Covers the fallback to the generic getindex for indices whose axes are not Base.OneTo, which returns a static matrix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
Author
|
The motivation for this is so that eg The only potential downside is if someone expects slicing to make a mutable array. But we already break that assumption for |
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.
Adds
FillArrays.OffDiagonal, a matrix whose only nonzero entries lie on a single diagonal:OffDiagonal(band, k, axes)storesbandon thek-th diagonal, using the same convention asdiag/diagind(k > 0is above the diagonal).length(band)must match the length of that diagonal, and may be zero if the diagonal lies outside the matrix.OffDiagonal(band, k)builds the squarelength(band) + abs(k)matrix.Slicing
Indexing these types by unit ranges or
:now returns anOffDiagonalinstead of a denseMatrix:Diagonal{<:Number,<:AbstractFillVector}(including squareEye)RectDiagonal(including rectangularEye)OffDiagonalitselfA slice of any of these matrices has nonzeros on at most one diagonal, so the result needs only a new band offset and a slice of the band. Lazy bands stay lazy, and the slice doesn't allocate:
What still takes the existing path:
Diagonals not backed by a fill, such asDiagonal([1, 2, 3])[1:2, 1:2], still return aMatrix.getindex.Diagonalfills with non-Numbereltypes (block diagonals) are left alone, becauseOffDiagonalfills off-band entries withzero(T).OffDiagonalalso getssetindex!(on the band only),diag,⋅printing for zeros, and hashing alongsideRectDiagonal. LikeRectDiagonal, it isn't exported.The version is bumped to 1.18.0, since these slices now return a different type.
Tests
The new testset checks every contiguous sub-range of a set of
Eye,Diagonalfill,RectDiagonalandOffDiagonalmatrices against dense slicing: square and rectangular, Vector and Fill bands, sub- and superdiagonals, and empty bands.🤖 Generated with Claude Code