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

Konrad HINSEN hinsenk@ere.umontreal.ca
Mon, 19 Aug 1996 12:25:15 -0400


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

But having indexing work differently from reduction only adds to
the confusion.

> to the situation you describe. Perhaps indexing into a null-shape array
> could return the scalar? This works in the present implementation. But see

Again this is hardly an obvious behaviour. The number of indices must
always be equal to the rank of an array, so a rank-0 array needs
exactly zero indices ;-)

> But we *do* have them:
> 
> >>> from Numeric import *
> >>> a=array(5)
> >>> a.shape
> ()
> >>> a[0]
> 5
> 
> 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])?

That is indeed a problem. You are right that the array constructor should
return an array, but on the other hand there are no rank-0 arrays.
Since we have decided not to have rank-0 array objects, I can think
of two solutions:
1) Declare scalars to be special cases of arrays, and let the
   constructor return a scalar.
2) Raise an exception.
I definitely favour the first, because I can imagine that array(x)
with scalar x can occur as a special case of an application where
x could be an arbitrary nested list. In that case returning a
scalar would most probably work. On the other hand, I can't think
of too many cases where one would type array(5) by mistake and not
notice the error without an exception.

Konrad.

-------------------------------------------------------------------------------
Konrad Hinsen                     | E-Mail: hinsenk@ere.umontreal.ca
Departement de chimie             | Tel.: +1-514-343-6111 ext. 3953
Universite de Montreal            | Fax:  +1-514-343-7586
C.P. 6128, succ. Centre-Ville     | Deutsch/Esperanto/English/Nederlands/
Montreal (QC) H3C 3J7             | Francais (phase experimentale)
-------------------------------------------------------------------------------

=================
MATRIX-SIG  - SIG on Matrix Math for Python

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