Skip to content

Add `return_index` option to set operations (`setxor1d`, etc) #458

Closed
wants to merge 1 commit into from

2 participants

@bfroehle

Not ready for merge, just asking for comments.

From numpy.lib.arraysetops:

To do: Optionally return indices analogously to unique for all functions.

I've gone ahead and implemented a return_index parameter for one function -- setxor1d -- and I'd like some feedback before I plunge ahead to write the corresponding options for the rest of the functions (setdiff1d, union1d, etc).

Questions to be addressed:

  1. Is the name return_index alright, even thought it returns two sets of indices? It is the same name as used in np.unique.
  2. Will you accept this without a corresponding return_inverse? I would think yes, as MATLAB doesn't allow for such an option and it's not necessarily obvious what it would even mean.
  3. Code style... is it alright? Or would you prefer more of a global block format:
    def setxor1d(ar1, ar2, assume_sorted=False, return_index=False):

        if return_index:
            < new code >
        else:
            < old code >
@teoliphant
NumPy member

This seems OK to me.

But, I would prefer that you create a new function for the case of return_index = True. This has always been Guido's suggestion, though we don't follow it very well in NumPy.

For example, you could use the postfix _idxs to indicate the return_index = True:

setxor1d_idxs, etc.

@bfroehle

While we are discussing options, another would be something like argsetxor1d which could be to setxor1d as argsort is to sort.

That is,

>>> a = np.array( [5, 7, 1, 2] )
>>> b = np.array( [2, 4, 3, 1, 5] )
>>> ia, ib = argsetxor1d(a, b)
>>> ia
array([1])
>>> a[ia]
array([7])
>>> ib
array([2, 1])
>>> b[ib]
array([3, 4])
>>> np.alltrue(setxor1d(a, b) == np.sort(np.concatenate((a[ia], b[ib]))))
True
@bfroehle bfroehle closed this Feb 12, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.