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

repeat(A::AbstractArray; inner=k::Int) fails for Arrays of Arrays. #22748

Closed
braydenware opened this issue Jul 11, 2017 · 2 comments
Closed

repeat(A::AbstractArray; inner=k::Int) fails for Arrays of Arrays. #22748

braydenware opened this issue Jul 11, 2017 · 2 comments
Labels
arrays [a, r, r, a, y, s]

Comments

@braydenware
Copy link

#20635 changed the implementation of repeat for arrays to improve performance, but it now results in errors for Arrays of Arrays.

On julia version 0.6.0,

julia> repeat([[1,2], [3,4]]; inner=2)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Array{Int64,1}
This may have arisen from a call to the constructor Array{Int64,1}(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] setindex!(::Array{Array{Int64,1},1}, ::Array{Int64,1}, ::UnitRange{Int64}) at ./array.jl:573
 [2] _repeat at ./abstractarraymath.jl:411 [inlined]
 [3] #repeat#117(::Int64, ::Tuple{Int64}, ::Function, ::Array{Array{Int64,1},1}) at ./abstractarraymath.jl:366
 [4] (::Base.#kw##repeat)(::Array{Any,1}, ::Base.#repeat, ::Array{Array{Int64,1},1}) at ./<missing>:0

The result should be

julia> repeat([[1,2], [3,4]]; inner=2)
4-element Array{Array{Int64,1},1}:
 [1,2]
 [1,2]
 [3,4]
 [3,4]

as it is in julia 0.5.

The error message is different for higher dimensional Arrays:

julia> repeat([fill(1, 2,2), fill(2, 2,2)]; inner=2)
ERROR: DimensionMismatch("tried to assign 4 elements to 2 destinations")
Stacktrace:
 [1] throw_setindex_mismatch(::Array{Int64,2}, ::Tuple{Int64}) at ./indices.jl:92
 [2] setindex_shape_check(::Array{Int64,2}, ::Int64) at ./indices.jl:140
 [3] setindex!(::Array{Array{Int64,2},1}, ::Array{Int64,2}, ::UnitRange{Int64}) at ./array.jl:563
 [4] _repeat at ./abstractarraymath.jl:411 [inlined]
 [5] #repeat#117(::Int64, ::Tuple{Int64}, ::Function, ::Array{Array{Int64,2},1}) at ./abstractarraymath.jl:366
 [6] (::Base.#kw##repeat)(::Array{Any,1}, ::Base.#repeat, ::Array{Array{Int64,2},1}) at ./<missing>:0

which should be

julia> repeat([fill(1, 2,2), fill(2, 2,2)]; inner=2)
4-element Array{Array{Int64,2},1}:
 [1 1; 1 1]
 [1 1; 1 1]
 [2 2; 2 2]
 [2 2; 2 2]

The culprit seems to be trying to set a slice of the output array at once, using the behavior A[vector] = scalar to set multiple elements of A to the same scalar.
When A is an Array of Arrays and scalar is an Array, A[vector] = scalar behavior does not work, perhaps because it would be ambiguous with A[vector] = vector.
For example,

R = Vector{Matrix{Int}}(4)
R[1] = fill(1, 2,2)
R[2] = fill(1, 2,2)
R[3] = fill(2, 2,2)
R[4] = fill(2, 2,2)

cannot be replaced with

R = Vector{Matrix{Int}}(4)
R[1:2] = fill(1, 2,2)
R[3:4] = fill(2, 2,2)

for producing the behavior of repeat([fill(1, 2,2), fill(2, 2,2)]; inner=2).

Note that the behavior of the outer keyword works fine:

julia> repeat([[1,2],[3,4]]; outer=2)
4-element Array{Array{Int64,1},1}:
 [1, 2]
 [3, 4]
 [1, 2]
 [3, 4]

as well as the behavior of indexable objects that are not AbstractArrays:

julia> repeat([(1,2),(3,4)]; inner=2)
4-element Array{Tuple{Int64,Int64},1}:
 (1, 2)
 (1, 2)
 (3, 4)
 (3, 4)
@braydenware
Copy link
Author

Oops, didn't see that you filed an issue slightly before me. Should I close?

@pabloferz
Copy link
Contributor

No, I sent a PR to fix the issue, this will be closed once the PR gets merged.

@ararslan ararslan added the arrays [a, r, r, a, y, s] label Jul 12, 2017
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]
Projects
None yet
Development

No branches or pull requests

3 participants