[PYTHON MATRIX-SIG] max/min bug?

Tue, 28 Jan 1997 10:01:50 +0100

```> Is the following a bug or am I just confused??

You are just confused, but you are not alone. This problem will probably
bite everyone at least once.

> >>> from Numeric import *
> >>> a = reshape(arange(9),(3,3))
> >>> a
> 0 1 2
> 3 4 5
> 6 7 8
> >>> min(a)
> 6 7 8
> >>> max(a)
> 0 1 2
> >>>

Short answer: use minimum.reduce(a) and maximum.reduce(a) to get what you
expect.

Long answer: min() and max() are built-in Python functions that predate
NumPy. If they are called with a single argument which is a sequence
(e.g. an array), they return the smallest/largest element of that
sequence. Sounds fine, but isn't: what is the smallest of three arrays?

Python insists that all objects have a well-defined order relation,
even if there is no "natural" one. For types/classes that don't define
an order relation, the default order relation is comparison of addresses.
So here's what min(a) in your example does: it extracts a[0], a[1], and
a[2], each of which is a 1d array, and returns the one with the smallest
address. In other words, a perfectly pointless operation, since the
result is machine-dependent.

With the current Python interpreter there is no way to change the
behaviour of min() and max(). Hopefully this will change in a later
version.
--
-------------------------------------------------------------------------------
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
-------------------------------------------------------------------------------

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org