[Numpy-discussion] Is this expected behavior?

Russel Howe russel@appliedminds....
Mon Jan 18 13:41:47 CST 2010


This looks like the difference between memmove and memcpy to me, but I 
am not sure what the expected behavior of numpy should be.  The first 
shift behaves the way I expect, the second is surprising.

I know about numpy.roll.  I was hoping for something faster, which this 
would be if it worked.

In [1]: a = (np.random.random((10,10))*10).astype('u1')

In [2]: a
Out[2]:
array([[8, 0, 5, 4, 8, 2, 7, 8, 7, 6],
        [6, 6, 3, 3, 9, 8, 0, 8, 9, 5],
        [5, 0, 1, 1, 2, 5, 8, 2, 5, 3],
        [9, 0, 0, 2, 8, 2, 0, 7, 7, 0],
        [9, 8, 6, 9, 6, 3, 9, 4, 4, 5],
        [2, 7, 6, 9, 3, 8, 9, 9, 6, 9],
        [2, 8, 8, 4, 0, 3, 7, 6, 7, 6],
        [2, 4, 9, 2, 4, 7, 3, 6, 7, 4],
        [3, 2, 0, 7, 0, 7, 6, 6, 1, 6],
        [2, 3, 8, 8, 9, 6, 7, 2, 5, 0]], dtype=uint8)

In [3]: a[:, :-1] = a[:, 1:]

In [4]: a
Out[4]:
array([[0, 5, 4, 8, 2, 7, 8, 7, 6, 6],
        [6, 3, 3, 9, 8, 0, 8, 9, 5, 5],
        [0, 1, 1, 2, 5, 8, 2, 5, 3, 3],
        [0, 0, 2, 8, 2, 0, 7, 7, 0, 0],
        [8, 6, 9, 6, 3, 9, 4, 4, 5, 5],
        [7, 6, 9, 3, 8, 9, 9, 6, 9, 9],
        [8, 8, 4, 0, 3, 7, 6, 7, 6, 6],
        [4, 9, 2, 4, 7, 3, 6, 7, 4, 4],
        [2, 0, 7, 0, 7, 6, 6, 1, 6, 6],
        [3, 8, 8, 9, 6, 7, 2, 5, 0, 0]], dtype=uint8)

In [5]: a[:, 1:] = a[:, :-1]

In [6]: a
Out[6]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
        [7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
        [8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
        [4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
        [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
        [3, 3, 3, 3, 3, 3, 3, 3, 3, 3]], dtype=uint8)

In [7]: np.__version__
Out[7]: '1.3.0'


More information about the NumPy-Discussion mailing list