[Numpy-svn] r3482 - in trunk/numpy: core random/mtrand

numpy-svn at scipy.org numpy-svn at scipy.org
Sat Dec 9 14:00:32 CST 2006


Author: oliphant
Date: 2006-12-09 14:00:27 -0600 (Sat, 09 Dec 2006)
New Revision: 3482

Modified:
   trunk/numpy/core/defchararray.py
   trunk/numpy/random/mtrand/mtrand.pyx
Log:
Fix shuffle and chararray printing for empty strings.

Modified: trunk/numpy/core/defchararray.py
===================================================================
--- trunk/numpy/core/defchararray.py	2006-12-08 03:23:10 UTC (rev 3481)
+++ trunk/numpy/core/defchararray.py	2006-12-09 20:00:27 UTC (rev 3482)
@@ -47,7 +47,7 @@
         if isinstance(val, (string_, unicode_)):
             temp = val.rstrip()
             if len(temp) == 0:
-                val = val[0]
+                val = ''
             else:
                 val = temp
         return val

Modified: trunk/numpy/random/mtrand/mtrand.pyx
===================================================================
--- trunk/numpy/random/mtrand/mtrand.pyx	2006-12-08 03:23:10 UTC (rev 3481)
+++ trunk/numpy/random/mtrand/mtrand.pyx	2006-12-09 20:00:27 UTC (rev 3482)
@@ -1206,13 +1206,33 @@
         shuffle(x)
         """
         cdef long i, j
+        cdef int copy=0
 
-        # adaptation of random.shuffle()
         i = len(x) - 1
-        while i > 0:
-            j = rk_interval(i, self.internal_state)
-            x[i], x[j] = x[j], x[i]
-            i = i - 1
+        try:
+ 	    j = len(x[0])
+        except:
+            j = 0
+
+        if (j == 0):
+	    # adaptation of random.shuffle()
+            while i > 0:
+                j = rk_interval(i, self.internal_state)
+                x[i], x[j] = x[j], x[i]
+                i = i - 1
+        else:
+            # make copies
+	    copy = hasattr(x[0].copy)
+            if copy:                 
+                while(i > 0):
+                    j = rk_interval(i, self.internal_state)
+                    x[i], x[j] = x[j].copy(), x[i].copy()
+                    i = i - 1
+            else:
+                while(i > 0):
+                    j = rk_interval(i, self.internal_state)
+                    x[i], x[j] = x[j][:], x[i][:]
+                    i = i - 1
                 
     def permutation(self, object x):
         """Given an integer, return a shuffled sequence of integers >= 0 and 



More information about the Numpy-svn mailing list