[Numpy-discussion] sum and prod

Han Genuit hangenuit at gmail.com
Sun Sep 9 19:43:34 EDT 2012

>> Is the difference between prod and sum intentional? I would expect
>> that  numpy.prod would also work on a generator, just like numpy.sum.
> Whatever the correct result may be, I would expect them to have the same
> behavior with respect to a generator argument.

I found out that np.sum() has some special treatment in
fromnumeric.py, where in case of a generator argument it uses the
Python sum() function instead of the NumPy one. This is not the case
for np.prod(), where the generator argument stays NPY_OBJECT in
PyArray_GetArrayParamsFromObject. There is no NumPy code for handling
generators, except for np.fromiter(), but that needs a dtype (which
cannot be inferred automatically before running the generator). It
might be more consistent to add special generator cases to other NumPy
functions as well, using Python reduce() or imap(), but I'm not sure
about the best way to solve this..

More information about the NumPy-Discussion mailing list