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

Charles R Harris charlesr.harris@gmail....
Wed Jul 22 13:30:05 CDT 2009


2009/7/22 Hans Meine <meine@informatik.uni-hamburg.de>

> Hi!
>
> (This mail is a reply to a personal conversation with Ullrich Köthe, but is
> obviously of a greater concern.  This is about VIGRA's new NumPy-based
> python
> bindings.)  Ulli considers this behaviour of NumPy to be a bug:
>
> In [1]: a = numpy.array([200], numpy.uint8)
>
> In [2]: a + a
> Out[2]: array([144], dtype=uint8)
>

> However, this is well-known, often-discussed, and IMHO not really
> unexpected
> for computer programmers who ever worked with C-like languages (even Java
> has
> many such problems).  Christian even said this is what he wants.
>
> OTOH, I agree that it is a missing feature that NumPy performs "coercion
> before the operation" (to be more precise: the temporary data type should
> be
> promoted from the operand types, and /then/ the coercion - which can also
> reduce the number of bits - should happen), to fix this strange behaviour:
>
> In [3]: numpy.add(a, a, numpy.empty((1, ), dtype = numpy.uint32))
> Out[3]: array([144], dtype=uint32)
>
> Now, our opinions differ on how to deal with this - Ulli planned to
> overwrite
> (more or less) all ufuncs in vigranumpy in order to return float32 (which
> is
> sort of a common denominator and the type nearly all other vigranumpy
> functions should accept).  I see two main disadvantages here:
>
> a) Choosing float32 seems to be arbitrary, and I'd like as much as possible
> of
> vigranumpy to be independent from VIGRA and its particular needs.  I have
> seen
> so many requests (e.g. on the c++-sig mailing list) for *good* C++/boost-
> python <-> numpy bindings that it would be a pity IMO to add special cases
> for
> VIGRA by overloading __add__ etc.
>
> b) Also, I find it unexpected and undesirable to change the behaviour of
> such
> basic operations as addition on our ndarray-derived image types.  IMO this
> brings the danger of new users being confused about the different
> behaviours,
> and even experienced vigranumpy users might eventually fall in the trap
> when
> dealing with plain ndarrays and our derived types side-by-side.
>
> Ideally, I'd like numpy to be "fixed" - I hope that the "only" obstacle is
> that someone needs to do it, but I am afraid of someone mentioning the term
> "backward-compatibility" (Ulli would surely call it "bug-compatibility"
> here
> ;-) ).


The rule here is that the output is computed as the common input type, then
cast to the type of the output array. It will also be downcast if the output
array is of lesser precision. This has been discussed on the list, my
preference would have been to raise an error on mismatched types but the
consensus was to keep the current casting behaviour. What you are asking for
is that the ufunc upcast the arrays before the addition if the output array
is of higher precision. This complicates the logic. I think it is better in
this case to explicitly make a of higher precision before the call. Note
that the ufunc loops don't generally handle mixed types with the exception
of logical functions and, IIRC, the exponent/mantissa functions.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090722/2df0f18e/attachment.html 


More information about the NumPy-Discussion mailing list