[PYTHON MATRIX-SIG] Re: sum() in 1.0a2 + indexing BUG

Carlos Fonseca Carlos Fonseca <fonseca@gaivota.demon.co.uk>
Sun, 18 Aug 1996 15:40:54 +0100 (BST)

First, the bug:

>>> from Numeric import *
>>> array(0)[0:]

On Sun, 18 Aug 1996, Konrad Hinsen wrote:
>    I think sum(a) (and other reductions) should return an array with shape ()
>    when a is 1d, and not a scalar.  Currently, sum(a).anyarraymethod and
>    sum(a)[0] will fail if a is 1d. 
> Which is precisely why I have always insisted that array operations
> should be implemented as functions and not as methods.
> Of course the alternative would be to make rank-0 arrays distinct from
> scalars, and this alternative has been discussed a lot in the early
> days of the Matrix SIG. But it creates a few fundamental
> problems. Everyones idea of arrays is that they are collections of
> scalars, and one expects to be able to pick the scalars from an array
> by indexing. But if rank-0 arrays are distinct from scalars,
> indexing a rank-1 array would return a rank-0 array. Worse, there
> would be no obvious way to extract a real scalar from a rank-0
> array, so another conversion function would be necessary. The
> resulting system would be extremely confusing to users.

My message referred to reductions, not to indexing. Indexing could still
return scalars, where appropriate. 

I understand that indexing with scalars (but not with slices!) could be
seen as a reduction, and thus made to return a null-shape array, leading
to the situation you describe. Perhaps indexing into a null-shape array
could return the scalar? This works in the present implementation. But see

> Not having specific rank-0 objects is therefore the lesser evil,
> but then we have to live with the implementation limitations
> given by the Python interpreter.
> Konrad.

But we *do* have them:

>>> from Numeric import *
>>> a=array(5)
>>> a.shape
>>> a[0]

It is fair to expect the array constructor to return an array. Perhaps
it should return one with shape (1,)? But then, what about array([5])?


MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org