# [Numpy-discussion] numarray rank-0 array issues

Perry Greenfield perry at stsci.edu
Mon Dec 9 07:41:03 CST 2002

```A little while ago we tackled the issue of whether indexing numarray
arrays should return Python scalars or rank-0 arrays. For reasons
gone into at great length previously on this list we decided to
always return Python scalars when no precision is lost by conversion to
Python scalars (Float128 may be an exception to this rule, but we'll
deal with that later). We consider this issue closed. But there
are some loose ends.

There was an opinion that reduction operations (e.g., add.reduce)
should always return arrays. However, some desired that the rank-0
arrays returned when reducing a rank-1 array should be indexable
and have a length. In particular:

>>> x = arange(10)
>>> v = add.reduce(x)
>>> print v
55
>>> v
array(55)
>>> len(v)
1
>>> v[0]
55

The great majority objected to len(rank-0) being allowed
as well as rank-0[0] working. We agree.

We propose that at least one version of the reduction operations
always return arrays, but that the array "end-point" always
be a rank-1 len-1 array instead of a rank-0 array. This is because
a rank-1 len-1 array

1) has a len() = 1
2) can be indexed.
3) because of broadcasting, behaves like a scalar in expressions
with arrays (i.e., as was intended for rank-0 arrays)

Thus, if one repeatedly reduces an N-dimensional array, one
eventually gets a rank-1 len-1 array, and reducing a rank-1
len-1 array  simply generates a new rank-1 len-1 array with the
same value.

>>> x = arange(10)
>>> v = add.areduce(x)
>>> v
array([55])
>>> add.areduce(v)
array([55])

Is there any reason why this behavior would not serve the purposes
of those that wanted rank-0 arrays returned from reduction operations?

Likewise, we could provide a function to wrap scalars as rank-1 len-1
arrays (much like array(5) produces a rank-0 array) for the purposes
of more generic functions and routines so that checks for scalars
do not need to be made. This function would convert scalars to arrays,
but simply return the arrays themselves if passed as arguments.

One possibility is that we eliminate rank-0 arrays and use rank-1 len-1
arrays in their place (e.g., array(5) returns a rank-1 len-1 array).
The problem is that there may be some who already dependon rank-0
properties and this probably will break existing code. Any opinions?

Finally, the default reduction operation (i.e., add.reduce) could
be given the behavior described above. We are inclined to leave it
as is, i.e., to return scalars when reducing 1-d arrays and provide
a different operator method (areduce) to always return arrays. Any
disagreements?

Perry Greenfield

```

More information about the Numpy-discussion mailing list