-
-
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
Ellipsis slicing #5405
Comments
+1. I find myself using this a lot in numpy. I also like the numpy |
+1 for newaxis and something that works like ... in numpy (I'm not sure if |
In Python 3.x
You can use it as a prettier alternative to None, if you want. In Python 2.7, however, you cannot just write
In Julia, we have to decide weather we choose one of both or just don't allow |
In a sense this is already here, although at the scalar level rather than the vectorized level: R = CartesianRange(size(A)[1:end-1])
for j = 1:size(A,ndims(A))
for i in R
# do something with A[i,j]
end
end In fact it's even better than that, because you can do this with "middle indexes," see #10814, which is presumably impossible with ellipsis indexing. There are still some challenges with regards to type-stability, at least in context of the example in #10814. But for this example, I suspect it would be OK (I haven't tested). |
If we do decide to add this, it'd be good to try it out first before adding syntax. We could experiment with allowing indexing with CartesianRange objects directly. My hunch, though, is that this isn't needed nearly as much as it is in NumPy since we don't need to bend over backwards to vectorize things for performance. In fact, we do the opposite... |
I don't have an opinion on the syntax, but it has been pointed out before (No bending over backwards, though...!) On Thursday, April 23, 2015, Matt Bauman [email protected] wrote:
|
@timholy Does that example actually work? |
Indeed. Indexing with any combination of |
Writing |
For the vectorized access a not very intrusive approach is to define
and friends. This does not need any additional infrastructure. |
Yes, as @mschauer mentioned, this isn't about forcing vectorization, but rather it is the natural thing to do in many cases. This allows you to index 1,...,n arbitrary size tensors, and because it's in a standard array it's very performant (and can make clean code in conjunction with the f.() syntax). Your fix there is a good stand-in until something more general comes along. |
@mbauman contributed a great solution to EllipsisNotation.jl which essentially solves this problem. https://github.com/ChrisRackauckas/EllipsisNotation.jl It handles cases like I for one think it would be nice to have this in Base so it could be standardized as part of Julia notation. It would also solve the #24069 problem of wanting a type for all indices that keeps shape. Since the implementation is all there, I think the main question would be notation since interval arithmetic people seem to want |
If gets into Base, then it should use |
|
Amusingly We could use a word like |
I like the idea of sometime in the future having |
I prefer |
I would like to propose the Unicode symbol |
Hopefully this is still on the "future features" check-list. Related discourse discussion. |
I tried to make this possible in #24091, but that approach wasn't welcomed. Instead, someone would need to add this as a special form in the I'm not the biggest fan of adding a special case that works in (I hope you don't mind I took the liberty of fixing your link). |
There is not much discussion in #24091, but well, I can trust your judgement "not gonna happen". A pity in my opinion. |
Well, if we can vote for features, then I would say +1 for this syntax. It would be nice and clean if it would be fitted into the language. |
Note BTW that the objection above that |
And it would be fine too if it's defined in Base. But https://github.com/ChrisRackauckas/EllipsisNotation.jl is pretty clean and stable at this point. IMO it's about time for it to move to Base and be a standard part of Julia syntax. We can bikeshed the symbol for it, I don't care too much about it, but I think the implementation is correct enough now to be stable enough for Base. |
One argument for doing this at a lower level than EllipsisNotation is that it could avoid this problem (which is #35681): julia> Meta.@lower A[.., end]
:($(Expr(:thunk, CodeInfo(
@ none within `top-level scope`
1 ─ %1 = Base.lastindex(A, 2) # always 2, even if A has 4 dimensions
│ %2 = Base.getindex(A, .., %1)
└── return %2
))))
This was later reversed, so that IntervalSets.jl does not have to load the entire ArrayInterface.jl. |
This seems like a useful feature to me, I think it would just need someone to implement it. |
What part needs to be implemented? Or do you mean just copy-paste into Base? |
Making |
Numpy has this ellipsis notation for filling
index tuples up to dimension, where in
A[..., i, k]
...
is interpreted as shorthand for the number of colons:, :, : etc
needed to fillthe indexing tuple up to dimension
n
.http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
This gives nice access to contiguous subarrays in the dense array case and would be not more harmful than the
:
notation itself.Was this feature already considered? It was mentioned by @toivoh on the mailing list, but did not trigger discussion.
The text was updated successfully, but these errors were encountered: