Skip to content

Conversation

ogauthe
Copy link
Contributor

@ogauthe ogauthe commented Apr 10, 2025

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.

Copy link

codecov bot commented Apr 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.64%. Comparing base (a80fcf7) to head (634a7af).
Report is 3 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master     #457       +/-   ##
===========================================
+ Coverage    0.00%   93.64%   +93.64%     
===========================================
  Files          18       19        +1     
  Lines        1642     1667       +25     
===========================================
+ Hits            0     1561     +1561     
+ Misses       1642      106     -1536     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jishnub
Copy link
Member

jishnub commented Apr 11, 2025

Could you elaborate on what the issue is?

@ogauthe
Copy link
Contributor Author

ogauthe commented Apr 11, 2025

Currently, it is not possible to create a zero dimensional BlockArray using Base.similar. Some methods fail, some other return an Array.

b0 = BlockArray{Float64}(undef)
b1 = BlockArray{Float64}(undef, [1])

@test_broken similar(b0, Float32, ()) isa BlockArray{Float32, 0}  # 0 axis output returns Array
@test_broken similar(b1, Float32, ()) isa BlockArray{Float32, 0}  # 0 axis output returns Array
@test similar(b1, Float32, ()) isa Array{Float32, 0}

similar(b0)  # 0 axis input errors
ERROR: MethodError: no method matching _BlockArray(::Array{Float64, 0}, ::Tuple{})
The function `_BlockArray` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  _BlockArray(::Type{R}, ::NTuple{N, AbstractUnitRange{<:Integer}}) where {T, N, R<:(AbstractArray{<:AbstractArray{T, N}, N})}
   @ BlockArrays ~/.julia/packages/BlockArrays/v4qys/src/blockarray.jl:93
  _BlockArray(::R, ::BS) where {T, N, R<:(AbstractArray{<:AbstractArray{T, N}, N}), BS<:NTuple{N, AbstractUnitRange{<:Integer}}}
   @ BlockArrays ~/.julia/packages/BlockArrays/v4qys/src/blockarray.jl:65
  _BlockArray(::R, ::BS) where {N, R<:(AbstractArray{<:AbstractArray{V, N} where V, N}), BS<:NTuple{N, AbstractUnitRange{<:Integer}}}
   @ BlockArrays ~/.julia/packages/BlockArrays/v4qys/src/blockarray.jl:77
  ...

Stacktrace:
 [1] similar
   @ ~/.julia/packages/BlockArrays/v4qys/src/blockarray.jl:454 [inlined]
 [2] similar(a::BlockArray{Float64, 0, Array{Array{Float64, 0}, 0}, Tuple{}})
   @ BlockArrays ~/.julia/packages/BlockArrays/v4qys/src/abstractblockarray.jl:35
 [3] top-level scope
   @ REPL[4]:1

BlockedArray has similar issues.

This PR fixes similar with zero axes (either in the input or in the output) such that it always return a BlockArray/BlockedArray,

@jishnub
Copy link
Member

jishnub commented Apr 12, 2025

Oh, I see, the issue is with broadcasting dropping the container for zero-dim cases.

@jishnub jishnub merged commit 9a2bce9 into JuliaArrays:master Apr 12, 2025
16 checks passed
@ogauthe ogauthe deleted the similar0 branch April 13, 2025 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants