Skip to content

Commit 0255982

Browse files
authored
A[Block.(1:3)] should return a BlockArray (#122)
* A[Block.(1:3)] should return a BlockArray * Update Project.toml
1 parent 52a2a9f commit 0255982

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
name = "BlockArrays"
22
uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
3-
version = "0.12.7"
3+
version = "0.12.8"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
77
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
88
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
99

1010
[compat]
11-
ArrayLayouts = "0.3.2"
11+
ArrayLayouts = "0.3.4"
1212
Compat = "2.2, 3"
1313
julia = "1.1"
1414

src/BlockArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import LinearAlgebra: lmul!, rmul!, AbstractTriangular, HermOrSym, AdjOrTrans,
3838
import ArrayLayouts: _fill_lmul!, MatMulVecAdd, MatMulMatAdd, MatLmulVec, MatLdivVec,
3939
materialize!, MemoryLayout, sublayout, transposelayout, conjlayout,
4040
triangularlayout, triangulardata, _inv, _copyto!, axes_print_matrix_row,
41-
colsupport, rowsupport
41+
colsupport, rowsupport, sub_materialize
4242

4343
if !@isdefined(only)
4444
using Compat: only

src/blocklinalg.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ function rowsupport(::BlockLayout, A, k)
4242
axes(A,2)[Block.(JR)]
4343
end
4444

45+
46+
4547
similar(M::MulAdd{<:AbstractBlockLayout,<:AbstractBlockLayout}, ::Type{T}, axes) where {T,N} =
4648
similar(BlockArray{T}, axes)
4749

@@ -57,6 +59,9 @@ sublayout(BL::BlockLayout{MLAY,BLAY}, ::Type{<:Tuple{BlockSlice1,<:BlockSlice{Bl
5759
sublayout(BL::BlockLayout{MLAY,BLAY}, ::Type{<:Tuple{<:BlockSlice{BlockRange{1,Tuple{II}}},BlockSlice1}}) where {MLAY,BLAY,II} =
5860
BlockLayout{typeof(sublayout(MLAY(),Tuple{II,Int})), BLAY}()
5961

62+
# materialize views, used for `getindex`
63+
sub_materialize(::AbstractBlockLayout, V, _) = BlockArray(V)
64+
sub_materialize(::AbstractStridedLayout, V, ::Tuple{<:BlockedUnitRange}) = PseudoBlockArray(V)
6065

6166
conjlayout(::Type{T}, ::BlockLayout{MLAY,BLAY}) where {T<:Complex,MLAY,BLAY} = BlockLayout{MLAY,typeof(conjlayout(T,BLAY()))}()
6267
conjlayout(::Type{T}, ::BlockLayout{MLAY,BLAY}) where {T<:Real,MLAY,BLAY} = BlockLayout{MLAY,BLAY}()

src/views.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,16 @@ const BlockSlice1 = BlockSlice{Block{1,Int},UnitRange{Int}}
156156
block(A::BlockSlice) = block(A.block)
157157
block(A::Block) = A
158158

159-
getblock(A::SubArray{<:Any,N,<:Any,NTuple{N,BlockSlice1}}) where N =
159+
getblock(A::SubArray{<:Any,N,<:Any,NTuple{N,BlockSlice1}}) where N =
160160
getblock(parent(A), Int.(block.(parentindices(A)))...)
161161
getblock(A::Adjoint, k::Int, j::Int) = getblock(parent(A), j, k)'
162162
getblock(A::Transpose, k::Int, j::Int) = transpose(getblock(parent(A), j, k))
163163

164-
strides(A::SubArray{<:Any,N,<:BlockArray,NTuple{N,BlockSlice1}}) where N =
164+
strides(A::SubArray{<:Any,N,<:BlockArray,NTuple{N,BlockSlice1}}) where N =
165165
strides(getblock(A))
166166

167167
for Adj in (:Transpose, :Adjoint)
168-
@eval strides(A::SubArray{<:Any,N,<:$Adj{<:Any,<:BlockArray},NTuple{N,BlockSlice1}}) where N =
168+
@eval strides(A::SubArray{<:Any,N,<:$Adj{<:Any,<:BlockArray},NTuple{N,BlockSlice1}}) where N =
169169
strides(getblock(A))
170170
end
171171

@@ -175,3 +175,5 @@ unsafe_convert(::Type{Ptr{T}}, V::SubArray{T, N, <:Any, <:NTuple{N, BlockSlice1}
175175

176176
unsafe_convert(::Type{Ptr{T}}, V::SubArray{T,N,PseudoBlockArray{T,N,AT},<:Tuple{Vararg{BlockOrRangeIndex}}}) where {T,N,AT} =
177177
unsafe_convert(Ptr{T}, V.parent) + (Base.first_index(V)-1)*sizeof(T)
178+
179+

test/test_blockviews.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,13 @@ using BlockArrays, Test, Base64
165165
@test stringmime("text/plain", W) == "3×1 view(::BlockArray{$Int,2,Array{Array{$Int,2},2},Tuple{BlockedUnitRange{Array{$Int,1}},BlockedUnitRange{Array{$Int,1}}}}, BlockSlice(Block{1,$Int}[Block(1), Block(2)],1:1:3), BlockSlice(Block(1),1:1)) with eltype $Int with indices 1:1:3×Base.OneTo(1):\n 1\n\n 4\n 7"
166166
end
167167
end
168+
169+
@testset "getindex with BlockRange" begin
170+
A = BlockArray(randn(6), 1:3)
171+
@test A[Block.(2:3)] isa BlockArray
172+
@test A[Block.(2:3)] == A[2:end]
173+
A = PseudoBlockArray(randn(6), 1:3)
174+
@test A[Block.(2:3)] isa PseudoBlockArray
175+
@test A[Block.(2:3)] == A[2:end]
176+
end
168177
end

0 commit comments

Comments
 (0)