From: Charles R H. <cha...@gm...> - 2006-08-31 15:33:39
|
On 8/31/06, Fernando Perez <fpe...@gm...> wrote: > > On 8/31/06, Travis Oliphant <oli...@ie...> wrote: > > > What about > > > > N.array(3).size > > > > N.array([3]).size > > > > N.array([3,3]).size > > > > Essentially, the [] is being treated as an object when you explicitly > > ask for an object array in exactly the same way as 3 is being treated as > > a number in the default case. It's just that '[' ']' is "also" being > > used as the dimension delimiter and thus the confusion. > > > > It is consistent. It's a corner case, and I have no problem fixing the > > special-case code running when dtype=object so that array([], > > dtype=object) returns an empty array, if that is the consensus. > > I wasn't really complaining: these are corner cases I've never seen in > real use, so I'm not really sure how critical it is to worry about > them. Though I could see code which does automatic size/shape checks > tripping on some of them. The shape tuples shed a bit of light on > what's going on for the surprised (like myself): > > In [8]: N.array(3).shape > Out[8]: () > > In [9]: N.array([3]).shape > Out[9]: (1,) > > In [10]: N.array([3,3]).shape > Out[10]: (2,) > > In [11]: N.array([]).shape > Out[11]: (0,) > > In [12]: N.array([[]]).shape > Out[12]: (1, 0) > > In [13]: N.array([[],[]]).shape > Out[13]: (2, 0) > > > I won't really vote for any changes one way or another, as far as I'm > concerned it's one of those 'learn the library' things. I do realize > that the near-ambiguity between '[]' as an empty object and '[]' as > the syntactic delimiter for a container makes this case a bit of a > gotcha. > > 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? > > In [15]: N.array([])[0] > > --------------------------------------------------------------------------- > exceptions.IndexError Traceback (most > recent call last) > > /home/fperez/research/code/mjmdim/pycode/<ipython console> > > IndexError: index out of bounds > > > And is this really expected? > > In [18]: N.array([]).any() > Out[18]: False This could be interpreted as : exists x, x element of array, s.t. x is true. In [19]: N.array([]).all() > Out[19]: True Seems right: for all x, x element of array, x is true. It's a bit funny to have an array for which 'no elements are true' > (any==false), yet 'all are true' (all==true), isn't it? Fun with empty sets! The question is, is a zero dimensional array an empty container or does it contain its value. The numpy choice of treating zero dimensional arrays as both empty containers and scalar values makes the determination a bit ambiguous although it is consistent with the indexing convention. Chuck |