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

set-like operations #14

Open
asmeurer opened this issue Apr 29, 2020 · 2 comments
Open

set-like operations #14

asmeurer opened this issue Apr 29, 2020 · 2 comments

Comments

@asmeurer
Copy link
Member

It would also be useful to have various set-like operations, treating the elements indexed by an index as a set

in meaning subset
+ and & meaning union
| meaning intersection
- meaning subtraction (idx1 - idx2 are the elements indexed by idx1 but not idx2)

For example, idx1 in idx2 would return True if all the elements indexed by idx1 are also indexed by idx2. idx1 + idx2 would give an index representing the elements indexed by either idx1 or idx2.

There will be lots of cases where they have to raise exceptions obviously because it can't be computed. I don't know if the canonical form after reduce(shape) will always avoid that, or if we would also need method variants that can include a shape. There will also be cases where the index can't be represented except by an array index.

Another issue is that for integer array indices, the same element can be indexed multiple times, so it doesn't completely make sense to treat it as a set.

@asmeurer asmeurer added this to the Low Priority milestone May 13, 2020
@asmeurer
Copy link
Member Author

asmeurer commented Jun 2, 2020

We could also use < for subset.

@telamonian
Copy link
Contributor

telamonian commented Oct 13, 2020

Another issue is that for integer array indices, the same element can be indexed multiple times, so it doesn't completely make sense to treat it as a set.

You'd have to treat them as bags (aka multisets). Which could definitely be useful in some contexts, but is probably not intuitive for anyone to justify the syntactic sugar. For example, I could not tell off the top of my head how x & y or x | y is supposed to work if x and y are bags containing different multiplicities of some element a.

On the other hand, I think it would make a great deal of sense to include these set (and/or bag) ops as methods for the index types, with the same names as the methods of the set builtin.

Maybe down the road, once ndindex is more mature and it's API has gelled, we could then consider adding the op overloading sugar, if it still makes sense.

Edit

After 5 minutes of reading https://en.wikipedia.org/wiki/Multiset, I know understand how the set-like ops work for bags. It's not super complicated, but I think my point about it being unintuitive for most users still stands

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants