[Numpy-discussion] sum and prod

nicky van foreest vanforeest at gmail.com
Sun Sep 9 04:23:59 EDT 2012


On 9 September 2012 00:10, Warren Weckesser
<warren.weckesser at enthought.com> wrote:
>
>
> On Sat, Sep 8, 2012 at 4:56 PM, nicky van foreest <vanforeest at gmail.com>
> wrote:
>>
>> 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
>
>
>
> I get 2 here, not 1 (numpy version 1.6.1).

Sorry. Typo.

>
>
>>
>> <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.
>
>
>
> Whatever the correct result may be, I would expect them to have the same
> behavior with respect to a generator argument.
>
>
>>
>> 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.
>
>
>
> How about:
>
> In [15]: np.all(args > 0)
> Out[15]: True
>
>
>  Warren
>
>
>
>>
>> Nicky
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list