Skip to content

Bounds-checking in triangular indexing branches #1305

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

Merged
merged 14 commits into from
Aug 21, 2025

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Apr 23, 2025

After this PR, calls like the following would throw:

julia> U = UpperTriangular([1 2; 3 4])
2×2 UpperTriangular{Int64, Matrix{Int64}}:
 1  2
   4

julia> U[1,0]
0

Currently, there's no bounds-checking on the indices if they don't correspond to stored values.

Also, this PR checks for compatible types in assigning to the diagonal of unit triangular matrices. This fixes issues like, e.g.:

julia> A = @SMatrix [1 2; 3 4]
2×2 SMatrix{2, 2, Int64, 4} with indices SOneTo(2)×SOneTo(2):
 1  2
 3  4

julia> M = fill(A, 2, 2)
2×2 Matrix{SMatrix{2, 2, Int64, 4}}:
 [1 2; 3 4]  [1 2; 3 4]
 [1 2; 3 4]  [1 2; 3 4]

julia> U = UnitUpperTriangular(M)
2×2 UnitUpperTriangular{SMatrix{2, 2, Int64, 4}, Matrix{SMatrix{2, 2, Int64, 4}}}:
 [1 0; 0 1]  [1 2; 3 4]
            [1 0; 0 1]

julia> U[1,1] = 1
1

After this,

julia> U[1,1] = 1
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type SMatrix{2, 2, Int64, 4}

However, this depends on JuliaLang/julia#58209 for the SizedArrays tests to pass, as oneunit currently doesn't work with a SizedArray.

@jishnub jishnub force-pushed the jishnub/unit_tri_setindex branch 2 times, most recently from 6648614 to 9159fbb Compare April 28, 2025 05:51
Copy link

codecov bot commented Apr 28, 2025

Codecov Report

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

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1305   +/-   ##
=======================================
  Coverage   93.87%   93.88%           
=======================================
  Files          34       34           
  Lines       15857    15879   +22     
=======================================
+ Hits        14886    14908   +22     
  Misses        971      971           

☔ View full report in Codecov by Sentry.
📢 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.

@jishnub jishnub added backport 1.11 Change should be backported to the 1.11 release backport 1.10 Change should be backported to the 1.10 release backport 1.12 Change should be backported to release-1.12 labels May 11, 2025
@jishnub jishnub force-pushed the jishnub/unit_tri_setindex branch 2 times, most recently from 32ff7f4 to 0b47f18 Compare May 11, 2025 17:06
@jishnub jishnub mentioned this pull request May 12, 2025
27 tasks
jishnub added a commit that referenced this pull request May 26, 2025
Backported PRs:
- [x] #1209 <!-- Remove `LinearAlgebra` qualifications in `cholesky.jl`
-->
- [x] #1230 <!-- Avoid materializing `diag` in `Diagonal` `kron` -->
- [x] #1240 <!-- Reduce `stable_muladdmul` branches in `generic
matvecmul!` -->
- [x] #1247 <!-- fix dispatch to herk -->
- [x] #1255 <!-- use smaller matrix size in `peakflops` on 32-bit -->
- [x] #1310 <!-- Only `@noinline` error path in `matmul_size_check` -->
- [x] #1267 <!-- Refine column ranges in `_isbanded_impl` -->
- [x] #1320 <!-- Copy matrices in `triu`/`tril` if no zero exists for
the `eltype` -->
- [x] #1324 <!-- Fix empty `Tridiagonal` broadcast -->
- [x] #1327 <!-- `iszero` check in hessenberg setindex -->
- [x] #1326 <!-- Fix multiplication with empty `HessenbergQ` -->
- [x] #1332 <!-- Unwrap triangular matrices in broadcast -->
- [x] #1337 <!-- Change `1:size` to `axes` in bidiag mul -->
- [x] #1342 <!-- `Char` uplo in `Bidiagonal` constructor -->
- [x] #1344 <!-- Update the docstring of ldiv! -->
- [x] #1335 <!-- Test: prune old LA based on ENV variable -->
- [x] #1346 <!-- Fix scaling unit triangular matrices -->
- [x] #1355 <!-- Add compat notice for `diagview` -->
- [x] #1349 <!-- Prune `LinearAlgebra` module in ambiguity test -->

Contains multiple commits, manual intervention needed:
- [x] #1238 <!-- Ensure positive-definite matrix in lapack posv test -->
- [x] #1298 <!-- Add `diagm` example -->
- [x] #1312 <!-- WIP: Try use method deletion instead of custom sysimage
-->
- [x] #1333 <!-- Make `fillstored!` public -->
- [x] #1331 <!-- Document SingularException throw for
inv(::AbstractMatrix) -->
- [x] #1350 <!-- Fix copy for partly initialized unit triangular -->

Non-merged PRs with backport label:
- [x] #1352 <!-- log for dense diagonal matrix with negative elements
-->
- [ ] #1305 <!-- Bounds-checking in triangular indexing branches -->

---------

Co-authored-by: Mateus Araújo <[email protected]>
Co-authored-by: Jeff Bezanson <[email protected]>
Co-authored-by: Steven G. Johnson <[email protected]>
Co-authored-by: WalterMadelim <[email protected]>
Co-authored-by: Kristoffer Carlsson <[email protected]>
Co-authored-by: Daniel Karrasch <[email protected]>
Co-authored-by: Michael Abbott <[email protected]>
@jishnub jishnub mentioned this pull request Jun 5, 2025
4 tasks
jishnub added a commit that referenced this pull request Jun 13, 2025
Backported PRs:
- [x] #1364 <!-- clarify eigen docstring -->
- [x] #1363 <!-- Change default symmetriceigen algorithm back to
RobustRepresentations -->

Non-merged PRs with backport label:
- [ ] #1365 <!-- Add docstring to the `rank(::QRPivoted)` method -->
- [ ] #1305 <!-- Bounds-checking in triangular indexing branches -->
@jishnub jishnub force-pushed the jishnub/unit_tri_setindex branch from 083216e to 763a99d Compare June 19, 2025 07:23
@dkarrasch
Copy link
Member

This looks like ready to go, doesn't it? Should then be included in #1420.

@dkarrasch dkarrasch merged commit bcc4638 into master Aug 21, 2025
4 checks passed
@dkarrasch dkarrasch deleted the jishnub/unit_tri_setindex branch August 21, 2025 12:12
@dkarrasch dkarrasch mentioned this pull request Aug 21, 2025
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 1.10 Change should be backported to the 1.10 release backport 1.11 Change should be backported to the 1.11 release backport 1.12 Change should be backported to release-1.12
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants