[Numpy-discussion] Numeric/numarray compatibility issue

konrad.hinsen at laposte.net konrad.hinsen at laposte.net
Thu Mar 3 09:33:17 EST 2005

Following a bug report concerning ScientificPython with numarray, I  
noticed an incompatibility between Numeric and numarray, and I am  
wondering if this is intentional.

In Numeric, the result of a comparison operation is an integer array.  
In numarray, it is a Bool array. Bool arrays seem to behave like Int8  
arrays when arithmetic operations are applied. The net result is that

	print n.add.reduce(n.greater(n.arange(128), -1))

yields -128, which is not what I would expect.

I can see two logically coherent points of views:

1) The Numeric view: comparisons yield integer arrays, which may be  
used freely in arithmetic.

2) The "logician's" view: comparisons yield arrays of boolean values,  
on which no arithmetic is allowed at all, only logical operations.

The first approach is a lot more pragmatic, because there are a lot of  
useful idioms that use the result of comparisons in arithmetic, whereas  
an array of boolean values cannot be used for much else than logical  

And now for my pragmatic question: can anyone come up with a solution  
that will work under both Numeric an numarray, won't introduce a speed  
penalty under Numeric, and won't leave the impression that the  
programmer had had too many beers?  There is the quick hack

	print n.add.reduce(1*n.greater(n.arange(128), -1))

but it doesn't satisfy the last two criteria.

Konrad Hinsen
Laboratoire Leon Brillouin, CEA Saclay,
91191 Gif-sur-Yvette Cedex, France
Tel.: +33-1 69 08 79 25
Fax: +33-1 69 08 82 61
E-Mail: khinsen at cea.fr

More information about the NumPy-Discussion mailing list