[MATRIX-SIG] two possible bugs?

Konrad Hinsen hinsen@ibs.ibs.fr
Tue, 17 Mar 1998 19:03:47 +0100


> And then something I don't understand:
> 
> >>> a=array(10)
> >>> a.shape
> ()
> >>> a[0]
> 10
> >>> b=array([10])
> >>> b.shape
> (1,)
> >>> b[0]
> 10
> >>> a[-1]
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> IndexError: index out of bounds
> >>> b[-1]
> 10
> 
> Shouldn't a and b not be the same?

No. Your first example (array(10)) is a bit special. It represents an
array of rank zero, which is equivalent to a scalar. In fact, any
computation would return a scalar and never an array of rank zero; the
possibility to create a rank 0 object exists only because some code
might insist on receiving an array object under all circumstances.
There was some discussion about this a long time ago on the Matrix SIG.

Note also that a[0] should *in principle* raise an exception, because
the number of indices exceeds the rank. However, that would leave no
way at all to extract a "real" scalar from a rank 0 array object,
therefore the rules are "bent" a bit.

Moral: don't create rank 0 arrays unless you know very well what you
are doing.
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                          | E-Mail: hinsen@ibs.ibs.fr
Laboratoire de Dynamique Moleculaire   | Tel.: +33-4.76.88.99.28
Institut de Biologie Structurale       | Fax:  +33-4.76.88.54.94
41, av. des Martyrs                    | Deutsch/Esperanto/English/
38027 Grenoble Cedex 1, France         | Nederlands/Francais
-------------------------------------------------------------------------------