Skip to content

Commit d406642

Browse files
authored
BlockArray construction from immutable array (#357)
* BlockArray construction from immutable array * Fix test * Use map instead of broadcasting
1 parent 2ce8d26 commit d406642

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/blockarray.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,11 @@ BlockArray(arr::AbstractArray{T, N}, block_sizes::Vararg{AbstractVector{<:Intege
175175
BlockArray{T}(arr, block_sizes...)
176176

177177
function BlockArray{T}(arr::AbstractArray{T, N}, baxes::NTuple{N,AbstractUnitRange{Int}}) where {T,N}
178-
block_arr = _BlockArray(Array{typeof(arr),N}, baxes)
179-
for block_index in Iterators.product(blockaxes.(baxes,1)...)
180-
indices = getindex.(baxes,block_index)
181-
block_arr[block_index...] = arr[indices...]
178+
blocks = map(Iterators.product(map(x -> blockaxes(x,1), baxes)...)) do block_index
179+
indices = map((x,y) -> x[y], baxes, block_index)
180+
arr[indices...]
182181
end
183-
return block_arr
182+
return _BlockArray(blocks, baxes)
184183
end
185184

186185
BlockArray{T}(arr::AbstractArray{<:Any, N}, baxes::NTuple{N,AbstractUnitRange{Int}}) where {T,N} =

test/test_blockarrays.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ end
8080
B = mortar(fill(S,2,2))
8181
A = Array(B)
8282
@test A isa Matrix
83+
84+
# test that BlockArrays may be created from immutable arrays
85+
B = BlockArray(reshape(1:9,3,3), [2,1], [2,1])
86+
@test blocksizes(B) == ([2,1], [2,1])
87+
@test B == reshape([1:9;],3,3)
88+
@test blocks(B) isa Matrix{Matrix{Int}}
8389
end
8490

8591
@testset "PseudoBlockArray constructors" begin

0 commit comments

Comments
 (0)