Skip to content

Nested slicing operations for merging blocks #466

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 4 commits into from
Jul 10, 2025

Conversation

mtfishman
Copy link
Collaborator

@mtfishman mtfishman commented Jun 25, 2025

Fixes #359.

This introduces syntax for merging blocks by indexing with nested vectors of Block{1}, BlockRange{1}, BlockIndexRange{1}, etc. The implementation is very similar to #445.

As an example:

julia> using BlockArrays

julia> A = BlockArray(randn(8, 8), [2, 2, 2, 2], [2, 2, 2, 2])
4×4-blocked 8×8 BlockMatrix{Float64}:
  0.163738   -0.859246-0.399256  -0.143974-0.812783   0.4698770.0631785   1.56266  
 -0.209473    1.237010.479233   0.219572-0.548844   1.3630.795119   -0.169581 
 ───────────────────────┼─────────────────────────┼────────────────────────┼────────────────────────
  1.34493    -0.8460041.32566    0.4700430.650647   0.617614-0.0926643   0.550304 
  1.20779    -0.5281431.09621   -0.0868135-0.178719  -0.1054560.243687   -1.31676  
 ───────────────────────┼─────────────────────────┼────────────────────────┼────────────────────────
  1.201       1.486910.264342   0.696739-0.86067   -0.608839-0.082433   -0.0931981
  0.674171   -1.234940.124443   1.949830.929236  -1.62257-0.693029   -0.0212493
 ───────────────────────┼─────────────────────────┼────────────────────────┼────────────────────────
  0.0174839   1.65394-0.851706  -1.237221.63834    0.1586631.27422    -2.37923  
  1.28377     0.3640390.564611  -1.346-0.619627   0.2409330.270664   -0.593572 

julia> A[[[Block(1), Block(2)], [Block(3), Block(4)]], [[Block(1), Block(2)], [Block(3), Block(4)]]]
2×2-blocked 8×8 BlockedMatrix{Float64}:
  0.163738   -0.859246  -0.399256  -0.143974-0.812783   0.469877   0.0631785   1.56266  
 -0.209473    1.23701    0.479233   0.219572-0.548844   1.363      0.795119   -0.169581 
  1.34493    -0.846004   1.32566    0.4700430.650647   0.617614  -0.0926643   0.550304 
  1.20779    -0.528143   1.09621   -0.0868135-0.178719  -0.105456   0.243687   -1.31676  
 ──────────────────────────────────────────────┼──────────────────────────────────────────────
  1.201       1.48691    0.264342   0.696739-0.86067   -0.608839  -0.082433   -0.0931981
  0.674171   -1.23494    0.124443   1.949830.929236  -1.62257   -0.693029   -0.0212493
  0.0174839   1.65394   -0.851706  -1.237221.63834    0.158663   1.27422    -2.37923  
  1.28377     0.364039   0.564611  -1.346-0.619627   0.240933   0.270664   -0.593572 

julia> A[[[Block(1)[2:2], Block(2)[2:2]], [Block(3)[2:2], Block(4)[2:2]]], [[Block(1)[2:2], Block(2)[2:2]], [Block(3)[2:2], Block(4)[2:2]]]]
2×2-blocked 4×4 BlockedMatrix{Float64}:
  1.23701    0.2195721.363     -0.169581 
 -0.528143  -0.0868135-0.105456  -1.31676  
 ───────────────────────┼───────────────────────
 -1.23494    1.94983-1.62257   -0.0212493
  0.364039  -1.3460.240933  -0.593572 

It feels unfortunate that the implementation has to be done in this way by catching the various cases manually with dispatch, but I can't think a better way (besides introducing a custom slicing operation).

Copy link

codecov bot commented Jun 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.95%. Comparing base (cc32221) to head (3b0ce73).
Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #466      +/-   ##
==========================================
+ Coverage   93.90%   93.95%   +0.04%     
==========================================
  Files          19       19              
  Lines        1707     1721      +14     
==========================================
+ Hits         1603     1617      +14     
  Misses        104      104              

☔ 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.

@dlfivefifty dlfivefifty merged commit a4a0b93 into JuliaArrays:master Jul 10, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

More convenient syntax for merging blocks
2 participants