From 48c6fa966d473083d0857ce08fb11875cd1d90fa Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Wed, 16 Oct 2024 16:33:06 +0530 Subject: [PATCH 1/2] Preserve axes in similar for BlockArrays --- src/blockarray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/blockarray.jl b/src/blockarray.jl index 168997b1..1605fc23 100644 --- a/src/blockarray.jl +++ b/src/blockarray.jl @@ -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} From 1ce8185c3e92ffab535ca008e77c7975dbf024ce Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Wed, 16 Oct 2024 16:34:26 +0530 Subject: [PATCH 2/2] Add test --- test/test_blockarrays.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/test_blockarrays.jl b/test/test_blockarrays.jl index e0b55a87..f1ad6faa 100644 --- a/test/test_blockarrays.jl +++ b/test/test_blockarrays.jl @@ -248,6 +248,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])