Skip to content

Subtracting 0-dimensional AbstractDiskArrays fails #285

@sethaxen

Description

@sethaxen

The problem

Here's an MWE:

julia> using DiskArrays.TestTypes

julia> v = UnchunkedDiskArray(rand(2));

julia> v-v;  # works

julia> a = UnchunkedDiskArray(fill(rand()));

julia> a - a; # fails
ERROR: MethodError: no method matching similar(::Base.Broadcast.Broadcasted{…}, ::Type{…}, ::Tuple{})
The function `similar` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  similar(::Base.ReshapedArray, ::Type, ::NTuple{N, Int64} where N)
   @ Base reshapedarray.jl:252
  similar(::Array, ::Type, ::NTuple{N, Int64}) where N
   @ Base array.jl:377
  similar(::Base.ReinterpretArray, ::Type, ::NTuple{N, Int64} where N)
   @ Base reinterpretarray.jl:223
  ...

Stacktrace:
 [1] similar(bc::Base.Broadcast.Broadcasted{DiskArrays.ChunkStyle{…}, Nothing, typeof(-), Tuple{…}}, ::Type{DiskArrays.BroadcastDiskArray{…}})
   @ Base.Broadcast ./broadcast.jl:227
 [2] broadcast_preserving_zero_d
   @ ./broadcast.jl:884 [inlined]
 [3] -(A::UnchunkedDiskArray{Float64, 0, Array{Float64, 0}}, B::UnchunkedDiskArray{Float64, 0, Array{Float64, 0}})
   @ Base ./arraymath.jl:8
 [4] top-level scope
   @ REPL[8]:1
Some type information was truncated. Use `show(err)` to see complete types.

Same error happens for ChunkedDiskArray.

Proposed solution

This happens because subtracting AbstractArrays ultimately hits this method, which for 0-dimensional arrays calls similar(bc, ::Type{T}) on the result of Base.broadcasted(-, a, a). For a normal Array, we hit these two methods, but DiskArrays lacks these analogous methods:

Base.similar(::Base.Broadcast.Broadcasted{DiskArrays.ChunkStyle{N}}, ::Type{ElType}, dims) where {N,ElType} =
    similar(Array{ElType, length(dims)}, dims)
Base.similar(::Base.Broadcast.Broadcasted{DiskArrays.ChunkStyle{N}}, ::Type{Bool}, dims) where N =
    similar(BitArray, dims)

This isn't quite enough to make the above example work, because DiskArrays also lacks an analog to this method:

@inline Base.copy(bc::Base.Broadcast.Broadcasted{DiskArrays.ChunkStyle{0}}) = bc[CartesianIndex()]

With these 3 overloads, the example works:

julia> v - v
2-element DiskArrays.BroadcastDiskArray{Float64, 1, Base.Broadcast.Broadcasted{DiskArrays.ChunkStyle{1}, Tuple{Base.OneTo{Int64}}, typeof(-), Tuple{UnchunkedDiskArray{Float64, 1, Vector{Float64}}, UnchunkedDiskArray{Float64, 1, Vector{Float64}}}}}

Chunked: (
    [2]
)


julia> a - a
0-dimensional Array{Float64, 0}:
0.0

Does it make sense to add these?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions