Version 4 (modified by sasha, 7 years ago)

--

Zero-Rank Arrays

Zero-rank arrays are array with shape=(). For example:

>>> x = array(1)
>>> x.shape
()

Indexing of Zero-Rank Arrays

As of NumPy release 0.9.3, zero-rank arrays do not support any indexing

>>> x[...]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: 0-d arrays can't be indexed.

On the other hand there are several cases that make sense for rank-zero arrays.

Ellipsis and empty tuple

Sasha started a discussion on scipy-dev with the folowing proposal:

... it may be reasonable to allow a[...].  This way
ellipsis can be interpereted as any number of  ":"s including zero. 
Another subscript operation that makes sense for scalars would be
a[...,newaxis] or even a[{newaxis, }* ..., {newaxis,}*], where 
{newaxis,}* stands for any number of comma-separated newaxis tokens. 
This will allow one to use ellipsis in generic code that would work on
any numpy type. 

The idea to allow increasing the rank of zero-rank arrays with [..., newaxis] was originally proposed by Sebastien.

Francesc Altet supported the idea of [...] on zero-rank arrays and suggested that [()] be supported as well.

Francesc's proposal was:

In [65]: type(numpy.array(0)[...])
Out[65]: <type 'numpy.ndarray'>

In [66]: type(numpy.array(0)[()])   # Indexing a la numarray
Out[66]: <type 'int32_arrtype'>

In [67]: type(numpy.array(0).item())  # already works
Out[67]: <type 'int'>

There is a consensus that for a zero-rank array x, both x[...] and x[()] should be valid, but the question remains on what should be the type of the result - zero rank ndarray or x.dtype?

(Sasha) First, whatever choice is made for x[...] and x[()] they should be the same because ... is just syntactic sugar for "as many : as necessary", which in the case of zero rank leads to ... = (:,)*0 = (). Second, rank zero arrays and numpy scalar types are interchangeable within numpy, but numpy scalars can be use in some python constructs where ndarrays can't. For example:

>>> (1,)[array(0)]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: tuple indices must be integers
>>> (1,)[int32(0)]
1

Since most if not all numpy function automatically convert zero-rank arrays to scalars on return, there is no reason for [...] and [()] operations to be different.

Increasing rank with newaxis

more to come