Skip to content

Commit cc32221

Browse files
Merge BlockIndexRanges (#470)
Fixes #467. --------- Co-authored-by: Sheehan Olver <[email protected]>
1 parent c775e52 commit cc32221

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

src/BlockArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using LinearAlgebra, ArrayLayouts, FillArrays
44
# AbstractBlockArray interface exports
55
export AbstractBlockArray, AbstractBlockMatrix, AbstractBlockVector, AbstractBlockVecOrMat
66
export Block, getblock, getblock!, setblock!, eachblock, blocks
7-
export blockaxes, blocksize, blocklength, blockcheckbounds, BlockBoundsError, BlockIndex
7+
export blockaxes, blocksize, blocklength, blockcheckbounds, BlockBoundsError, BlockIndex, BlockIndexRange
88
export blocksizes, blocklengths, blocklasts, blockfirsts, blockisequal
99
export BlockRange, blockedrange, BlockedUnitRange, BlockedOneTo
1010

src/blockbroadcast.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ julia> itr = SubBlockIterator(subblock_lasts, block_lasts)
8787
SubBlockIterator([1, 3, 6], [1, 3, 4, 6])
8888
8989
julia> collect(itr)
90-
4-element Vector{BlockArrays.BlockIndexRange{1, Tuple{UnitRange{Int64}}, Tuple{Int64}, Int64}}:
90+
4-element Vector{BlockIndexRange{1, Tuple{UnitRange{Int64}}, Tuple{Int64}, Int64}}:
9191
Block(1)[1:1]
9292
Block(2)[1:2]
9393
Block(3)[1:1]

src/blockindices.jl

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@ A `Block` is simply a wrapper around a set of indices or enums so that it can be
55
indexing a `AbstractBlockArray` with a `Block` the a block at that block index will be returned instead of
66
a single element.
77
8+
It can be constructed and used to index into `BlockArrays` in the following manner:
9+
810
```jldoctest
11+
julia> Block(1)
12+
Block(1)
13+
14+
julia> Block(1, 2)
15+
Block(1, 2)
16+
17+
julia> Block((Block(1), Block(2)))
18+
Block(1, 2)
19+
920
julia> A = BlockArray(ones(2,3), [1, 1], [2, 1])
1021
2×2-blocked 2×3 BlockMatrix{Float64}:
1122
1.0 1.0 │ 1.0
@@ -119,9 +130,21 @@ end
119130
A `BlockIndex` is an index which stores a global index in two parts: the block
120131
and the offset index into the block.
121132
122-
It can be used to index into `BlockArrays` in the following manner:
133+
It can be constructed and used to index into `BlockArrays` in the following manner:
123134
124135
```jldoctest
136+
julia> BlockIndex((1,2), (3,4))
137+
Block(1, 2)[3, 4]
138+
139+
julia> Block(1)[3] === BlockIndex((1), (3))
140+
true
141+
142+
julia> Block(1,2)[3,4] === BlockIndex((1,2), (3,4))
143+
true
144+
145+
julia> BlockIndex((Block(1)[3], Block(2)[4]))
146+
Block(1, 2)[3, 4]
147+
125148
julia> arr = Array(reshape(1:25, (5,5)));
126149
127150
julia> a = BlockedArray(arr, [3,2], [1,4])
@@ -133,10 +156,10 @@ julia> a = BlockedArray(arr, [3,2], [1,4])
133156
4 │ 9 14 19 24
134157
5 │ 10 15 20 25
135158
136-
julia> a[BlockIndex((1,2), (1,2))]
159+
julia> a[Block(1,2)[1,2]]
137160
11
138161
139-
julia> a[BlockIndex((2,2), (2,3))]
162+
julia> a[Block(2,2)[2,3]]
140163
20
141164
```
142165
"""
@@ -196,13 +219,54 @@ end
196219
"""
197220
BlockIndexRange(block, startind:stopind)
198221
199-
represents a cartesian range inside a block.
222+
Represents a cartesian range inside a block.
223+
224+
It can be constructed and used to index into `BlockArrays` in the following manner:
225+
226+
```jldoctest
227+
julia> BlockIndexRange(Block(1,2), (2:3,3:4))
228+
Block(1, 2)[2:3, 3:4]
229+
230+
julia> Block(1)[2:3] === BlockIndexRange(Block(1), 2:3)
231+
true
232+
233+
julia> Block(1,2)[2:3,3:4] === BlockIndexRange(Block(1,2), (2:3,3:4))
234+
true
235+
236+
julia> BlockIndexRange((Block(1)[2:3], Block(2)[3:4]))
237+
Block(1, 2)[2:3, 3:4]
238+
239+
julia> arr = Array(reshape(1:25, (5,5)));
240+
241+
julia> a = BlockedArray(arr, [3,2], [1,4])
242+
2×2-blocked 5×5 BlockedMatrix{Int64}:
243+
1 │ 6 11 16 21
244+
2 │ 7 12 17 22
245+
3 │ 8 13 18 23
246+
───┼────────────────
247+
4 │ 9 14 19 24
248+
5 │ 10 15 20 25
249+
250+
julia> a[Block(1,2)[1:2,2:3]]
251+
2×2 Matrix{Int64}:
252+
11 16
253+
12 17
254+
255+
julia> a[Block(2,2)[1:2,3:4]]
256+
2×2 Matrix{Int64}:
257+
19 24
258+
20 25
259+
```
200260
"""
201261
BlockIndexRange
202262

203263
BlockIndexRange(block::Block{N}, inds::Vararg{AbstractUnitRange{<:Integer},N}) where {N} =
204264
BlockIndexRange(block,inds)
205265

266+
function BlockIndexRange(inds::Tuple{BlockIndexRange{1},Vararg{BlockIndexRange{1}}})
267+
BlockIndexRange(Block(block.(inds)), map(ind -> ind.indices[1], inds))
268+
end
269+
206270
block(R::BlockIndexRange) = R.block
207271

208272
copy(R::BlockIndexRange) = BlockIndexRange(R.block, map(copy, R.indices))
@@ -359,6 +423,12 @@ julia> BlockRange((2, 2)) |> collect # number of elements, starting at 1
359423
360424
julia> Block(1):Block(2)
361425
BlockRange((1:2,))
426+
427+
julia> Block.(1:2)
428+
BlockRange((1:2,))
429+
430+
julia> BlockRange((Block.(1:2), Block.(3:4)))
431+
BlockRange((1:2, 3:4))
362432
```
363433
"""
364434
BlockRange

test/test_blockindices.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice, BlockedSlice
8989
@test Block(1)[1:2] == BlockIndexRange(Block(1),(1:2,))
9090
@test Block(1,1)[1,1] == BlockIndex((1,1),(1,1)) == BlockIndex((1,1),(1,))
9191
@test Block(1,1)[1:2,1:2] == BlockIndexRange(Block(1,1),(1:2,1:2))
92+
@test BlockIndexRange((Block(1)[1:2],Block(1)[1:2])) == BlockIndexRange(Block(1,1),(1:2,1:2))
9293
@test Block(1)[1:3][1:2] == BlockIndexRange(Block(1),1:2)
9394
@test Block(1,1)[2:4,2:4][2:3,2:3] == BlockIndexRange(Block(1,1),(3:4,3:4))
9495
@test BlockIndexRange(Block(),())[] == BlockIndex()

0 commit comments

Comments
 (0)