Skip to content

Commit

Permalink
Preserve axes in similar for BlockArrays (#422)
Browse files Browse the repository at this point in the history
* Preserve axes in similar for BlockArrays

* Add test
  • Loading branch information
jishnub authored Oct 29, 2024
1 parent 2718188 commit 1e5feaa
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/blockarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ end
@inline Base.similar(block_array::Type{<:AbstractArray{T}}, axes::Tuple{Union{AbstractUnitRange{<:Integer},Integer},AbstractBlockedUnitRange,Vararg{Union{AbstractUnitRange{<:Integer},Integer}}}) where T =
BlockArray{T}(undef, map(to_axes,axes))

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

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

Expand Down
3 changes: 3 additions & 0 deletions test/test_blockarrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ end
@test similar(randn(6,5), Float64, (3,blockedrange(1:3))) isa BlockedMatrix
@test similar(typeof(view(randn(5),1:3)), (blockedrange(1:3),)) isa BlockedVector
@test similar(view(randn(5),1:3), Int, (blockedrange(1:3),)) isa BlockedVector{Int}

b = BlockVector([1,2,3,4,5,6,7,8,9,10], (BlockedOneTo(5:5:10),))
@test zero(b) isa typeof(b)
end

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

0 comments on commit 1e5feaa

Please sign in to comment.