[SciPy-user] SciPy-user Digest, Vol 29, Issue 11
oliphant.travis at ieee.org
Fri Jan 6 14:32:23 CST 2006
Brendan Simons wrote:
> Great! That would be very helpful to me.
It's done in SVN. Also resize(a,0) works too.
>> But, note also that if another object is referencing a you can't do a
>> resize like this:
>> a = numpy.ones(5)
>> b = a
>> a.resize(...) will give an error because the object a has more than one
>> reference to it.
> Oh, I hadn't realized this. I can work around that limitation, but it
> might not be apparent to future users.
> I can't comment about the relative merits of malloc approaches, but I
> certainly don't want to sacrifice the performance of numpy for this
> use case.
Ultimately this is because you can't tell whether you simply have
b = a # We really could resize a because b is just another name for a...
b = a[1:5:2,0:3:2] # now b is a *view* of a's memory. If we resize a,
then b is really going to be messed up and give us problems.
We could distinguish these two using another array flag like (SHAREDATA)
> So the memory address belongs to the reference, and not the array
> itself? *Looks embarassed for not understanding better how things work*
The memory address belongs to whichever array actually owns the data
(which one allocated it). Check a.flags.owndata to see if the array
owns it or not. Obviously you can't resize data you don't own. But,
you also can't resize if another array. If the array does not own it's
own data, then .base will point to the object that it got it's memory
from (which might not own its own data --- you could recurse until you
find it though...)
>> If you really need the numarray ability to swap out the memory address
>> of the array for another one, then you might as well use the resize
>> a = resize(a,(0,)) # which we will have to fix to make work right ;-)
> The global resize function will change the memory address of all
> references to the array then? If so, that sounds like an adequate
> solution, thanks.
No, it has no way to do that, there is no information kept about which
objects have references to the array (and indeed would be quite involved
to keep track of that). All that happens is that you've just changed a
to point to another array which is of size 0.
> Brendan Simons, Project Engineer
> Stern Laboratories, Hamilton Ontario
>SciPy-user mailing list
>SciPy-user at scipy.net
More information about the SciPy-user