efficent test for array with only one value?

Kyler Laird Kyler at news.Lairds.org
Tue Jan 20 10:12:05 EST 2004


Robert Kern <rkern at ucsd.edu> writes:

>However, alltrue(a.flat == a.flat[0]) will work but won't short-circuit. 

How did I miss that?!  Yes, that's the kind of operation I sought.
I'm surprised that it doesn't short-circuit.  It *should*, right?

It's a shame that it only works along a single axis.  The use of
"flat" is a problem for me.  I'm looking at sections of an image
(building a quadtree) so the arrays I'm checking are not contiguous.
I think that means I have to resort to something ugly like this.
	Numeric.alltrue(Numeric.alltrue(a == a[0,0]))
That eliminates many opportunities for short-circuiting.

I can also flatten the array using reshape() before checking it.  I
assume that also means a lot of possibly unnecessary operations.
(Does reshape() return a copy of the array or just an array with
the original data and a new shape?)

>Fast, though, if the array isn't huge.

Indeed, I think I'll use it.  I can always write a clean short-
circuiting C version later.

Thank you!

--kyler



More information about the Python-list mailing list