-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
RFC (C=collaborator): multidimensional comprehensions that use CartesianRange iterators #10523
Comments
With the upcoming changes, this will be easy to implement, perhaps by defining methods for |
Historically, Now, you could write the above as B = [A[I]+7 for I in CartesianRange(size(A))] which I think solves some of your concerns. |
How are we doing on this? Would be nice to be able to include comprehensions in #15434. |
Ideally, #14770 and
could be solved jointly. |
I'm not sure I see why they are joint? We also have a very good mechanism to express the shape of the comprehension ( |
If there is a syntax for linear indexing, say for the sake of argument |
I thought the closest thing to consensus in that discussion was to keep linear indexing, just not "partial linear indexing." (i.e., |
This doesn't work for arrays that are not one-based, or which use strided indexing. They may not be in Base, but it would be nice to support them anyway. |
I believe the shape of a comprehension should depend entirely on the iterable. Simplest is |
In some situations it would be easy to implement [f(a) for a in reshape(A, sz)]
[f(A[I]) for I in eachindex(reshape(A, sz))] can be expected to work in general. I'm less sure that iter = some_iter_constructor(A) # e.g., eachindex
[f(A[I]) for I in reshape(iter)] will be something that will give good results in general. I don't have access to the right machine now, but tonight I can push my WIP ReshapedArray branch so you can see what I'm planning. Briefly, eachindex(R::ReshapedArray) = ReshapedIterator(R)
getindex(R::ReshapedArray, I::ReshapedIndex) = R.parent[I.Iparent] # I.Iparent is an index instance of the type favored for the parent effectively turning iteration over a reshaped array into iteration over the parent. |
I agree there is something slightly off about wrapping an iterator just to change its shape. Maybe I should question whether anybody is actually bothered by, say, a product iterator being "N-d". |
The solution might be to be very careful when giving an iterator a shape and rather introduce a new name for a shaped iterator variant. Then the principle |
I like N-d product iterators. Before talking about fancy reshape stuff, I should have also said what was surely obvious to you, that |
@mschauer Couldn't we have |
Thinking about it again... yes, we could just do that. |
This is fixed now if you iterate over |
Loving it already. |
It would be sweet to be able to create a 3-dimensional array like this:
Looks like comprehensions are performed in the scheme code, and I've never gotten around to (and probably won't get around to) teaching myself lisp (too many other things to do). Anyone interested in collaborating on implementing this? I don't know how much I can do on the julia side, but happy to help if I can.
Reference:
http://stackoverflow.com/questions/29050763/slicing-and-broadcasting-multidimensional-arrays-in-julia-meshgrid-example/29060865#29060865
The text was updated successfully, but these errors were encountered: