# [Numpy-discussion] A case for rank-0 arrays

Francesc Altet faltet at carabos.com
Fri Feb 24 01:01:02 CST 2006

```A Dissabte 18 Febrer 2006 20:49, Sasha va escriure:
> PROPOSALS
> ==========
>
> Here are three suggestions:
>
> 1. Probably the most controversial question is what getitem should
> return. I believe that most of the confusion comes from the fact that
> the same syntax implements two different operations: indexing and
> projection (for the lack of better name).  Using the analogy between
> ndarrays and functions, indexing is just the application of the
> function to its arguments and projection is the function projection
> ((f, x) -> lambda (*args): f(x, *args)).
>
> The problem is that the same syntax results in different operations
> depending on the rank of the array.
>
> Let
>
> >>> x = ones((2,2))
> >>> y = ones(2)
>
> then x[1] is projection and type(x[1]) is ndarray, but y[1] is
> indexing and type(y[1]) is int32.  Similarly, y[1,...] is indexing,
> while x[1,...] is projection.
>
> I propose to change numpy rules so that if ellipsis is present inside
> [], the operation is always projection and both y[1,...] and
> x[1,1,...] return zero-rank arrays.  Note that I have previously
> rejected Francesc's idea that x[...] and x[()] should have different
> meaning for zero-rank arrays.  I was wrong.

+1 (if I want to be consequent ;-)

I guess that this would imply that:

In [19]: z=numpy.array(1)

In [20]: type(z[()])
Out[20]: <type 'int32scalar'>

In [21]: type(z[...])
Out[21]: <type 'numpy.ndarray'>

isn't it?

> 2. Another source of ambiguity is the various "reduce" operations such
> as sum or max.  Using the previous example, type(x.sum(axis=0)) is
> ndarray, but type(y.sum(axis=0)) is int32.  I propose two changes:
>
>    a. Make x.sum(axis)  return ndarray unless axis is None, making
> type(y.sum(axis=0)) is ndarray true in the example.
>
>    b. Allow axis to be a sequence of ints and make
> x.sum(axis=range(rank(x))) return rank-0 array according to the rule
> 2.a above.
>
>    c. Make x.sum() raise an error for rank-0 arrays and scalars, but
> allow x.sum(axis=()) to return x.  This will make numpy sum consistent
> with the built-in sum that does not work on scalars.

Well, to say the truth, I've not a strong opinion on this one (this is
why I "mostly" supported your proposal ;-), but I think that if Travis
has reasons to oppose to it, we should listen to him.

> 3. This is a really small change currently
> I propose to make shape=() valid in ndarray constructor.

+1

Cheers,

--
>0,0<   Francesc Altet     http://www.carabos.com/
V   V   Cárabos Coop. V.   Enjoy Data
"-"

```