diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-10-13 20:50:00 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-10-13 20:50:00 +0000 |
commit | 099f140d97c07671497e5883ce2be9fcac62a3a1 (patch) | |
tree | 4ad18c2e84e564cbc153e8c00467491ff70aa77b /numpy/core/numeric.py | |
parent | ec5a3911b06f7dbefe89947a42d8ff23e1b82fdd (diff) | |
download | numpy-099f140d97c07671497e5883ce2be9fcac62a3a1.tar.gz |
Add roll function from ticket #293
Diffstat (limited to 'numpy/core/numeric.py')
-rw-r--r-- | numpy/core/numeric.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 541a54739..2bb8013b9 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -7,7 +7,7 @@ __all__ = ['newaxis', 'ndarray', 'flatiter', 'ufunc', '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 @@ def tensordot(a, b, axes=2): 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. |