[Numpy-discussion] Method to shift elements in an array?

Zachary Pincus zpincus at stanford.edu
Wed Feb 22 12:26:04 EST 2006


Here is my eventual solution. I'm not sure it's speed-optimal for  
even a python implementation, but it is terse.

I agree that it might be nice to have this fast, and/or in C (I'm  
using it for finite difference and related things).

def cshift(l, offset):
   offset %= len(l)
   return numpy.concatenate((l[-offset:], l[:-offset]))

Zach



On Feb 22, 2006, at 11:40 AM, Mads Ipsen wrote:

> On Wed, 22 Feb 2006, Alan G Isaac wrote:
>
>> On Wed, 22 Feb 2006, Zachary Pincus apparently wrote:
>>> Does numpy have an built-in mechanism to shift elements along some
>>> axis in an array? (e.g. to "roll" [0,1,2,3] by some offset, here 2,
>>> to make [2,3,0,1])
>>
>> This sounds like the rotater command in GAUSS.
>> As far as I know there is no equivalent in numpy.
>> Please post your ultimate solution.
>>
>> Cheers,
>> Alan Isaac
>>
>
> Similar to cshift() (cyclic shift) in F90. Very nice for calculating
> finite differences, such as
>
>   x' = ( cshift(x,+1) - cshift(x-1) ) / dx
>
> This would be a very handy feature indeed.
>
> // Mads
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through  
> log files
> for problems?  Stop!  Download the new AJAX search engine that makes
> searching your log files as easy as surfing the  web.  DOWNLOAD  
> SPLUNK!
> http://sel.as-us.falkag.net/sel? 
> cmd=lnk&kid=103432&bid=230486&dat=121642
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion





More information about the NumPy-Discussion mailing list