[Matrix-SIG] min/max question and strange result
Warren Focke
Warren Focke <warren@xtepca.gsfc.nasa.gov>
Wed, 13 May 1998 14:31:09 -0400 (EDT)
On Wed, 13 May 1998, Konrad Hinsen wrote:
>[bsd@scripps.edu]
> > 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.
NumPy does define it's own sense of order:
>int array_compare(PyArrayObject *self, PyObject *other) {
> /* I'd really like to raise an exception here, but I don't think
>that's possible. */
> /* This sort of comparision doesn't make sense on arrays. */
>
> return -1;
>}
but it's at least as surprising as comparison based on pointer value:
>>> a=array([1])
>>> b=array([1])
>>> a<b
1
>>> b<a
1
Lists define a comparison operator also, it even makes sense.
Warren Focke