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

Index multi-dimensional arrays with a tuple? #6856

Closed
wsliang opened this issue May 15, 2014 · 6 comments
Closed

Index multi-dimensional arrays with a tuple? #6856

wsliang opened this issue May 15, 2014 · 6 comments
Labels
domain:arrays [a, r, r, a, y, s]

Comments

@wsliang
Copy link
Contributor

wsliang commented May 15, 2014

Would it make sense to allow tuples to index multi-dimensional arrays? For example, if A is a 2D matrix, then A[(2, 1)] would do the same thing as A[2,1].

The rationale for this is that sometimes it is more convenient to work with, and pass around, a single variable which contains all the indexing information, instead of d separate coordinates. For example, if loc is an (Int, Int) tuple representing the current location on a grid, it would be nice to be able to write:

loc = next_location(loc)
sum += A[loc]

instead of sum += A[loc...].

One potential cause for confusion is that this would mean A[[1:2]] and A[(1, 2)] would do different things. However, seeing as A[(1, 2)] is currently undefined, it seems like it should be ok.

@simonster
Copy link
Member

This proposal would seem to conflict with the plans to use tuples as fixed-size immutable arrays (#5857), which would suggest that A[(2, 1)] should be equivalent to A[[2, 1]].

@JeffBezanson
Copy link
Sponsor Member

It might be possible in the future to wrap a tuple in an immutable type with no overhead.

@toivoh
Copy link
Contributor

toivoh commented May 15, 2014

Still, you can already to A[(1,2)...] to get the same effect, and i suspect
it would equally efficient as well. (In both cases, as long as the size of
the tuple is known, you're good.)

@cbecker
Copy link
Contributor

cbecker commented May 15, 2014

My opinion is that, regardless of what is done, it has to be clear what such operation means.
In that sense, A[myTuple...] is very intuitive.

@kshyatt kshyatt added the domain:arrays [a, r, r, a, y, s] label Sep 15, 2016
@mbauman
Copy link
Sponsor Member

mbauman commented Sep 15, 2016

This can now be done by wrapping the tuple in a CartesianIndex. In fact, you can mix-and-match CartesianIndexes, integers, arrays, and arrays of CartesianIndex together seamlessly. And in most cases the wrapper can be optimized away. Between that and splatting, I think this is well covered.

@mbauman mbauman closed this as completed Sep 15, 2016
@timholy
Copy link
Sponsor Member

timholy commented Sep 15, 2016

It definitely is. One still might wonder about defining all the operations we allow on CartesianIndex for tuples, and then we wouldn't even need CartesianIndex. But that's a little scary too.

Semi-related: recent case in which we discuss indexing with tuples-of-ClosedIntervals, and had to define some slightly unconventional methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays [a, r, r, a, y, s]
Projects
None yet
Development

No branches or pull requests

8 participants