[Numpy-discussion] Add/multiply reduction confusion

Andrew Friedley afriedle at indiana.edu
Mon Jun 29 10:42:35 EDT 2009


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



More information about the NumPy-Discussion mailing list