Skip to content

Commit 1e5feaa

Browse files
authored
Preserve axes in similar for BlockArrays (#422)
* Preserve axes in similar for BlockArrays * Add test
1 parent 2718188 commit 1e5feaa

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/blockarray.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ end
451451
@inline Base.similar(block_array::Type{<:AbstractArray{T}}, axes::Tuple{Union{AbstractUnitRange{<:Integer},Integer},AbstractBlockedUnitRange,Vararg{Union{AbstractUnitRange{<:Integer},Integer}}}) where T =
452452
BlockArray{T}(undef, map(to_axes,axes))
453453

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

456456
const OffsetAxis = Union{Integer, UnitRange, Base.OneTo, Base.IdentityUnitRange}
457457

test/test_blockarrays.jl

+3
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ end
250250
@test similar(randn(6,5), Float64, (3,blockedrange(1:3))) isa BlockedMatrix
251251
@test similar(typeof(view(randn(5),1:3)), (blockedrange(1:3),)) isa BlockedVector
252252
@test similar(view(randn(5),1:3), Int, (blockedrange(1:3),)) isa BlockedVector{Int}
253+
254+
b = BlockVector([1,2,3,4,5,6,7,8,9,10], (BlockedOneTo(5:5:10),))
255+
@test zero(b) isa typeof(b)
253256
end
254257

255258
@test_throws DimensionMismatch BlockArray([1,2,3],[1,1])

0 commit comments

Comments
 (0)