Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcasting over views with non-Int indices fails (or creates an OffsetArray) #32442

Closed
jaakkor2 opened this issue Jun 27, 2019 · 2 comments
Closed
Labels
arrays [a, r, r, a, y, s] broadcast Applying a function over a collection bug Indicates an unexpected problem or unintended behavior regression Regression in behavior compared to a previous version

Comments

@jaakkor2
Copy link
Contributor

Some variation of the view does not get broadcasted due to MethodError.

a=rand(UInt32,20); b=view(a,UInt64.(11:20)); c=rand(UInt64,5); tmp=reinterpret(UInt64,b) .- c # ok
a=rand(UInt32,20); b=view(a,(11:20)); c=rand(UInt64,5); tmp=reinterpret(UInt64,b) .- c # ok
a=rand(UInt32,20); b=view(a,(UInt64(11):UInt64(20))); c=rand(UInt64,5); tmp=reinterpret(UInt64,b) .- c # not ok
ERROR: MethodError: no method matching similar(::Type{Array{UInt64,N} where N}, ::Tuple{UnitRange{Int64}})
Closest candidates are:
  similar(::AbstractArray{T,N} where N, ::Tuple) where T at abstractarray.jl:573
  similar(::Type{T<:AbstractArray}, ::Union{Integer, AbstractUnitRange}...) where T<:AbstractArray at abstractarray.jl:616
  similar(::Type{T<:AbstractArray}, ::Tuple{Vararg{Int64,N}} where N) where T<:AbstractArray at abstractarray.jl:618
  ...
Stacktrace:
 [1] similar(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{UnitRange{Int64}},typeof(-),Tuple{Base.ReinterpretArray{UInt64,1,UInt32,SubArray{UInt32,1,Array{UInt32,1},Tuple{UnitRange{UInt64}},true}},Array{UInt64,1}}}, ::Type{UInt64}) at .\broadcast.jl:196
 [2] copy at .\broadcast.jl:773 [inlined]
 [3] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(-),Tuple{Base.ReinterpretArray{UInt64,1,UInt32,SubArray{UInt32,1,Array{UInt32,1},Tuple{UnitRange{UInt64}},true}},Array{UInt64,1}}}) at .\broadcast.jl:753
 [4] top-level scope at none:0

This works

a=rand(UInt32,20); b=view(a,(UInt64(11):UInt64(20))); c=rand(UInt64,5); tmp=copy(reinterpret(UInt64,b)) .- c # ok

and a bit surprisingly this as well

using Interpolations
a=rand(UInt32,20); b=view(a,(UInt64(11):UInt64(20))); c=rand(UInt64,5); tmp=reinterpret(UInt64,b) .- c # now ok

this failed above, now the output is an OffsetArray.

Version info as follows

julia> versioninfo()
Julia Version 1.1.1
Commit 55e36cc308 (2019-05-16 04:10 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
@mbauman mbauman changed the title MethodError for broadcast involving view Broadcasting over views with non-Int indices fails (or creates an OffsetArray) Jul 1, 2019
@mbauman
Copy link
Member

mbauman commented Jul 1, 2019

Thanks for the report — the simpler MWE is just:

julia> view(a,(Int32(11):Int32(20))) .+ [1]
ERROR: MethodError: no method matching similar(::Type{Array{Int64,N} where N}, ::Tuple{UnitRange{Int64}})

We should be a bit more accepting of arrays with OneTo axes here beyond just OneTo{Int}. The reason for all the differences above is that you're sometimes creating a range and sometimes copying the range values into an Array (which has axes with Int values).

@mbauman mbauman added arrays [a, r, r, a, y, s] broadcast Applying a function over a collection bug Indicates an unexpected problem or unintended behavior labels Jul 1, 2019
@mbauman
Copy link
Member

mbauman commented Jul 1, 2019

This worked on 1.0 but fails in 1.1 and 1.2 and master.

@mbauman mbauman added the regression Regression in behavior compared to a previous version label Jul 1, 2019
mbauman added a commit that referenced this issue Dec 31, 2019
…eltypes

In cases where we have multiple arrays with `OneTo` axes that do not share the same axis eltype, we should simply default to constructing a new array with `OneTo{Int}` axes.
KristofferC pushed a commit that referenced this issue Jan 8, 2020
…eltypes (#34230)

In cases where we have multiple arrays with `OneTo` axes that do not share the same axis eltype, we should simply default to constructing a new array with `OneTo{Int}` axes.

(cherry picked from commit 11e7c33)
KristofferC pushed a commit that referenced this issue Apr 11, 2020
…eltypes (#34230)

In cases where we have multiple arrays with `OneTo` axes that do not share the same axis eltype, we should simply default to constructing a new array with `OneTo{Int}` axes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] broadcast Applying a function over a collection bug Indicates an unexpected problem or unintended behavior regression Regression in behavior compared to a previous version
Projects
None yet
Development

No branches or pull requests

2 participants