[Numpy-discussion] numpy ring buffer

Robert Kern robert.kern at gmail.com
Mon May 6 05:52:10 EDT 2013


On Mon, May 6, 2013 at 10:39 AM, Daniele Nicolodi <daniele at grinta.net> wrote:
> On 06/05/2013 11:01, Robert Kern wrote:
>> np.roll() copies all of the data every time. It does not return a
>> view.
>
> Are you sure about that?  Either I'm missing something, or it returns a
> view in my testing (with a fairly old numpy, though):
>
> In [209]: np.__version__
> Out[209]: '1.6.2'
>
> In [210]: v1 = np.arange(10)
>
> In [211]: v1.flags['OWNDATA']
> Out[211]: True
>
> In [212]: v2 = np.roll(v1, -1)
>
> In [213]: v2.flags['OWNDATA']
> Out[213]: False

It may return a view on something, but it isn't a view on the original
array. It can't be, because the rolled result cannot be represented as
uniformly-strided memory access on the original data. Check the
source.

https://github.com/numpy/numpy/blob/master/numpy/core/numeric.py#L1173

The reason you see a "view" is that final `reshape()` call. But it
first copies the original data using `take()`.

--
Robert Kern



More information about the NumPy-Discussion mailing list