From: Charles R H. <cha...@gm...> - 2006-08-31 17:26:19
|
On 8/31/06, Christopher Barker <Chr...@no...> wrote: > > Fernando Perez wrote: > > In [8]: N.array(3).shape > > Out[8]: () > > > In [11]: N.array([]).shape > > Out[11]: (0,) > > > I guess my only remaining question is: what is the difference between > > outputs #8 and #11 above? Is an empty shape tuple == array scalar, > > while a (0,) shape indicates a one-dimensional array with no elements? > > If this interpretation is correct, what is the usage of the latter > > kind of object, given how it can't even be indexed? > > It can be iterated over (with zero iterations): > > >>> a = N.array([]) > >>> for i in a: > ... print i > ... > > whereas the scalar can not: > > >>> b = N.array(3) > >>> b > array(3) > >>> for i in b: > ... print i > ... > Traceback (most recent call last): > File "<stdin>", line 1, in ? > TypeError: iteration over a scalar (0-dim array) > > Of course the scalar isn't empty, so ti's different in that way too. Can > there be an empty scalar? It doesn't look like it. In fact, this looks > like it may be a bug: > >>> a = N.array([1,2,3]).sum(); a.shape; a.size; a > () > 1 > 6 > > That's what I'd expect, but what if you start with a (0,) array: > >>> a = N.array([]).sum(); a.shape; a.size; a > () > 1 > 0 > > where did that zero come from? I think that is correct, sums over empty sets are conventionally set to zero because they are conceived of as adding all the values in the set to zero. Typically this would be implemented as sum = 0 for i in set : sum += i; Chuck |