Skip to content

Commit

Permalink
Support views involving vectors of blocks, Fixes #184 and #358 (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlfivefifty authored Feb 6, 2025
1 parent 6e54660 commit a4e544e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "BlockArrays"
uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
version = "1.3.0"
version = "1.4.0"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand Down
5 changes: 5 additions & 0 deletions src/blockaxis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@ end

@propagate_inbounds getindex(b::AbstractBlockedUnitRange, KR::BlockSlice) = b[KR.block]

getindex(b::AbstractBlockedUnitRange, KR::AbstractVector{<:Block{1}}) = mortar([b[K] for K in KR])
getindex(b::AbstractBlockedUnitRange, KR::AbstractVector{<:BlockIndexRange{1}}) = mortar([b[K] for K in KR])
getindex(b::AbstractBlockedUnitRange, KR::AbstractVector{<:BlockIndex{1}}) = [b[K] for K in KR]
getindex(b::AbstractBlockedUnitRange, Kkr::BlockIndexRange{1}) = b[block(Kkr)][Kkr.indices...]

_searchsortedfirst(a::AbstractVector, k) = searchsortedfirst(a, k)
function _searchsortedfirst(a::Tuple, k)
k first(a) && return 1
Expand Down
4 changes: 2 additions & 2 deletions src/blockindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ Block(bs::BlockIndexRange) = bs.block


"""
BlockSlice(indices)
BlockSlice(block, indices)
Represent an AbstractUnitRange of indices that attaches a block.
Represent an AbstractUnitRange{<:Integer} of indices that attaches a block.
Upon calling `to_indices()`, Blocks are converted to BlockSlice objects to represent
the indices over which the Block spans.
Expand Down
16 changes: 15 additions & 1 deletion src/views.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ Returns the indices associated with a block as a `BlockSlice`.
"""
function unblock(A, inds, I)
B = first(I)
BlockSlice(B,inds[1][B])
_blockslice(B, inds[1][B])
end

_blockslice(B, a::AbstractUnitRange) = BlockSlice(B, a)
_blockslice(B, a) = a # drop block structure

# Allow `ones(2)[Block(1)[1:1], Block(1)[1:1]]` which is
# similar to `ones(2)[1:1, 1:1]`.
# Need to check the length of I in case its empty
Expand All @@ -28,6 +32,13 @@ to_index(::BlockRange) = throw(ArgumentError("BlockRange must be converted by to
(inds[1][I[1]], to_indices(A, _maybetail(inds), tail(I))...)
@inline to_indices(A, inds, I::Tuple{BlockIndexRange{1,R}, Vararg{Any}}) where R =
(unblock(A, inds, I), to_indices(A, _maybetail(inds), tail(I))...)
@inline to_indices(A, inds, I::Tuple{AbstractVector{Block{1,R}}, Vararg{Any}}) where R =
(unblock(A, inds, I), to_indices(A, _maybetail(inds), tail(I))...)
@inline to_indices(A, inds, I::Tuple{AbstractVector{<:BlockIndex{1}}, Vararg{Any}}) =
(unblock(A, inds, I), to_indices(A, _maybetail(inds), tail(I))...)
@inline to_indices(A, inds, I::Tuple{AbstractVector{<:BlockIndexRange{1}}, Vararg{Any}}) =
(unblock(A, inds, I), to_indices(A, _maybetail(inds), tail(I))...)


# splat out higher dimensional blocks
# this mimics view of a CartesianIndex
Expand All @@ -44,6 +55,9 @@ to_index(::BlockRange) = throw(ArgumentError("BlockRange must be converted by to
@inline to_indices(A, I::Tuple{BlockIndexRange, Vararg{Any}}) = to_indices(A, axes(A), I)
@inline to_indices(A, I::Tuple{Block, Vararg{Any}}) = to_indices(A, axes(A), I)
@inline to_indices(A, I::Tuple{BlockRange, Vararg{Any}}) = to_indices(A, axes(A), I)
@inline to_indices(A, I::Tuple{AbstractVector{<:Block{1}}, Vararg{Any}}) = to_indices(A, axes(A), I)
@inline to_indices(A, I::Tuple{AbstractVector{<:BlockIndex{1}}, Vararg{Any}}) = to_indices(A, axes(A), I)
@inline to_indices(A, I::Tuple{AbstractVector{<:BlockIndexRange{1}}, Vararg{Any}}) = to_indices(A, axes(A), I)

@propagate_inbounds reindex(idxs::Tuple{BlockSlice{<:BlockRange}, Vararg{Any}},
subidxs::Tuple{BlockSlice{<:BlockIndexRange}, Vararg{Any}}) =
Expand Down
15 changes: 15 additions & 0 deletions test/test_blockarrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,21 @@ end
@test_throws BoundsError a[:,Block.(1:2)]
@test size(a[:,1]) == (8,)
end

@testset "Block-vector indexing (#184)" begin
a = BlockArray(1:6, [1,2,2,1])
@test a[Block.(2:3)] == a[collect(Block.(2:3))]

for B in (BlockArray(I, fill(2,4), fill(2,5)), BlockedArray(I, fill(2,4), fill(2,5)))
@test B[[Block(1),Block(2)], [Block(1),Block(2)]] == view(B, [Block(1),Block(2)], [Block(1), Block(2)]) == view(B, Block.(1:2), [Block(1), Block(2)]) == view(B, Block.(1:2), Block.(1:2))
@test B[[Block(1),Block(3)], [Block(1), Block(5)]] == view(B, [Block(1),Block(3)], [Block(1), Block(5)])
end
end
@testset "BlockIndex-vector indexing (#358)" begin
a = BlockArray(randn(6, 6), [3, 3], [3, 3])
@test a[[Block(1)[1:2], Block(2)[1:2]], [Block(1)[1:2], Block(2)[1:2]]] == [a[Block(1,1)[1:2,1:2]] a[Block(1,2)[1:2,1:2]]; a[Block(2,1)[1:2,1:2]] a[Block(2,2)[1:2,1:2]]]
@test a[[Block(1)[1], Block(2)[2]], [Block(1)[1:2], Block(2)[1:2]]] == [a[Block(1)[1],Block(1)[1:2]]' a[Block(1)[1], Block(2)[1:2]]'; a[Block(2)[2],Block(1)[1:2]]' a[Block(2)[2], Block(2)[1:2]]']
end
end

end # module
5 changes: 5 additions & 0 deletions test/test_blockindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -888,4 +888,9 @@ end
@test !blockisequal(axv, axB)
end

@testset "BlockIndices" begin
a = BlockedOneTo(1:3)
@test a[[Block(1),Block(3)]] == a[Block.(1:2:3)] == [1,3]
end

end # module

2 comments on commit a4e544e

@dlfivefifty
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/124470

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.4.0 -m "<description of version>" a4e544ee9ffd1c1f93efcd46fbcb8328c338c2fe
git push origin v1.4.0

Please sign in to comment.