From 06db31eac5bf957778c5b95f813b4cd0d86ed012 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Tue, 19 Sep 2006 00:00:03 +0000 Subject: Fix unique1d for empty sets. Closes r208. --- numpy/lib/arraysetops.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'numpy/lib/arraysetops.py') diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py index c7449eb12..a12185b41 100644 --- a/numpy/lib/arraysetops.py +++ b/numpy/lib/arraysetops.py @@ -48,20 +48,24 @@ def ediff1d(ary, to_end = None, to_begin = None): ## # 01.11.2005, c # 02.11.2005 -def unique1d( ar1, retindx = False ): +def unique1d(ar1, retindx=False): """Unique elements of 1D array. When ret_indx is True, return also the indices indx such that ar1.flat[indx] is the resulting array of unique elements.""" + ar = numpy.asarray(ar1).ravel() + if ar.size == 0: + if retindx: return numpy.empty(0, numpy.bool), ar + else: return ar + if retindx: - ar = numpy.array(ar1).ravel() perm = ar.argsort() aux = ar.take(perm) - flag = ediff1d( aux, 1 ) != 0 + flag = ediff1d(aux, 1) != 0 return perm.compress(flag), aux.compress(flag) + else: - ar = numpy.array( ar1 ).flatten() - ar.sort() - return ar.compress( ediff1d( ar, 1 ) != 0) + ar = numpy.array(sorted(ar)) + return ar.compress(ediff1d(ar, 1) != 0) ## # 01.11.2005, c -- cgit v1.2.1