-
-
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
NegatedIndex type #1032
Comments
What does it do on integer vectors? |
I'm basically thinking that the structure looks like this: type NegatedIndex{T}
idx::T
end So you just keep the original object until its time to do the actual indexing, at which point you know what you're "subtracting" the index object from, which lets you compute the actual index set and then do regular indexing with that. So |
We do not define That said, I do like the idea, and here are some thoughts.
A complement operator may even be useful outside of indexing, like in loop iterations and such. |
Looking at the names in http://en.wikipedia.org/wiki/Complement_(set_theory), we could even just have a functional interface, using |
I really think that the |
-viral On 12-Jul-2012, at 11:40 PM, Stefan Karpinski wrote:
|
I think it seems a bit fishy to define |
It is a little weird, but I was thinking that you resolve, e.g. |
I'm a bit worried about surprises that might come out of this. E g you might expect The way interpret it,
would always partition the elements of |
This seems to have fizzled out, but I still really, really want this functionality to be added. It's one of the most convenient tricks in R for describing data resampling. See the jackknife for the simplest example of a statistical method in which this notation helps. I just discovered that one of the other DataFrame developers hacked together an implementation of this style of indexing for the columns DataFrames simply because it was so unnatural to articulate one algorithm without it. |
If I'm understanding this correctly, the negated index Can someone please explain the following: "For example, the following creates a vector slice with the third member removed. Why wouldn't |
Yes, that's absolutely right. I just made a bunch of mistakes jumbled together in that post :-) |
I'm looking into this. |
You might not have to; define |
Of course, you'll also need to look at |
We might have to change the indexing code to explicitly combine dimension sizes with indexes to allow implementing things like this (and
we'd write
|
Jeff, can you elaborate on that? I'm failing to see the need for it – not that there isn't one, I'm just not seeing why. |
Because there is no way to implement iteration of something like |
Yes, obviously you need a context for that. The part I needed clarification of was "change the indexing code" – what indexing code are you referring to? |
Mostly works on the following index types: (Int, Range1{Int}, Range{Int}) Does not yet work for index of Array{Int}
After looking at some code with @punkrockpolly, I'm wondering if the best approach here isn't to have a immutable Complement{T}
collection::T
end
in(x,c::Complement) = !in(x,c.collection) Then you can also do things like this: getindex(v::AbstractVector, c::Complement) = v[filter!(i->in(i,c), 1:end)] Of course, that doesn't cover everything, but I think the limited scope of the |
Paired on this with @punkrockpolly, see: https://github.com/punkrockpolly/Playing-with-Julia/blob/master/negatedindex.jl The premise of the Complement type is that it abstracts the complement of a collection – primarily that `x in c` <=> `!(x in c.collection)` This commit punts on indexing for more than two dimensions because that turns out to be an incredibly invasive change to huge amounts the multidimensional array indexing code.
I am closing this, since this is something that can easily live in a package. Please reopen if you think otherwise. |
I'd suggest putting this into SubArray as a valid index type---eventually |
Well, #10525 would allow us to define this once and transform the complement to direct indices (or potentially even doing so without any intermediates). One thing I realized the other day is that we almost have a negated index type already in base. It's the complement IntSet. If IntSets were not in the range 0:(typemax(Int)-1), but instead 1:typemax(Int), it'd actually simplify quite a bit of their implementation. And it'd make |
Cool. But AFAIK the order of elements is not defined for |
It's documented as being sorted, which seems sensible. |
Ah, and yes, my changes in #10331 for indexing with |
Would JuliaLang/LinearAlgebra.jl#255 allow "negated" indexes for array-like constructs with named indexes? (which map keys to indexes, like |
Not directly, no. Both SubArray and fallback non-scalar indexing have converged to simply re-index into the stored or passed indices. The difficulty with using IntSets and complement types as indices is that they do not directly support fast indexed access. I don't want more special cases, so that means doing what we do with logical vectors: transform them to normal index vectors after checking their bounds. Perhaps the only action item for Base is to change the |
See https://github.com/mbauman/InvertedIndices.jl for an implementation of this idea as a package. |
Is there any chance this would be included in Base? People keep asking about it: |
Idea spawned by this discussion. The idea is that just as writing
v[b], v[!b]
whereb
is a logical indexing vector partitionsv
into two disjoint sets of elements, writingv[x], x[!x]
should also partitionv
into two disjoint sets of elements whenx
is a single index or a range of indices. This requires some newNegatedIndex
type, together with!
methods for integers and ranges and the appropriate method for indexing arrays withNegatedIndex
objects.The text was updated successfully, but these errors were encountered: