I guess this is a numpy question, but how do I do truth tests on arrays? I'm trawling around the documentation but struggling to find an answer.. I want to test if any array contains ONLY zeros. Originally I had: if a == 0: # code in here I need this to be true both for a = 0 # ie a is just a number and for a = array((0,0,0,0,0)) It must return false for any array with any nonzero elements anywhere At the moment python does this (which is wrong):
vin = array((1.,0,0)) vin array([ 1., 0., 0.]) if vin == 0: print "yes"
yes
Thanks Dave
Howey, David A wrote:
I guess this is a numpy question, but how do I do truth tests on arrays? I'm trawling around the documentation but struggling to find an answer..
I want to test if any array contains ONLY zeros.
Originally I had: if a == 0: # code in here
I need this to be true both for a = 0 # ie a is just a number
and for a = array((0,0,0,0,0))
It must return false for any array with any nonzero elements anywhere
In [1]: a = array((0,0,0,0,0)) In [2]: alltrue(a == 0) Out[2]: 1 In [3]: a == 0 Out[3]: array([1, 1, 1, 1, 1],'b') In [4]: a[1] = 10 In [5]: alltrue(a == 0) Out[5]: 0 In [6]: a == 0 Out[6]: array([1, 0, 1, 1, 1],'b') There's also a sometrue() function, too. -- Robert Kern rkern@ucsd.edu "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter
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
Howey, David A wrote:
I guess this is a numpy question, but how do I do truth tests on arrays? I'm trawling around the documentation but struggling to find an answer..
I want to test if any array contains ONLY zeros.
Originally I had: if a == 0: # code in here
I need this to be true both for a = 0 # ie a is just a number
and for a = array((0,0,0,0,0))
Have a look at sometrue/alltrue: e.g. alltrue(a==0.0) returns True if all elements are zero. Christian
I'm sorry this is more of a numerix (or even a lapack) problem than a direct SciPy problem. I've been trying to make up Numeric & numarray on a Linux opteron box, using atlas/lapack. Numeric eventually seemed to work and pass its tests, though I had to compile ATLAS and LAPACK with -fPIC. ATLAS passed sanity tests. numarray-1.3.3 is proving more difficult. Worked out that installing numarray with python setup.py config --use_lapack install --gencode is the (undocumented) way to link to external libraries. No compilation or installation problems, but when I ran testall.test() found 3 problems to do with determinants (messages at end). Sign of 2nx2n determinants seems to be wrong. e.g. import numarray.linear_algebra.LinearAlgebra2 as la2 import numarray.numeric as num a = num.array([[1.,2.], [3.,4.]]) print la2.determinant(a) -> gives 2.0 (should be -2) but 2n+1 x2n+1 seems OK. c = num.array([[1,0,0],[0,1.,-2.], [0,3.,4.]]) print la2.determinant(b) -> gives (correctly) 10.0 Default installation w/o lapack gives the right answers. So I tested lapack by compiling *all* the lapack stuff, (including the test suite) instead of just the lapacklib i.e. I did make all instead of make lapacklib This gave 'major' (i.e. order 10^16) errors in cgd.out, csep.out, ded.out, dgd.out, sgd.out, ssep.out,ssvd.out, zgd.out. Now [cdsz]gd.out errors are apparently expected -- see http://web.mit.edu/lapack/www/faq.html#1.24 But errors in ded.out,ssep.out, ssvd.out may be more serious. Are they causing the incorrect-sign determinants? Puzzled. --George Nurser. compilation method --------------------------- ATLAS. make xconfig ./xconfig -F f '-fPIC -fomit-frame-pointer -O -m64 -fno-second-underscore' -F c '-fPIC -fomit-frame-pointer -O -mfpmath=387 -m64' -F m '-fPIC -fomit-frame-pointer -O -mfpmath=387 -m64' -b /usr/lib/libblas.a make install arch=Linux_HAMMER64SSE2 LAPACK. use ATLAS blas; OPTS/NOOPT following http://www.scipy.org/mailinglists/mailman?fn=scipy-user/2005-February/ 004066.html so in make.inc OPTS = -funroll-all-loops -O3 -m64 -fno-second-underscore -fPIC NOOPT = -m64 -fno-second-underscore -fPIC BLASLIB = .....ATLAS/lib/Linux_HAMMER64SSE2/f77blas.a lapack errors ------------------ grep ail * cgd.out: Matrix types (see CDRGEV for details): cgd.out: CGV drivers: 63 out of 1092 tests failed to pass the threshold csep.out: Matrix types (see xDRVST for details): csep.out: CST drivers: 1 out of 11664 tests failed to pass the threshold ded.out: Matrix types (see DDRVES for details): ded.out: DES: 1 out of 3270 tests failed to pass the threshold ded.out: Matrix types (see DDRVSX for details): ded.out: DSX: 1 out of 3500 tests failed to pass the threshold dgd.out: Matrix types (see DDRGEV for details): dgd.out: DGV drivers: 8 out of 1092 tests failed to pass the threshold dgd.out: DXV drivers: 200 out of 5000 tests failed to pass the threshold sgd.out: Matrix types (see SDRGEV for details): sgd.out: SGV drivers: 8 out of 1092 tests failed to pass the threshold sgd.out: SXV drivers: 37 out of 5000 tests failed to pass the threshold ssep.out: Matrix types (see SCHKST for details): ssep.out:Test performed: see SCHKST for details. ssep.out: SST: 2 out of 4662 tests failed to pass the threshold ssep.out: Matrix types (see xDRVST for details): ssep.out: SST drivers: 1 out of 14256 tests failed to pass the threshold ssvd.out: Matrix types (see xCHKBD for details): ssvd.out: SBD: 1 out of 5510 tests failed to pass the threshold zgd.out: Matrix types (see ZDRGEV for details): zgd.out: ZGV drivers: 62 out of 1092 tests failed to pass the threshold zgd.out: ZXV drivers: 24 out of 5000 tests failed to pass the threshold
participants (5)
-
Christian Kristukat -
Dan Christensen -
George Nurser -
Howey, David A -
Robert Kern