[Numpy-discussion] Add/multiply reduction confusion

Andrew Friedley afriedle@indiana....
Sun Jul 5 10:45:50 CDT 2009


I figured this out in case anyone is interested.

I found the check that does the type 'upcasting' in 
umath_ufunc_object.inc around line 3072 (NumPy 1.3.0).  Turns out all I 
need to do is make sure my add and multiply ufuncs are actually named 
'add' and 'multiply' and arrays will be upcasted appropriately.

Maybe this is worth documenting somewhere, maybe in the UFunc C API?  Or 
is it documented already, and I just missed it?

Andrew

Andrew Friedley wrote:
> Hi,
> 
> I'm trying to understand how integer types are upcast for add/multiply 
> operations for my GSoC project (Implementing Ufuncs using CorePy).
> 
> The documentation says that for reduction with add/multiply operations, 
> integer types are 'upcast' to the int_ type (int64 on my system).  What 
> exactly does this mean, internally?  Where/when does the upcasting 
> occur? Is it a C-style cast, or a memory copy to a new temporary array?
> 
> I'm a confused as to which low-level ufunc loop type is used (and why). 
>   This is what I see:
> 
>  >>> a = numpy.arange(131072, dtype=numpy.int32)
>  >>> r = numpy.add.reduce(a)
>  >>> print type(r)
> <type 'numpy.int64'>
>  >>> print hex(r)
> 0x1ffff0000L
> 
> Okay, fine.  But I have my own ufunc, which defines only the following 
> types right now (I stripped it down for debugging):
> 
>  >>> print corefunc.add.types
> ['ii->i', 'll->l']
> 
> NumPy has this, for comparison:
> 
>  >>> print numpy.add.types
> ['??->?', 'bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l', 
> 'LL->L', 'qq->q', 'QQ->Q', 'ff->f', 'dd->d', 'gg->g', 'FF->F', 'DD->D', 
> 'GG->G', 'OO->O']
> 
> Also just to verify I did this:
> 
>  >>> print numpy.typeDict['i']
> <type 'numpy.int32'>
>  >>> print numpy.typeDict['l']
> <type 'numpy.int64'>
> 
> 
> Yet when I call my own ufunc, this happens:
> 
>  >>> a = numpy.arange(131072, dtype=numpy.int32)
>  >>> r = corefunc.add.reduce(a)
>  >>> print type(r)
> <type 'numpy.int32'>
>  >>> print hex(r)
> -0x10000
> 
> It looks like no upcasting is occurring here?  My ii->i loop is being 
> used, not the ll->l loop.. why?  I'm guessing this is something I am 
> doing wrong, any ideas what it is?
> 
> Andrew
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion


More information about the NumPy-Discussion mailing list