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

Robert Kern rkern at ucsd.edu
Mon Mar 28 23:37:16 EST 2005

Stephen Walton wrote:
> 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

x[:] = any_old_scalar

you mean?

Perhaps not, *if* I need the default type, which I rarely do. And when I 
do need the default type, and I'm coding carefully, I will add the type 
anyways to be explicit.

I *do* think that

   x = CFloat.new(shape, 2j*pi)

is better than

   x = empty(shape, type=CFloat)
   x[:] = 2j*pi

I don't think there's much OO in it. The implementations won't change, 
really. It's more a matter of aesthetics of the API. I like it for much 
the same reasons that transpose(array) et al. were folded into methods 
of arrays.

Also, with Perry's and Francesc's suggestions, it collapses three very 
similar functions into one.

> Having said that, the main reason I use zeros() in MATLAB is to 
> preallocate space. 

I use it the same in Python. Sometimes, I'm going to be replacing all of 
the values (in which case I would use empty()), but often I only need to 
sparsely replace values. Usually, the "background" value ought to be 0, 
but occasionally, things get weirder.

But, this isn't a particularly important issue.

Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter

More information about the NumPy-Discussion mailing list