Re: [Numpy-discussion] sum and prod

2012-09-09 Thread nicky van foreest
On 9 September 2012 00:10, Warren Weckesser
warren.weckes...@enthought.com wrote:


 On Sat, Sep 8, 2012 at 4:56 PM, nicky van foreest vanfore...@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@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion



 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] sum and prod

2012-09-09 Thread nicky van foreest
Thanks for your hints.

NIcky


On 9 September 2012 00:30, eat e.antero.ta...@gmail.com wrote:
 Hi,

 On Sun, Sep 9, 2012 at 12:56 AM, nicky van foreest vanfore...@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])

 Can't see why someone would write code like above, but anyway:
 In []: args = np.array([4,8])
 In []: print np.sum( (arg  0) for arg in args)
 2
 In []: print np.sum([(arg  0) for arg in args])
 2
 In []: print np.prod( (arg  0) for arg in args)
 generator object genexpr at 0x062BDA08
 In []: print np.prod([(arg  0) for arg in args])
 1
 In []: print np.prod( (arg  0) for arg in args).next()
 True
 In []: sys.version
 Out[]: '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'
 In []: np.version.version
 Out[]: '1.6.0'

 My 2 cents,
 -eat


 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
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion



 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] sum and prod

2012-09-09 Thread Han Genuit
 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..
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] sum and prod

2012-09-08 Thread nicky van foreest
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
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] sum and prod

2012-09-08 Thread Han Genuit
Hi,

Maybe try something like this?

 args = np.array([4,8])
 np.prod(args  0)
1
 np.sum(args  0)
2

Cheers,
Han
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] sum and prod

2012-09-08 Thread Warren Weckesser
On Sat, Sep 8, 2012 at 4:56 PM, nicky van foreest vanfore...@gmail.comwrote:

 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).



 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@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] sum and prod

2012-09-08 Thread eat
Hi,

On Sun, Sep 9, 2012 at 12:56 AM, nicky van foreest vanfore...@gmail.comwrote:

 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])

Can't see why someone would write code like above, but anyway:
In []: args = np.array([4,8])
In []: print np.sum( (arg  0) for arg in args)
2
In []: print np.sum([(arg  0) for arg in args])
2
In []: print np.prod( (arg  0) for arg in args)
generator object genexpr at 0x062BDA08
In []: print np.prod([(arg  0) for arg in args])
1
In []: print np.prod( (arg  0) for arg in args).next()
True
In []: sys.version
Out[]: '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'
In []: np.version.version
Out[]: '1.6.0'

My 2 cents,
-eat


 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
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion