[Matrix-SIG] min/max question and strange result

Konrad Hinsen hinsen@ibs.ibs.fr
Wed, 13 May 1998 19:24:32 +0200


> >>> a.shape = (3,1)
> >>> a
> array([[-1],
>        [ 0],
>        [ 1]])
> >>> min(a)
> array([1])
> >>> max(a)
> array([-1])
> 
> This seems odd.
> Why is [-1] greater than [1] ??

That's a feature of Python's current comparison approach, which is
under discussion in comp.lang.python at the moment.

Currently Python defines an order between any objects, whether that
makes sense or not. Object types that don't define their own sense of
order (as the numbers do) are sorted according to their memory
address. Lists are such data types, and arrays too. That's why you get
surprising answers.

If you want reaonsable behaviour for arrays, use NumPy functions, e.g.
Numeric.maximum.reduce(a) to find the largest element.
-- 
-------------------------------------------------------------------------------
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
-------------------------------------------------------------------------------