[Numpy-svn] r3334 - in trunk/numpy: core lib

numpy-svn at scipy.org numpy-svn at scipy.org
Fri Oct 13 15:50:02 CDT 2006


Author: oliphant
Date: 2006-10-13 15:50:00 -0500 (Fri, 13 Oct 2006)
New Revision: 3334

Modified:
   trunk/numpy/core/numeric.py
   trunk/numpy/lib/shape_base.py
Log:
Add roll function from ticket #293

Modified: trunk/numpy/core/numeric.py
===================================================================
--- trunk/numpy/core/numeric.py	2006-10-13 20:41:27 UTC (rev 3333)
+++ trunk/numpy/core/numeric.py	2006-10-13 20:50:00 UTC (rev 3334)
@@ -7,7 +7,7 @@
            'asarray', 'asanyarray', 'ascontiguousarray', 'asfortranarray',
            'isfortran', 'empty_like', 'zeros_like',
            'correlate', 'convolve', 'inner', 'dot', 'outer', 'vdot',
-           'alterdot', 'restoredot', 'rollaxis', 'cross', 'tensordot',
+           'alterdot', 'restoredot', 'roll', 'rollaxis', 'cross', 'tensordot',
            'array2string', 'get_printoptions', 'set_printoptions',
            'array_repr', 'array_str', 'set_string_function',
            'little_endian', 'require',
@@ -332,6 +332,25 @@
     res = dot(at, bt)
     return res.reshape(olda + oldb)
 
+def roll(a, shift, axis=None):
+    """Roll the elements in the array by 'shift' positions along
+    the given axis.
+    """
+    a = asanyarray(a)
+    if axis is None:
+        n = a.size
+        reshape=1
+    else:
+        n = a.shape[axis]
+        reshape=0
+    shift %= n
+    indexes = concatenate((arange(n-shift,n),arange(n-shift)))
+    res = a.take(indexes, axis)
+    if reshape:
+        return res.reshape(a.shape)
+    else:
+        return res
+
 def rollaxis(a, axis, start=0):
     """Return transposed array so that axis is rolled before start.
 

Modified: trunk/numpy/lib/shape_base.py
===================================================================
--- trunk/numpy/lib/shape_base.py	2006-10-13 20:41:27 UTC (rev 3333)
+++ trunk/numpy/lib/shape_base.py	2006-10-13 20:50:00 UTC (rev 3334)
@@ -1,6 +1,6 @@
 __all__ = ['atleast_1d','atleast_2d','atleast_3d','vstack','hstack',
            'column_stack','row_stack', 'dstack','array_split','split','hsplit',
-           'vsplit','dsplit','apply_over_axes','expand_dims',
+           'vsplit','dsplit','apply_over_axes','expand_dims', 
            'apply_along_axis', 'kron', 'tile', 'get_array_wrap']
 
 import numpy.core.numeric as _nx



More information about the Numpy-svn mailing list