[Numpy-discussion] Overloading numpy's ufuncs for better typecoercion?
Wed Jul 22 08:14:32 CDT 2009
> Ideally, I'd like numpy to be "fixed"
what do you mean by "fixed"?
Are you referring to Out or Out?
In : a = numpy.array(, numpy.uint8)
In : a + a
Out: array(, 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()
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(, dtype=...)
it means he/she knows what they are doing.
If instead, you refer to
In : numpy.add(a, a, numpy.empty((1, ), dtype = numpy.uint32))
Out: array(, 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(, numpy.int16)
>>> c = numpy.array(, numpy.int16)
>>> numpy.subtract(b.astype(numpy.int8), c.astype(numpy.int8), numpy.empty((1, ), dtype = numpy.int8))
More information about the NumPy-Discussion