From 68401716f372d4a708c5c7835d2f6b2b797062f6 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Fri, 27 Jun 2025 15:15:31 -0400 Subject: [PATCH 1/3] Merge BlockIndexRanges --- Project.toml | 2 +- src/BlockArrays.jl | 2 +- src/blockindices.jl | 4 ++++ test/test_blockindices.jl | 1 + 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index edf66627..e455ea42 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "BlockArrays" uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" -version = "1.6.3" +version = "1.6.4" [deps] ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" diff --git a/src/BlockArrays.jl b/src/BlockArrays.jl index 15341192..2820b6d6 100644 --- a/src/BlockArrays.jl +++ b/src/BlockArrays.jl @@ -4,7 +4,7 @@ using LinearAlgebra, ArrayLayouts, FillArrays # AbstractBlockArray interface exports export AbstractBlockArray, AbstractBlockMatrix, AbstractBlockVector, AbstractBlockVecOrMat export Block, getblock, getblock!, setblock!, eachblock, blocks -export blockaxes, blocksize, blocklength, blockcheckbounds, BlockBoundsError, BlockIndex +export blockaxes, blocksize, blocklength, blockcheckbounds, BlockBoundsError, BlockIndex, BlockIndexRange export blocksizes, blocklengths, blocklasts, blockfirsts, blockisequal export BlockRange, blockedrange, BlockedUnitRange, BlockedOneTo diff --git a/src/blockindices.jl b/src/blockindices.jl index 1226e8af..9d1b0459 100644 --- a/src/blockindices.jl +++ b/src/blockindices.jl @@ -201,6 +201,10 @@ BlockIndexRange BlockIndexRange(block::Block{N}, inds::Vararg{AbstractUnitRange{<:Integer},N}) where {N} = BlockIndexRange(block,inds) +function BlockIndexRange(inds::Tuple{BlockIndexRange{1},Vararg{BlockIndexRange{1}}}) + BlockIndexRange(Block(block.(inds)), map(ind -> ind.indices[1], inds)) +end + block(R::BlockIndexRange) = R.block copy(R::BlockIndexRange) = BlockIndexRange(R.block, map(copy, R.indices)) diff --git a/test/test_blockindices.jl b/test/test_blockindices.jl index b57512be..02d232e1 100644 --- a/test/test_blockindices.jl +++ b/test/test_blockindices.jl @@ -89,6 +89,7 @@ import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice @test Block(1)[1:2] == BlockIndexRange(Block(1),(1:2,)) @test Block(1,1)[1,1] == BlockIndex((1,1),(1,1)) == BlockIndex((1,1),(1,)) @test Block(1,1)[1:2,1:2] == BlockIndexRange(Block(1,1),(1:2,1:2)) + @test BlockIndexRange((Block(1)[1:2],Block(1)[1:2])) == BlockIndexRange(Block(1,1),(1:2,1:2)) @test Block(1)[1:3][1:2] == BlockIndexRange(Block(1),1:2) @test BlockIndex((2,2,2),(2,)) == BlockIndex((2,2,2),(2,1,)) == BlockIndex((2,2,2),(2,1,1)) @test BlockIndex(2,(2,)) === BlockIndex((2,),(2,)) From 535506b491698c7157883860c540263a968c42da Mon Sep 17 00:00:00 2001 From: mtfishman Date: Fri, 27 Jun 2025 17:17:41 -0400 Subject: [PATCH 2/3] Fix doctest --- src/blockbroadcast.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/blockbroadcast.jl b/src/blockbroadcast.jl index d9a892f3..91dad24f 100644 --- a/src/blockbroadcast.jl +++ b/src/blockbroadcast.jl @@ -82,7 +82,7 @@ julia> itr = SubBlockIterator(subblock_lasts, block_lasts) SubBlockIterator([1, 3, 6], [1, 3, 4, 6]) julia> collect(itr) -4-element Vector{BlockArrays.BlockIndexRange{1, Tuple{UnitRange{Int64}}, Tuple{Int64}, Int64}}: +4-element Vector{BlockIndexRange{1, Tuple{UnitRange{Int64}}, Tuple{Int64}, Int64}}: Block(1)[1:1] Block(2)[1:2] Block(3)[1:1] From 2f6a2050bf6ef9c4ae9a7cff30cb41fa7e6f7aab Mon Sep 17 00:00:00 2001 From: mtfishman Date: Tue, 1 Jul 2025 12:14:55 -0400 Subject: [PATCH 3/3] Add doctests --- src/blockindices.jl | 74 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/src/blockindices.jl b/src/blockindices.jl index b7e44a1d..d2ce8540 100644 --- a/src/blockindices.jl +++ b/src/blockindices.jl @@ -5,7 +5,18 @@ A `Block` is simply a wrapper around a set of indices or enums so that it can be indexing a `AbstractBlockArray` with a `Block` the a block at that block index will be returned instead of a single element. +It can be constructed and used to index into `BlockArrays` in the following manner: + ```jldoctest +julia> Block(1) +Block(1) + +julia> Block(1, 2) +Block(1, 2) + +julia> Block((Block(1), Block(2))) +Block(1, 2) + julia> A = BlockArray(ones(2,3), [1, 1], [2, 1]) 2×2-blocked 2×3 BlockMatrix{Float64}: 1.0 1.0 │ 1.0 @@ -119,9 +130,21 @@ end A `BlockIndex` is an index which stores a global index in two parts: the block and the offset index into the block. -It can be used to index into `BlockArrays` in the following manner: +It can be constructed and used to index into `BlockArrays` in the following manner: ```jldoctest +julia> BlockIndex((1,2), (3,4)) +Block(1, 2)[3, 4] + +julia> Block(1)[3] === BlockIndex((1), (3)) +true + +julia> Block(1,2)[3,4] === BlockIndex((1,2), (3,4)) +true + +julia> BlockIndex((Block(1)[3], Block(2)[4])) +Block(1, 2)[3, 4] + julia> arr = Array(reshape(1:25, (5,5))); julia> a = BlockedArray(arr, [3,2], [1,4]) @@ -133,10 +156,10 @@ julia> a = BlockedArray(arr, [3,2], [1,4]) 4 │ 9 14 19 24 5 │ 10 15 20 25 -julia> a[BlockIndex((1,2), (1,2))] +julia> a[Block(1,2)[1,2]] 11 -julia> a[BlockIndex((2,2), (2,3))] +julia> a[Block(2,2)[2,3]] 20 ``` """ @@ -196,7 +219,44 @@ end """ BlockIndexRange(block, startind:stopind) -represents a cartesian range inside a block. +Represents a cartesian range inside a block. + +It can be constructed and used to index into `BlockArrays` in the following manner: + +```jldoctest +julia> BlockIndexRange(Block(1,2), (2:3,3:4)) +Block(1, 2)[2:3, 3:4] + +julia> Block(1)[2:3] === BlockIndexRange(Block(1), 2:3) +true + +julia> Block(1,2)[2:3,3:4] === BlockIndexRange(Block(1,2), (2:3,3:4)) +true + +julia> BlockIndexRange((Block(1)[2:3], Block(2)[3:4])) +Block(1, 2)[2:3, 3:4] + +julia> arr = Array(reshape(1:25, (5,5))); + +julia> a = BlockedArray(arr, [3,2], [1,4]) +2×2-blocked 5×5 BlockedMatrix{Int64}: + 1 │ 6 11 16 21 + 2 │ 7 12 17 22 + 3 │ 8 13 18 23 + ───┼──────────────── + 4 │ 9 14 19 24 + 5 │ 10 15 20 25 + +julia> a[Block(1,2)[1:2,2:3]] +2×2 Matrix{Int64}: + 11 16 + 12 17 + +julia> a[Block(2,2)[1:2,3:4]] +2×2 Matrix{Int64}: + 19 24 + 20 25 +``` """ BlockIndexRange @@ -363,6 +423,12 @@ julia> BlockRange((2, 2)) |> collect # number of elements, starting at 1 julia> Block(1):Block(2) BlockRange((1:2,)) + +julia> Block.(1:2) +BlockRange((1:2,)) + +julia> BlockRange((Block.(1:2), Block.(3:4))) +BlockRange((1:2, 3:4)) ``` """ BlockRange