[PYTHON MATRIX-SIG] bug and fix for Numeric.where

Perry A. Stoll pas@lems.brown.edu
Wed, 22 May 1996 10:43:42 -0400 (EDT)



While were looking at bugs...

I finally understand the beauty of the Numeric.where function (and the
"choose" function, but that's a separate story). But I think the
function in Numeric is wrong. Here's a replacement. The function is
pretty trivial, but I like the way it reads in code and vote for
keeping it.

def where(condition, x, y):
	"""where(condition,x,y) is shaped like condition and has elements of x and
	y where condition is respectively true or false
	"""
	return condition.choose(y, x) # removed condition as first argument

As a test demonstrating the problem and fix:
  Select elements from either a or b, which ever is greater - exactly
what the "where" function is for.

>>> import Numeric
>>> a = Numeric.arange(10)
>>> b = Numeric.zeros(10,'l') + 4
>>> Numeric.where(a.greater(b),a,b)
0 0 0 0 0 4 4 4 4 4                 # huh??

>>> where(a.greater(b),a,b)
4 4 4 4 4 5 6 7 8 9                 # ahhh, that's better.

-Perry
 <pas@lems.brown.edu>


=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================