[Numpy-discussion] Overloading numpy's ufuncs for better typecoercion?

Citi, Luca lciti@essex.ac...
Wed Jul 22 08:14:32 CDT 2009


Hi Hans!

> Ideally, I'd like numpy to be "fixed"
what do you mean by "fixed"?
Are you referring to Out[2] or Out[3]?

In [1]: a = numpy.array([200], numpy.uint8)
In [2]: a + a
Out[2]: array([144], dtype=uint8)

Please do not "fix" this, that IS the correct output.
What should numpy do? Promote every sum of arrays of uint8 to uint16?
Or perform the operation as uint16 and cast it back to uint8 only if all elements are less than 256,
therefore having the same operation on arrays of the same type return an unpredictable data type?

I think the answer is simple:
a = numpy.array([200])
if you want an integer
a = numpy.array([200.])
if you want a float. These are pretty safe for reasonable inputs.
Whenever the user writes:
a = numpy.array([200], dtype=...)
it means he/she knows what they are doing.

If instead, you refer to 
In [3]: numpy.add(a, a, numpy.empty((1, ), dtype = numpy.uint32))
Out[3]: array([144], dtype=uint32)
in this case I agree with you, the expected result should be 400.
The inputs could be casted to the output type before performing the operation.
I do not think performing the operations with the output dtype
would break something. Even in borderline cases like the following:
>>> b = numpy.array([400], numpy.int16)
>>> c = numpy.array([200], numpy.int16)
>>> numpy.subtract(b.astype(numpy.int8), c.astype(numpy.int8), numpy.empty((1, ), dtype = numpy.int8))
array([100], dtype=int8)

Best,
Luca


More information about the NumPy-Discussion mailing list