[Numpy-tickets] [NumPy] #997: [proposal][patch] bool(array) should "do the right thing" in the unambiguous cases.

NumPy numpy-tickets@scipy....
Tue Feb 3 18:07:42 CST 2009


#997: [proposal][patch] bool(array) should "do the right thing" in the
unambiguous cases.
-------------------------+--------------------------------------------------
 Reporter:  alsuren      |       Owner:  somebody                                                          
     Type:  enhancement  |      Status:  new                                                               
 Priority:  normal       |   Milestone:  1.3.0                                                             
Component:  Other        |     Version:  none                                                              
 Severity:  normal       |    Keywords:  assert, bool, ValueError, array, ambiguous, inconsistent, compare,
-------------------------+--------------------------------------------------
 After stumbling across http://mail.python.org/pipermail/python-
 list/2008-December/519124.html

 I thought I might as well make a concrete proposal. The test case proposed
 in the email is:

 >>>> import numpy
 >>>> y = numpy.zeros((3,))
 >>>> y
 > array([ 0.,  0.,  0.])
 >>>> bool(y==y)
 > Traceback (most recent call last):
 >   File "<stdin>", line 1, in <module>
 > ValueError: The truth value of an array with more than one element is
 > ambiguous. Use a.any() or a.all()
 >>>> ll1 = [y,1]
 >>>> y in ll1
 > True
 >>>> ll2 = [1,y]
 >>>> y in ll2
 > Traceback (most recent call last):
 >   File "<stdin>", line 1, in <module>
 > ValueError: The truth value of an array with more than one element is
 > ambiguous. Use a.any() or a.all()


 May I suggest that:
  bool(y==y) -> True
  bool(zeros((5,))==ones((5,))) -> False
 are not ambiguous: in each case, any(array)==all(array), and this value
 should simply be returned.

 I'll attach a patch against 1.1.1 which shows the general idea, but which
 I've not tested.

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


More information about the Numpy-tickets mailing list