#293: Add a roll function (or shift or rotate)
-------------------------+--------------------------------------------------
Reporter: baxissimo | Owner: somebody
Type: enhancement | Status: new
Priority: normal | Milestone: 1.0 Release
Component: Other | Version:
Severity: normal | Keywords:
-------------------------+--------------------------------------------------
Roll/shift/rotate is a function commonly available in other array
languages. Numpy should have it too. Especially now that there's a
"rollaxis".
Here's an implementation that i've spent a little time tweaking and timing
various variations on.
{{{
def roll(y,shift,axis):
"""Roll the elements in the array by 'shift' positions along the given
axis."""
from numpy import asanyarray,concatenate,arange
y = asanyarray(y)
n = y.shape[axis]
shift %= n # does the right thing for negative shifts, too
return y.take(concatenate((arange(shift,n),arange(shift))), axis)
}}}
This would be a good candidate for shape_base.py, probably.
Having this as a function also opens the door for a possibly faster
version implemented directly in C at some point in the future.
--
Ticket URL: <http://projects.scipy.org/scipy/numpy/ticket/293>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.