efficent test for array with only one value?

Robert Kern rkern at ucsd.edu
Tue Jan 20 00:35:17 EST 2004


Kyler Laird wrote:
> Robert Kern <rkern at ucsd.edu> writes:
> 
> 
>>>>>from Numeric import *
>>>>>a = ones((3,5))
>>>>>equal.reduce(a.flat)
>>
>>1
>>
>>>>>a[0,3] = 0
>>>>>equal.reduce(a.flat)
>>
>>0
>>
> 
>>Ufuncs are wonderful things.
> 
> 
> Yeah, but they also don't work for this.  First, I'm guessing that reduce
> will not stop immediately when equal encounters a False.  (It doesn't do
> "and".)
> 
> Also, it just plain doesn't work.
> 	>>> import Numeric
> 	>>> a = Numeric.zeros((3,5))
> 	>>> Numeric.equal.reduce(a.flat)
> 	0
> 	>>> Numeric.equal.reduce([0,0,0,0,0,0,1])
> 	1

Yeah, I'm sorry. I had a brainfart (one of several today, alas). The 
reduce method is completely the wrong thing to use since it uses the 
*result* of the last comparison as the first argument for the following 
comparison.

However, alltrue(a.flat == a.flat[0]) will work but won't short-circuit. 
Fast, though, if the array isn't huge.

> --kyler

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the Python-list mailing list