Robert Kern <rkern@ucsd.edu> writes:
Howey, David A wrote:
I want to test if any array contains ONLY zeros.
In [1]: a = array((0,0,0,0,0))
In [2]: alltrue(a == 0)
There's also a sometrue() function, too.
Some curious timing info for v=array([1]+[0]*32+[1]), python 2.4: Numeric 23.8 numarray 1.3.3 innerproduct(v,v)!=0.0 16 usec 5 usec sometrue(v) 8 usec 84 usec alltrue(v==0.0) 21 usec 50 usec So with both Numeric and numarray, alltrue is not the best option. But whether innerproduct or sometrue is best depends strongly on which package you are using. The 84 usec result for numarray is a bit shocking, considering that it can compute the innerproduct in 5 usec. Similar timing results happen for other arrays; I believe that neither alltrue nor sometrue use short-circuit logic, so they examine all the elements even if the truth value is clear from an initial subsequence. I also am in the position that I often need to know whether an array is completely zero, so I'd find a version of sometrue which short-circuits quite useful. Dan