# [Numpy-discussion] rank-0 arrays

Tim Hochberg tim.hochberg at ieee.org
Fri Sep 13 14:25:14 CDT 2002

```Thanks for the summary Eric.

However, I don't find this example particularly compelling. Probably because
I don't think len(b) should work if b is rank zero (or possibly return
None). Yeah, I know it's worked in NumPy for years, but that doesn't mean I
have to like it. I would favor stripping out as much of the weird special
casing of rank-0 arrays as possible in the transition to numarray.

In the particular example given below, isn't the return value meaningless if
b is rank-0. Or rather only meaningful if  "or whatever you need" happens to
be one. This all smells rather arbitrary to me. However, this isn't a
problem that I run into and I'll concede that it's possible that returning
rank[0] arrays and then treating the rank zeros arrays almost like shape
(1,) arrays most of the time may solve more problems than it causes, but I'd
be interested in seeing more realistic examples. I guess I'll go poke around
MA and see what I can see. Any other suggestions of what to look at?

-tim

> 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
>
>
>
> -------------------------------------------------------
> 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
>

```