x.min() depends on ordering

Robert Kern robert.kern at gmail.com
Sat Nov 11 17:14:30 CST 2006

Keith Goodman wrote:
> x.min() and x.max() depend on the ordering of the elements:
>>> x = M.matrix([[ M.nan, 2.0, 1.0]])
>>> x.min()
> nan
>>> x = M.matrix([[ 1.0, 2.0, M.nan]])
>>> x.min()
> 1.0
> If I were to try the latter in ipython, I'd assume, great, min()
> ignores NaNs. But then the former would be a bug in my program.
> Is this related to how sort works?

Not really. sort() is a more complicated algorithm that does a number of
different comparisons in an order that is difficult to determine beforehand.
x.min() should just be a straight pass through all of the elements. However, the
core problem is the same: a < nan, a > nan, a == nan are all False for any a.

Barring a clever solution (at least cleverer than I feel like being
immediately), the way to solve this would be to check for nans in the array and
deal with them separately (or simply ignore them in the case of x.min()).
However, this checking would slow down the common case that has no nans (sans
nans, if you will).

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo

More information about the Numpy-discussion mailing list