diff options
Diffstat (limited to 'numpy/lib/arraysetops.py')
-rw-r--r-- | numpy/lib/arraysetops.py | 174 |
1 files changed, 103 insertions, 71 deletions
diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index f8304cced..ad8d2d645 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -38,23 +38,26 @@ __all__ = ['ediff1d', 'unique1d', 'intersect1d', 'intersect1d_nu', 'setxor1d', import time import numpy as nm -def ediff1d(ary, to_end = None, to_begin = None): +def ediff1d(ary, to_end=None, to_begin=None): """The differences between consecutive elements of an array, possibly with prefixed and/or appended values. - :Parameters: - - `ary` : array + Parameters + ---------- + ary : array This array will be flattened before the difference is taken. - - `to_end` : number, optional + to_end : number, optional If provided, this number will be tacked onto the end of the returned differences. - - `to_begin` : number, optional + to_begin : number, optional If provided, this number will be taked onto the beginning of the returned differences. - :Returns: - - `ed` : array + Returns + ------- + ed : array The differences. Loosely, this will be (ary[1:] - ary[:-1]). + """ ary = nm.asarray(ary).flat ed = ary[1:] - ary[:-1] @@ -77,22 +80,26 @@ def unique1d(ar1, return_index=False): Most of the other array set operations operate on the unique arrays generated by this function. - :Parameters: - - `ar1` : array + Parameters + ---------- + ar1 : array This array will be flattened if it is not already 1D. - - `return_index` : bool, optional + return_index : bool, optional If True, also return the indices against ar1 that result in the unique array. - :Returns: - - `unique` : array + Returns + ------- + unique : array The unique values. - - `unique_indices` : int array, optional + unique_indices : int array, optional The indices of the unique values. Only provided if return_index is True. - :See also: - numpy.lib.arraysetops has a number of other functions for performing set - operations on arrays. + See Also + -------- + numpy.lib.arraysetops : Module with a number of other functions + for performing set operations on arrays. + """ ar = nm.asarray(ar1).flatten() if ar.size == 0: @@ -110,66 +117,78 @@ def unique1d(ar1, return_index=False): flag = nm.concatenate( ([True], ar[1:] != ar[:-1]) ) return ar[flag] -def intersect1d( ar1, ar2 ): +def intersect1d(ar1, ar2): """Intersection of 1D arrays with unique elements. Use unique1d() to generate arrays with only unique elements to use as inputs to this function. Alternatively, use intersect1d_nu() which will find the unique values for you. - :Parameters: - - `ar1` : array - - `ar2` : array + Parameters + ---------- + ar1 : array + ar2 : array + + Returns + ------- + intersection : array - :Returns: - - `intersection` : array + See Also + -------- + numpy.lib.arraysetops : Module with a number of other functions for + performing set operations on arrays. - :See also: - numpy.lib.arraysetops has a number of other functions for performing set - operations on arrays. """ aux = nm.concatenate((ar1,ar2)) aux.sort() return aux[aux[1:] == aux[:-1]] -def intersect1d_nu( ar1, ar2 ): +def intersect1d_nu(ar1, ar2): """Intersection of 1D arrays with any elements. The input arrays do not have unique elements like intersect1d() requires. - :Parameters: - - `ar1` : array - - `ar2` : array + Parameters + ---------- + ar1 : array + ar2 : array - :Returns: - - `intersection` : array + Returns + ------- + intersection : array + + See Also + -------- + numpy.lib.arraysetops : Module with a number of other functions for + performing set operations on arrays. - :See also: - numpy.lib.arraysetops has a number of other functions for performing set - operations on arrays. """ # Might be faster than unique1d( intersect1d( ar1, ar2 ) )? aux = nm.concatenate((unique1d(ar1), unique1d(ar2))) aux.sort() return aux[aux[1:] == aux[:-1]] -def setxor1d( ar1, ar2 ): +def setxor1d(ar1, ar2): """Set exclusive-or of 1D arrays with unique elements. Use unique1d() to generate arrays with only unique elements to use as inputs to this function. - :Parameters: - - `ar1` : array - - `ar2` : array + Parameters + ---------- + ar1 : array + ar2 : array - :Returns: - - `xor` : array + Returns + ------- + xor : array The values that are only in one, but not both, of the input arrays. - :See also: - numpy.lib.arraysetops has a number of other functions for performing set - operations on arrays. + See Also + -------- + numpy.lib.arraysetops : Module with a number of other functions for + performing set operations on arrays. + """ aux = nm.concatenate((ar1, ar2)) if aux.size == 0: @@ -182,24 +201,28 @@ def setxor1d( ar1, ar2 ): flag2 = flag[1:] == flag[:-1] return aux[flag2] -def setmember1d( ar1, ar2 ): +def setmember1d(ar1, ar2): """Return a boolean array of shape of ar1 containing True where the elements of ar1 are in ar2 and False otherwise. Use unique1d() to generate arrays with only unique elements to use as inputs to this function. - :Parameters: - - `ar1` : array - - `ar2` : array + Parameters + ---------- + ar1 : array + ar2 : array - :Returns: - - `mask` : bool array + Returns + ------- + mask : bool array The values ar1[mask] are in ar2. - :See also: - numpy.lib.arraysetops has a number of other functions for performing set - operations on arrays. + See Also + -------- + numpy.lib.arraysetops : Module with a number of other functions for + performing set operations on arrays. + """ ar1 = nm.asarray( ar1 ) ar2 = nm.asarray( ar2 ) @@ -225,42 +248,51 @@ def setmember1d( ar1, ar2 ): return flag[indx] -def union1d( ar1, ar2 ): - """Union of 1D arrays with unique elements. +def union1d(ar1, ar2): + """ + Union of 1D arrays with unique elements. Use unique1d() to generate arrays with only unique elements to use as inputs to this function. - :Parameters: - - `ar1` : array - - `ar2` : array + Parameters + ---------- + ar1 : array + ar2 : array - :Returns: - - `union` : array + Returns + ------- + union : array + + See also + -------- + numpy.lib.arraysetops : Module with a number of other functions for + performing set operations on arrays. - :See also: - numpy.lib.arraysetops has a number of other functions for performing set - operations on arrays. """ return unique1d( nm.concatenate( (ar1, ar2) ) ) -def setdiff1d( ar1, ar2 ): +def setdiff1d(ar1, ar2): """Set difference of 1D arrays with unique elements. Use unique1d() to generate arrays with only unique elements to use as inputs to this function. - :Parameters: - - `ar1` : array - - `ar2` : array + Parameters + ---------- + ar1 : array + ar2 : array - :Returns: - - `difference` : array + Returns + ------- + difference : array The values in ar1 that are not in ar2. - :See also: - numpy.lib.arraysetops has a number of other functions for performing set - operations on arrays. + See Also + -------- + numpy.lib.arraysetops : Module with a number of other functions for + performing set operations on arrays. + """ aux = setmember1d(ar1,ar2) if aux.size == 0: |