-
-
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
Make CartesianRange an AbstractArray #24715
Conversation
An interesting and creative idea; I don't think this has ever occurred to me. With this, it seems like one might even be able to get rid If I try to look into my crystal ball (something I'm not always great at), I think the biggest downside might be the risk of more ambiguities. That doesn't seem likely to be serious enough to block this. So I'm (ever so slightly nervously) 👍 on this. |
99548f2
to
4c0d0c6
Compare
OK, I made the calculation slightly more elegant (imho) by moving everything into the CR = CartesianRange((1:3, 1:5))
CR[4] # CartesianIndex(1, 2)
ind2sub((3,5), 4) # (1,2) The inverse is more cumbersome, however: rs = reshape(linearindices(CR), size(CR))
rs[1,2] # 4
sub2ind((3,5), 1, 2) # 4 On the other hand, once you have |
It would be nice to be able to get rid of |
If there is consensus that this is a good idea, one approach would be as follows:
linearmapping(iter::CartesianRange) = reshape(linearindices(iter), size(iter)) Then I'd be willing to extend this PR if this seems feasible for 0.7, and if someone can help with carefully checking that there are no performance regressions. |
This is something that I've considered myself, but I had been putting it in the same "performance trap" bucket as we have done for iterating over dimensions of individual CartesianIndexes. This is significantly different, though, since we have a way of stating the optimal way of indexing into arrays. Heck, if you need lots of Maybe we should change what The logical conclusion, then, I think would be to have Even better, this would mean that Just a brainstorm here. I like this, though. |
@mbauman I like the elegance of those ideas, but wouldn't this conflict with the custom indices specification, where different indexing types can be chosen along each dimension? Also, I tried changing indices as you suggested, but can't get sysimage to build or even get a successful evaluation using Revise.jl, so I guess this would be a very far-reaching change. On the other hand, I noticed |
I have added the I'll look into deprecating |
04dd4e7
to
4a6a566
Compare
Triage finds this to be a lovely idea and would love to see it merged :) |
I'm wondering if |
I was just working on the sub2ind((0:3,3:5), 0, 3)
CartesianToLinear(0:3,3:5)[0,3]
ind2sub((0:3,3:5), 1)
CartesianRange(0:3,3:5)[1] This also has a nice symmetry to it. |
I've added the commit that deprecates |
It feels a bit strange to me to have a type called |
The reason it is a type is for symmetry with linear = CartesianToLinear(dims)
for i in ...
for j in ...
k = linear[i,j]
...
end
end I agree it's possible to find a better name for the type, however :) |
056f7d4
to
d26672d
Compare
Sorry I lost track of this. I fixed some merge conflicts. This seems ready to merge when CI passes again. The CI queue seems likely to be stalled out today, but that should not prevent this from being merged for 1.0. |
Updated version in #25113 |
This is needed in response to JuliaLang#24715.
The use case for this change is easy conversion between linear and cartesian indices, which can be done by linearly indexing into the CartesianRange now.