[Numpy-svn] r3182 - in trunk/numpy/lib: . tests

numpy-svn at scipy.org numpy-svn at scipy.org
Mon Sep 18 18:27:36 CDT 2006


Author: stefan
Date: 2006-09-18 18:27:24 -0500 (Mon, 18 Sep 2006)
New Revision: 3182

Modified:
   trunk/numpy/lib/arraysetops.py
   trunk/numpy/lib/function_base.py
   trunk/numpy/lib/tests/test_arraysetops.py
Log:
Add ediff1d support for empty arrays.
Fix ediff1d for to_begin or to_end = 0.
Allow insert to operate on empty arrays.


Modified: trunk/numpy/lib/arraysetops.py
===================================================================
--- trunk/numpy/lib/arraysetops.py	2006-09-18 21:04:31 UTC (rev 3181)
+++ trunk/numpy/lib/arraysetops.py	2006-09-18 23:27:24 UTC (rev 3182)
@@ -34,28 +34,17 @@
 
 ##
 # 03.11.2005, c
-def ediff1d( ar1, to_end = None, to_begin = None ):
+def ediff1d(ary, to_end = None, to_begin = None):
     """Array difference with prefixed and/or appended value."""
-    dar1 = ar1[1:] - ar1[:-1]
-    if to_end and to_begin:
-        shape = (ar1.shape[0] + 1,) + ar1.shape[1:]
-        ed = numpy.empty( shape, dtype = ar1.dtype )
-        ed[0], ed[-1] = to_begin, to_end
-        ed[1:-1] = dar1
-    elif to_end:
-        ed = numpy.empty( ar1.shape, dtype = ar1.dtype )
-        ed[-1] = to_end
-        ed[:-1] = dar1
-    elif to_begin:
-        ed = numpy.empty( ar1.shape, dtype = ar1.dtype )
-        ed[0] = to_begin
-        ed[1:] = dar1
-    else:
-        ed = dar1
-
+    ary = numpy.asarray(ary)
+    ed = ary[1:] - ary[:-1]
+    if to_begin is not None:
+        ed = numpy.insert(ed, 0, to_begin)
+    if to_end is not None:
+        ed = numpy.append(ed, to_end)
+        
     return ed
 
-
 ##
 # 01.11.2005, c
 # 02.11.2005
@@ -95,11 +84,11 @@
 # 01.11.2005, c
 def setxor1d( ar1, ar2 ):
     """Set exclusive-or of 1D arrays with unique elements."""
-    aux = numpy.concatenate( (ar1, ar2 ) )
+    aux = numpy.concatenate((ar1, ar2))
     aux.sort()
-    flag = ediff1d( aux, to_end = 1, to_begin = 1 ) == 0
-    flag2 = ediff1d( flag, 0 ) == 0
-    return aux.compress( flag2 )
+    flag = ediff1d(aux, to_end = 1, to_begin = 1) == 0
+    flag2 = ediff1d(flag) == 0
+    return aux.compress(flag2)
 
 ##
 # 03.11.2005, c

Modified: trunk/numpy/lib/function_base.py
===================================================================
--- trunk/numpy/lib/function_base.py	2006-09-18 21:04:31 UTC (rev 3181)
+++ trunk/numpy/lib/function_base.py	2006-09-18 23:27:24 UTC (rev 3182)
@@ -1286,7 +1286,7 @@
     newshape = list(arr.shape)
     if isinstance(obj, (int, long, integer)):
         if (obj < 0): obj += N
-        if (obj < 0 or obj >=N):
+        if (obj < 0 or obj > N or (obj == N and N != 0)):
             raise ValueError, "index (%d) out of range (0<=index<=%d) "\
                   "in dimension %d" % (obj, N, axis)
         newshape[axis] += 1;

Modified: trunk/numpy/lib/tests/test_arraysetops.py
===================================================================
--- trunk/numpy/lib/tests/test_arraysetops.py	2006-09-18 21:04:31 UTC (rev 3181)
+++ trunk/numpy/lib/tests/test_arraysetops.py	2006-09-18 23:27:24 UTC (rev 3182)
@@ -6,6 +6,7 @@
 set_package_path()
 import numpy
 from numpy.lib.arraysetops import *
+from numpy.lib.arraysetops import ediff1d
 restore_path()
 
 ##################################################
@@ -68,6 +69,17 @@
         c = setxor1d( a, b )
         assert_array_equal( c, ec )
 
+    def check_ediff1d(self):
+        zero_elem = numpy.array([])
+        one_elem = numpy.array([1])
+        two_elem = numpy.array([1,2])
+        
+        assert_array_equal([],ediff1d(zero_elem))
+        assert_array_equal([0],ediff1d(zero_elem,to_begin=0))
+        assert_array_equal([0],ediff1d(zero_elem,to_end=0))
+        assert_array_equal([-1,0],ediff1d(zero_elem,to_begin=-1,to_end=0))
+        assert_array_equal([],ediff1d(one_elem))
+        assert_array_equal([1],ediff1d(two_elem))
 
     ##
     # 03.11.2005, c



More information about the Numpy-svn mailing list