From: Perry G. <pe...@st...> - 2005-05-17 16:16:25
|
On May 17, 2005, at 11:50 AM, John Hunter wrote: >>>>>> "Perry" == Perry Greenfield <pe...@st...> writes: > > Perry> Yes, indeed. > > I know you don't want to get into the business of porting numarray > functionality back to Numeric :-) but do you know of an easy way to > generalize this to N dimensions for Numeric. I took a brief look at > how numarray did it, and it relies on choose under the hood, but it > didn't look like it would be straightforward to port to Numeric. > > An Nd "find" would be useful in mpl, at least until the whole array > object scenario stabilizes. > > JDH numarray does it at a low level which wouldn't be easy to duplicate in Numeric. But I don't think it is that hard to do in Python so long as you don't mind using ravel() and generating an intermediate index array. Something like (not checked or tested, surely there's a mistake in it somewhere)? def find(condition): ind = nonzero(ravel(condition)) cshape = multiply.accumulate(condition.shape)[-1:0:-1] indices = [] # probably better done as a list comprehension but I'm too lazy to look it up for size in cshape: indices.append(ind//size) indices.append(ind % condition.shape[-1] return tuple(indices) But I may be missing a complication. Perry |