Skip to content

Commit 9a2bce9

Browse files
authored
fix zero-dim similar (#457)
`similar(::BlockArray` behaves in a surprising way for 0-dimensional block arrays. This PR is a follow-up to #410 and aims for `Base.similar(::T{<:AbstractBlockArray}` to still return a `BlockArray/BlockedArray` in the zero-dimensional limit.
1 parent b55c7a4 commit 9a2bce9

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BlockArrays"
22
uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
3-
version = "1.6.0"
3+
version = "1.6.1"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"

src/blockarray.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ end
453453

454454
@inline Base.similar(B::BlockArray, ::Type{T}) where {T} = _BlockArray(similar.(blocks(B), T), axes(B))
455455

456+
# specific 0-dim
457+
@inline Base.similar(B::BlockArray{<:Any,0}, ::Type{T}) where {T} = BlockArray(similar(only(blocks(B)), T), ())
458+
@inline Base.similar(::BlockArray, ::Type{T}, ::Tuple{}) where {T} = BlockArray{T}(undef)
459+
456460
const OffsetAxis = Union{Integer, UnitRange, Base.OneTo, Base.IdentityUnitRange}
457461

458462
# avoid ambiguities

src/blockedarray.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ function Base.similar(block_array::BlockedArray{T,N}, ::Type{T2}) where {T,N,T2}
203203
BlockedArray(similar(block_array.blocks, T2), axes(block_array))
204204
end
205205

206+
# specific zero dim
207+
Base.similar(::BlockedArray, ::Type{T}, ::Tuple{}) where {T} = BlockedArray{T}(undef)
208+
206209
to_axes(r::AbstractUnitRange) = r
207210
to_axes(n::Integer) = Base.oneto(n)
208211

test/test_blockarrays.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ end
325325

326326
@testset "zero dim" begin
327327
zerodim = ones()
328+
r = blockedrange([1])
328329
@test view(zerodim) isa AbstractArray{Float64, 0} # check no type-piracy
329330

330331
ret = BlockArray{Float64}(undef)
@@ -343,13 +344,19 @@ end
343344
view(ret)[] = 0
344345
@test ret[] == 0
345346

346-
ret = BlockArrays.BlockArray(zeros())
347+
ret = BlockArray(zeros())
347348
@test ret isa BlockArray{Float64, 0}
348349
@test size(ret) == ()
349350
@test all(iszero, ret)
350351
@test ret[Block()] == zeros()
351352

352-
ret = BlockArrays.BlockArray(zeros(1,1))
353+
@test similar(ret) isa BlockArray{Float64, 0}
354+
@test similar(ret, Float32) isa BlockArray{Float32, 0}
355+
@test similar(ret, Float32, ()) isa BlockArray{Float32, 0}
356+
@test similar(ret, Float32, (r,)) isa BlockVector{Float32}
357+
@test similar(BlockArray(zeros(r)), Float32, ()) isa BlockArray{Float32, 0}
358+
359+
ret = BlockArray(zeros(1,1))
353360
@test reshape(ret, ()) isa AbstractBlockArray{Float64, 0} # may be BlockedArray
354361
@test size(reshape(ret, ())) == ()
355362

@@ -373,7 +380,13 @@ end
373380
@test all(iszero, ret)
374381
@test ret[Block()] == zeros()
375382

376-
ret = BlockArrays.BlockedArray(zeros(1,1))
383+
@test similar(ret) isa BlockedArray{Float64, 0}
384+
@test similar(ret, Float32) isa BlockedArray{Float32, 0}
385+
@test similar(ret, Float32, ()) isa BlockedArray{Float32, 0}
386+
@test similar(ret, Float32, (blockedrange([1]),)) isa BlockedVector{Float32}
387+
@test similar(zeros(r), Float32, ()) isa BlockedArray{Float32, 0}
388+
389+
ret = BlockedArray(zeros(1,1))
377390
@test reshape(ret, ()) isa BlockedArray{Float64, 0}
378391
@test size(reshape(ret, ())) == ()
379392
end

0 commit comments

Comments
 (0)