[Numpy-discussion] sum and prod

nicky van foreest vanforeest@gmail....
Sat Sep 8 16:56:56 CDT 2012


Hi,

I ran the following code:

        args = np.array([4,8])
        print np.sum( (arg > 0) for arg in args)
        print np.sum([(arg > 0) for arg in args])
        print np.prod( (arg > 0) for arg in args)
        print np.prod([(arg > 0) for arg in args])

with this result:

2
1
<generator object <genexpr> at 0x1c70410>
1

Is the difference between prod and sum intentional? I would expect
that  numpy.prod would also work on a generator, just like numpy.sum.

BTW: the last line does what I need: the product over the truth values
of all elements of args. Is there perhaps a nicer (conciser) way to
achieve this?  Thanks.

Nicky


More information about the NumPy-Discussion mailing list