JuliaArrays / OffsetArrays.jl

Fortran-like arrays with arbitrary, zero or negative starting indices.
Other
195 stars 40 forks source link

broadcast fails - bad error message #348

Closed lmiq closed 5 months ago

lmiq commented 5 months ago

This is both a feature request and the report that the error message is not good:

julia> using OffsetArrays

julia> x = OffsetArray([1,2,3], 0:2)
3-element OffsetArray(::Vector{Int64}, 0:2) with eltype Int64 with indices 0:2:
 1
 2
 3

julia> x .+ [1,2,3]
ERROR: DimensionMismatch: arrays could not be broadcast to a common size; got a dimension with lengths 3 and 3
Stacktrace:
 [1] _bcs1
   @ ./broadcast.jl:555 [inlined]
 [2] _bcs
   @ ./broadcast.jl:549 [inlined]
 [3] broadcast_shape
   @ ./broadcast.jl:543 [inlined]
 [4] combine_axes
   @ ./broadcast.jl:524 [inlined]
 [5] instantiate
   @ ./broadcast.jl:306 [inlined]
 [6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{…}, Nothing, typeof(+), Tuple{…}})
   @ Base.Broadcast ./broadcast.jl:903
 [7] top-level scope
   @ REPL[3]:1
Some type information was truncated. Use `show(err)` to see complete types.

First, it would be nice if broadcasting worked in such a case. Second, in any case, the error message is not good and does not point to any possible solution, if supporting broadcasting in these cases is not possible for more fundamental reasons.

I guess there is the question on the nature of the indices of the returned array.

jishnub commented 5 months ago

Re. the error message, this needs to be fixed in Base (something similar to https://github.com/JuliaLang/julia/pull/41311). There is certainly scope for a much better error message here, and it's just that someone needs to do the work and submit a PR.

it would be nice if broadcasting worked in such a case

This is also prohibited by Base at present. I doubt that there's anything inherent in OffsetArrays that prevents this.

I guess there is the question on the nature of the indices of the returned array.

Exactly, what would be the axes of the result? In particular, if we have two offset arrays (instead of only one), what axes does the result have? I think this is the main unresolved point.

jishnub commented 5 months ago

The error message seems to have improved on Julia v1.11.0-alpha1:

julia> x .+ [1,2,3]
ERROR: DimensionMismatch: arrays could not be broadcast to a common size: a has axes OffsetArrays.IdOffsetRange(values=0:2, indices=0:2) and b has axes Base.OneTo(3)
[...]