[Numpy-discussion] Bug in resize method?

Charles R Harris charlesr.harris@gmail....
Wed Aug 29 13:29:22 CDT 2007


On 8/29/07, Timothy Hochberg <tim.hochberg@ieee.org> wrote:
>
>
>
> On 8/29/07, Charles R Harris <charlesr.harris@gmail.com> wrote:
> >
> >
> >
> > On 8/29/07, Christopher Barker < Chris.Barker@noaa.gov> wrote:
> > >
> > > Charles R Harris wrote:
> > > > What *should* the resize method do? It looks like
> > > > it is equivalent to assigning a shape tuple to a.shape,
> > >
> > > No, that's what reshape does.
> >
> >
> > No, reshape returns a view and the view doesn't own its data. Totally
> > different behavior in this context.
> >
> > > so why do we need it?
> > >
> > > resize() will change the SIZE of the array (number of elements), where
> > >
> > > reshape() will only change the shape, but not the number of elements.
> > > The fact that the size is changing is why it won't work if if doesn't
> > > own the data.
> >
> >
> > According to the documentation, the resize method changes the array
> > inplace. How can it be inplace if the number of elements changes?
> >
>
> It sounds like you and Chris are talking past each other on a matter of
> terminology. At a C-level, it's obviously not (necessarily) in place, since
> the array may get realloced as you surmise below. However, at the Python
> level, the change is in fact in place, in the same sense that appending to a
> Python list operates in-place, even though under the covers memory may get
> realloced there as well.
>
>
> > Admittedly, it *will* change the size, but that is not consistent with
> > the documentation. I suspect it reallocates memory and (hopefully) frees the
> > old, but then that is what the documentation should say because it explains
> > why the data must be owned -- a condition violated in some cases as
> > demonstrated above. I am working on documentation and that is why I am
> > raising these questions. There seem to be some inconsistencies that need
> > clarification and/or fixing.
> >
>
> The main inconsistency I see above is that resize appears to only require
> ownership of the data if in fact the number of items changes. I don't think
> that's actually a bug, but I don't like it much; I would prefer that resize
> be strict and always require ownership. However, I'm fairly certain that
> there are people that prefer "friendliness" over consistency, so I wouldn't
> be surprised to get some pushback on changing that.
>

I still don't see why the method is needed at all. Given the conditions on
the array, the only thing it buys you over the resize function or a reshape
is the automatic deletion of the old memory if new memory is allocated. And
the latter is easily done as a = reshape(a, new_shape). I know there was a
push to make most things methods, but it is possible to overdo it. Is this a
Numarray compatibility issue?

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070829/68933649/attachment-0001.html 


More information about the Numpy-discussion mailing list