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

Unexpected behaviour of broadcast getindex.() if there are slices #28031

Closed
MoonCoral opened this issue Jul 10, 2018 · 3 comments
Closed

Unexpected behaviour of broadcast getindex.() if there are slices #28031

MoonCoral opened this issue Jul 10, 2018 · 3 comments
Labels
broadcast Applying a function over a collection

Comments

@MoonCoral
Copy link
Contributor

MoonCoral commented Jul 10, 2018

Toy Example:
(Tested in 0.6 and 0.7-beta.0)

 # Nested Array
input = [
    [1, 2, 3, 4, 5, 6, 7, 8, 9],
    [10, 20, 30, 40, 50, 60, 70, 80, 90],
    [100, 200, 300, 400, 500, 600, 700, 800, 900]
]

# For reference, this does what I expect
output = getindex.(input, 3)
# 3-element Array{Int64,1}:
# 3
# 30
# 300

# Unexpected Output
output = getindex.(input, 4:6)
# 3-element Array{Int64,1}:
# 4
# 50
# 600

# What I expected to be returned
output = [getindex(input[1], 4:6), getindex(input[2], 4:6), getindex(input[3], 4:6)]
# 3-element Array{Array{Int64,1},1}:
# [4, 5, 6]      
# [40, 50, 60]   
# [400, 500, 600]

# This Errors
output = getindex.(input, 4:7)
# ERROR: DimensionMismatch("arrays could not be broadcast to a common size")

# What I expected to be returned
output = [getindex(input[1], 4:7), getindex(input[2], 4:7), getindex(input[3], 4:7)]
# 3-element Array{Array{Int64,1},1}:
# [4, 5, 6, 7]        
# [40, 50, 60, 70]    
# [400, 500, 600, 700]

Based on the above I assume, that the ranges are not passed verbatim by broadcast to the getindex function. But instead are expanded and participate in the broadcast.

My question now is, is this the desired behaviour and if so is there any non-verbose syntax to achieve what I want assuming I don't know the size of input beforehand and do not want to write a for loop as it interrupts broadcast chaining?

Edit: The current behaviour seems to be that of broadcast_getindex (getting deprecated in favour of the syntax in question in #27075).

@stevengj
Copy link
Member

Based on the above I assume, that the ranges are not passed verbatim by broadcast to the getindex function. But instead are expanded and participate in the broadcast.

Range arguments are treated as arrays (they are AbstractArrays) and are broadcasted over, whereas in this case you want them treated as "scalars" (i.e. not broadcasted over). You can suppress this with Ref or `RefValue:

julia> output = getindex.(input, Base.RefValue(4:6))
3-element Array{Array{Int64,1},1}:
 [4, 5, 6]      
 [40, 50, 60]   
 [400, 500, 600]

(Base.RefValue is a bit annoying here; see also #27608 for a possibly better syntax in the future.)

@stevengj stevengj added the broadcast Applying a function over a collection label Jul 11, 2018
@stevengj
Copy link
Member

Closing as this is not a bug.

@MoonCoral
Copy link
Contributor Author

Thank you for clarifying this and your advise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
broadcast Applying a function over a collection
Projects
None yet
Development

No branches or pull requests

2 participants