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

Citi, Luca lciti at essex.ac.uk
Wed Jul 22 10:43:43 EDT 2009


Hello Hans,

> Although it should be noted that in C/C++, 
> the result of uint8+uint8 is int.
But C/C++ works with scalars and often temporary results
are kept in registers.
On the contrary, numpy works with arrays. We cannot expect
(a+b)*c
to grow from uint8 to uint16 and then uint32 :-D

> For example, the conversion would typically have to be
> performed only once (after loading), no?  Then, why not simplify things
> further by adding a dtype= parameter to importImage()?  This could even
> default to float32 then.

I think this is the cleanest, easiest, better way to go.

Also, Matlab's imread reads the image in its format (e.g. uint8).
An explicit conversion is needed (e.g. double()) otherwise
all the operations are performed in uint8. Even
i = imread('...');
i = i + 34.5
does not trigger an upcast.
The only difference is that matlab saturates to 0 and 255
instead of wrapping.

Best,
Luca



More information about the NumPy-Discussion mailing list