[Numpy-discussion] Inplace shift
Keith Goodman
kwgoodman@gmail....
Sat Jun 7 09:28:39 CDT 2008
On Fri, Jun 6, 2008 at 10:46 PM, Anne Archibald
<peridot.faceted@gmail.com> wrote:
> 2008/6/6 Keith Goodman <kwgoodman@gmail.com>:
>> I'd like to shift the columns of a 2d array one column to the right.
>> Is there a way to do that without making a copy?
>>
>> This doesn't work:
>>
>>>> import numpy as np
>>>> x = np.random.rand(2,3)
>>>> x[:,1:] = x[:,:-1]
>>>> x
>>
>> array([[ 0.44789223, 0.44789223, 0.44789223],
>> [ 0.80600897, 0.80600897, 0.80600897]])
>
> As a workaround you can use backwards slices:
>worki
> In [40]: x = np.random.rand(2,3)
>
> In [41]: x[:,:0:-1] = x[:,-2::-1]
>
> In [42]: x
> Out[42]:
> array([[ 0.20183084, 0.20183084, 0.08156887],
> [ 0.30611585, 0.30611585, 0.79001577]])
Neat. It makes sense to go backwards. Thank you.
> Less painful for numpy developers but more painful for users is to
> warn them about the status quo: operations on overlapping slices can
> happen in arbitrary order.
Now I'm confused. Could some corner case of memory layout cause numpy
to work from right to left, breaking the workaround? Or can I depend
on the workaround working with numpy 1.0.4?
More information about the Numpy-discussion
mailing list