[Numpy-discussion] zeros() default type in Numeric3

Stephen Walton stephen.walton at csun.edu
Mon Mar 28 15:40:17 CST 2005


Robert Kern wrote:

>   Float.new(shape, value=None) # empty
>   Float.new(shape, value=0)    # zeros
>   Float.new(shape, value=1)    # ones

Uhm, my first reaction to this kind of thing is "ugh," but maybe I'm 
just not thinking in the correct OO mode.  Is this any better than 
zeros() and ones()?  For that matter, is it any better than

x=zeros(shape)
x=any_old_scalar

Having said that, the main reason I use zeros() in MATLAB is to 
preallocate space.  MATLAB can dynamically grow arrays, so the following 
is legal:

x=[];
do k=1:100
   x[:,k]=a_vector_of_100_values;

and produces a 100 by 100 array.  While legal, it is much faster to 
preallocate x by changing the first line to "x=zeros(100,100);".  Since 
NumPy arrays can't grow dynamically, perhaps this is a small issue.




More information about the Numpy-discussion mailing list