Automatic copies of NumPy arrays

Janko Hauser jhauser at ifm.uni-kiel.de
Fri Oct 20 15:24:17 EDT 2000


Jan Kybic <Jan.Kybic at epfl.ch> writes:

> Janko Hauser <jhauser at ifm.uni-kiel.de> writes:
> 
> > A good policy with NumPy functions should be that they do not alter
> > the input arguments, without mentioning this explicitly. This policy
> 
> Thanks for the speedy reaction. Yes, I agree that following your
> policy prevents the mystical errors but it can be sometimes
> inefficient, because you make a copy of an array just to destroy the
> original array immediately afterwards. I work with big images (3D) so
> sometimes it matters. 
> 
But if your algorythm needs to work on the original array, there is no
other way, than to copy it. On the other side there are only some ways to
modify an array inplace. So, while writing the function you will know,
when you change the array.

> What is this flag? My NumericalPython manual index does not talk about
> it. Where can I find more? 
This flag pops up in the construction of array as in 
>>> a=array([1,2,3], savespace=1)

This prevents that calculations with scalars get more easier, as they
are by default all double, and would cast the array to double.
If you set the savespace flag as in
>>> a=array([1,2,3], savespace=1)
>>> b=a*pi 
>>> b  
array([3, 6, 9],'s')

The typecode of the result is not changed.
In a function you can do this with the arguments as in
param1.savespace(1), but note that the result has also this flag set

>>> b.spacesaver()
1

And as the parameters are used by reference, you need to change this,
before you leave the function. So it can be a huge memory saving,
especially for image manipulation, but it is not trivial (think of
what happens, if the parameter has the flag set already in the calling
routine, now you would unset it in the function.

HTH,
__Janko
-- 
  Institut fuer Meereskunde             phone: 49-431-597 3989
  Dept. Theoretical Oceanography        fax  : 49-431-565876
  Duesternbrooker Weg 20                email: jhauser at ifm.uni-kiel.de
  24105 Kiel, Germany



More information about the Python-list mailing list