[Matrix-SIG] rotater?

Tim Hochberg tim.hochberg@ieee.org
Wed, 16 Jun 1999 13:30:43 -0600


This is a multi-part message in MIME format.

------=_NextPart_000_0144_01BEB7FC.6F547480
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Yoon, Hoon (CICG - NY Program Trading) wrote:

>  Gauss has this thing called rotater, which given array rotates it by
>another value in array
>
>x = [[1,2,3], [2,3,4]]
>shft = [1,2]
>rotater(x, shft)
>will give you
>[[2,3,1], [4,2,3]]
>   Easy enough to do in a loop, but can I avoid this somehow?


It pretty easy to avoid the inner loop due to shifting all of the elements,
you still may have to loop over the other axes, but perhaps this will get
you started:

-tim


------=_NextPart_000_0144_01BEB7FC.6F547480
Content-Type: text/plain;
	name="rotater.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="rotater.py"

from Numeric import *

def rotater(a, n):
    """Rotates the array a by n"""
    a = asarray(a)
    # Extension to multiple dimensions left as an excercise to the reader.
    result = zeros(shape(a), a.typecode())
    # Make rotation positive
    n = n % len(a)
    if n == 0:
        return a
    result[:n] = a[-n:]
    result[n:] = a[:-n]
    return result
    

x = [1,2,3,4,5]

print rotater(x, 1)
print rotater(x, 2)
print rotater(x, -1)
print rotater(x, -2)

------=_NextPart_000_0144_01BEB7FC.6F547480--