[Numpy-tickets] [NumPy] #414: bug in reduce for numpy.array

NumPy numpy-tickets at scipy.net
Mon Jan 8 11:28:32 CST 2007

#414: bug in reduce for numpy.array
 Reporter:  lbolla  |       Owner:  somebody
     Type:  defect  |      Status:  new     
 Priority:  normal  |   Milestone:          
Component:  Other   |     Version:          
 Severity:  normal  |    Keywords:          
 Wrong results are given by subtract.reduce and divide.reduce, with respect
 to the standard function reduce.
 Take a look at this piece of code:

 from numpy import *

 x = arange(4)            # x = array([0,1,2,3])

 def myadd(x,y):          # re-define the binary sum function

     return x + y

 print reduce(myadd, x)   # 6, as expected

 print add.reduce(x)      # 6, as expected

 def mysub(x,y):          # re-define the binary diff function

     return x - y

 print reduce(mysub, x)   # -6, as expected

 print subtract.reduce(x) # 2 ---> WRONG!


 It probably depends on the wrong order of the operands in the binary
 operation subtract or divide. Any non-commutative operation can lead to
 this problem (and it's worth checking!).
 This code works correctly if the Numeric package is imported istead of
 numpy (first row of the snippet).

Ticket URL: <http://projects.scipy.org/scipy/numpy/ticket/414>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.

More information about the Numpy-tickets mailing list