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 getindexwith colon unexpected #31109

Closed
KlausC opened this issue Feb 19, 2019 · 2 comments
Closed

broadcasting getindexwith colon unexpected #31109

KlausC opened this issue Feb 19, 2019 · 2 comments

Comments

@KlausC
Copy link
Contributor

KlausC commented Feb 19, 2019

Is the following intended or a bug?
I was surprised, because changing 1:4 to : changed the output type. I expected to see the same vector of vectors in both cases.

julia> v
2×4 Array{Complex{Float64},2}:
     -1.0+0.0im   0.739183+0.196401im   0.739183-0.196401im  0.627138+0.0im
 0.821812+0.0im  -0.501408+0.375337im  -0.501408-0.375337im       1.0+0.0im

julia> getindex.(Ref(v), Ref(:), 1:4)
4-element Array{Array{Complex{Float64},1},1}:
 [-1.0 + 0.0im, 0.8218121719277804 + 0.0im]                                             
 [0.7391830349599989 + 0.1964008799517751im, -0.501407601380187 + 0.37533731526626024im]
 [0.7391830349599989 - 0.1964008799517751im, -0.501407601380187 - 0.37533731526626024im]
 [0.6271384123034096 + 0.0im, 1.0 + 0.0im]                                              

julia> getindex.(Ref(v), Ref(:), :)
2×4 Array{Complex{Float64},2}:
     -1.0+0.0im   0.739183+0.196401im   0.739183-0.196401im  0.627138+0.0im
 0.821812+0.0im  -0.501408+0.375337im  -0.501408-0.375337im       1.0+0.0im

julia> 
@GunnarFarneback
Copy link
Contributor

The broadcast machinery effectively expands

getindex.(Ref(v), Ref(:), 1:4)

to

[getindex(v, :, 1), getindex(v, :, 2), getindex(v, :, 3), getindex(v, :, 4)]

or equivalently

[v[:, 1], v[:, 2], v[:, 3], v[:, 4]]

since 1:4 is iterable.

For

getindex.(Ref(v), Ref(:), :)

all arguments are scalars (: is just a generic function and Ref(:) doesn't make a difference) so it expands to

getindex(v, :, :)

i.e. v[:, :].

The key observation is that : only can be interpreted as "all indices" within the context of the getindex function. The broadcast machinery is fully generic and doesn't try to guess how some specific function like getindex will interpret its arguments.

@mbauman
Copy link
Member

mbauman commented Feb 20, 2019

Duplicate of #30450

@mbauman mbauman marked this as a duplicate of #30450 Feb 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants