[Numpy-discussion] rank-0 arrays

eric jones eric at enthought.com
Fri Sep 13 13:45:01 EDT 2002


Hey Tim,

Here is a short summary:

Reductions and indexing return different types based on the number of
dimensions of the input array:

>>> b = sum(a)
>>> l = len(b) # or whatever

This code works happily if "a" is 2 or more dimensions, but will fail if
it is 1d because the sum(a) will return a scalar in this case.  To write
generic code, you have to put an if/else statement in to check whether b
is a scalar or an array:

>>> b = sum(a)
>>> if type(b) is ArrayType:
...     l = len(b)
... else:
...     l = 1 # or whatever you need

or less verbose but still unpleasant:  

>>> b = asarray(sum(a))
>>> l = len(b)

eric

> -----Original Message-----
> From: numpy-discussion-admin at lists.sourceforge.net [mailto:numpy-
> discussion-admin at lists.sourceforge.net] On Behalf Of Tim Hochberg
> Sent: Friday, September 13, 2002 3:18 PM
> To: numpy-discussion at lists.sourceforge.net
> Subject: Re: [Numpy-discussion] rank-0 arrays
> 
> 
> 
> 
> [Perry wrote about Travis's proposal]
> 
> > refresh my memory about how this proposal would work. I've heard
> > proposals to add new types to Python itself, but that seems out of
> > the question. Are you talking about adding new scalar types as
> > module? E.g.
> [snip]
> > In any case, doesn't this still have the problem that Eric
complained
> > about, having to test for whether a result is an array or a scalar
> > (which was one of the drivers to produce rank-0 results).
> 
> Hi Perry,
> 
> I like Travis's proposal the best of those I've seen so far, but I
don't
> recall the details of Eric's problem. Could you refresh us as to the
> basics
> of it?.
> 
> -tim
> 
> 
> 
> 
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion





More information about the NumPy-Discussion mailing list