[Numpy-discussion] sort bug

Travis Oliphant oliphant.travis@ieee....
Thu Apr 26 03:26:59 CDT 2007


Anton Sherwood wrote:
> This code --
>
> 	adj = [ [eval(y) for y in x.split()] for x in infile ]
> 	val,vec = numpy.linalg.eig(adj)
> 	master = zip( val, vec.transpose() )
> 	master.sort()
>
> *sometimes* gives this error:
>
> 	Traceback (most recent call last):
> 	  File "3work.py", line 14, in <module>
> 	    master.sort()
> 	ValueError: The truth value of an array with more
> 	than one element is ambiguous. Use a.any() or a.all()
>
> What does sort() care about truth values?!
>   

The sort method on lists basically uses cmp(x,y) to determine sort 
order.  In this case, I suspect you are getting errors whenever you have 
equal-valued eigenvalues so the comparison has to go to the second item 
in the tuple (which is the array).   

cmp(x,y) must return -1, 0, or 1 which doesn't work on arrays with more 
than 1 element because it is ambiguous.  Thus you get this error.

The operation is undefined.  What do you actually want to do when you 
have equal-valued eigenvalues?



-Travis





More information about the Numpy-discussion mailing list