[PYTHON MATRIX-SIG] changes to compress

Perry A. Stoll stoll@atr-sw.atr.co.jp
Fri, 16 Feb 1996 16:55:06 +0900


Howdy All,

Question: How is the function Numeric.compress supposed to behave if
none of the elements of m meet the condition?

def compress(condition, m, dimension=-1):
	"""compress(condition, x, dimension=-1) = those elements of x corresponding 
	to those elements of condition that are "true".  condition must be the
	same size as the given dimension of x."""

Right now it raises an exception. I think it should return some
NULL-type object, either None, [] or maybe an empty array object if
such a thing exists. 

The problem stems from array methods not accepting empty lists as
arguments. Couldn't it just return an empty Array object, which would
act just like any other empty mapping or sequence object?

For now, I've changed compress to:

def compress(condition, m, dimension=-1):
	nonzero = condition.nonzero()
	if (nonzero): return m.take(nonzero, dimension)
	else:         return []

I noticed this while using Konrad's pretty printer - did anyone else
notice this? Konrad?

-Perry

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

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