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

accessing arrays via tuples #6940

Closed
vgupta1 opened this issue May 23, 2014 · 13 comments
Closed

accessing arrays via tuples #6940

vgupta1 opened this issue May 23, 2014 · 13 comments

Comments

@vgupta1
Copy link

vgupta1 commented May 23, 2014

Hi All,

This may be by design, but I found it confusing:

x = rand(10)
x[ [1, 3, 5] ] # works fine
x[ (1, 3, 5) ]  # does not work....

Was this intentional please? I may be missing something, but I would have thought it possible to index a vector using any iterable collection. Happy for someone to explain to me what I'm missing.

@IainNZ Thought you'd be interested in response.

@pao
Copy link
Member

pao commented May 23, 2014

Dup of #6856. That issue also includes a workaround for this.

@pao pao closed this as completed May 23, 2014
@IainNZ
Copy link
Member

IainNZ commented May 23, 2014

Well not quite, because splatting works when you want to index into multiple dimensions, but what @vgupta1 wants is to actually take multiple values. Which seems more #5857 related.

@StefanKarpinski
Copy link
Member

Agreed, this isn't a dup, IMO, but perhaps @vgupta1 can clarify.

@vgupta1
Copy link
Author

vgupta1 commented May 23, 2014

@IainNZ is right.. sorry for the confusion. I'd like the return type to be a Vector of Floats of length 3 in both cases.

@pao
Copy link
Member

pao commented May 23, 2014

Oops! My mistake. Though the ambiguity between the two is probably worth noting.

@IainNZ
Copy link
Member

IainNZ commented May 23, 2014

I think the conceptual glitch is that tuples act a lot like arrays that you can't change the size of, so its quite unexpected for this to not work.

Like, this works, and I tihnk is quite understandable/logical/consistent:

julia> x=rand(5,5)
5x5 Array{Float64,2}:
 0.4566    0.369542   0.795675  0.0495695  0.663   
 0.964897  0.391803   0.826508  0.633251   0.271672
 0.578666  0.0782035  0.273646  0.984753   0.140072
 0.125985  0.216062   0.239997  0.0356655  0.786063
 0.14152   0.613532   0.640586  0.878196   0.616077

julia> x[[2,4]...]
0.6332509202521432

julia> x[(2,4)...]
0.6332509202521432

but what @vgupta1 is doing doesn't.

@StefanKarpinski
Copy link
Member

It seems sensible for me that this should work, but maybe there's some issue I'm not thinking of. Maybe @andreasnoackjensen, @jiahao, @stevengj or @JeffBezanson can think of some problem.

@JeffBezanson
Copy link
Member

For Dicts, we've already equated indexing with a tuple with multi-argument indexing. I've always thought it would be nice to generalize linear indexing so that any indexing can be done efficiently with a single index object. Tuples are a natural candidate for n-d array index objects. I do see the potential for confusion though.

Comparing to a[X...] vs. a[Y...] is not convincing, because ... supports any iterable whatsoever, with the type of X and Y naturally being irrelevant.

@andreasnoack
Copy link
Member

For indexing, I think I'd expect tuples to behave like A[(2,3)]=A[(2,3)...] more than A[(2,3)]=A[[2,3]].

@tknopp
Copy link
Contributor

tknopp commented Jun 2, 2014

I would also find that tuples are a natural representation for a multiindex (i.e. vote for A[(2,3)]=A[(2,3)...]). How usefull/necessary this is, is also related to #1917 if that would use tuples as multiindices.

@JeffBezanson
Copy link
Member

Another point is that the kind of indexing done by A[[1,2,3]] depends on the index having a rank. For example, we might adopt the APL rule that the rank of the result is the sum of the ranks of the indexes. [1,2,3] is explicitly a 1d object, but it's less clear whether a tuple has an array rank.

@StefanKarpinski
Copy link
Member

I think that given the ambiguity between whether A[(1,2,3)] should mean A[1,2,3] or [ A[k] for k=(1,2,3) ] it seems to me that the only safe thing to do here is leave it undefined and force people to explicitly pick which one they mean.

@JeffBezanson
Copy link
Member

Yes, I think we're going to leave this alone for now.

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

7 participants