[Numpy-discussion] [numarray issue] nonzero? Comments on new name solicited

Perry Greenfield perry at stsci.edu
Mon May 5 10:23:12 CDT 2003


[In the next week or so I am going to be raising some issues
regarding the next numarray release, 0.6. By the way, we keep the
version number less than 1.0 because we still hold out the
possiblity of changing some interface issues (generally it has
been in the direction of making numarray more compatible with
Numeric). The following raises one of these interface issues.

numarray has a more general approach to the nonzero function 
than does Numeric. It is able to return multiple index arrays
for multidimensional arrays (where as nonzero only works for
1-d arrays). In the interest of giving a more generic interface
for the function, numarray always returns the index array(s)
as part of a tuple, even if the argument to nonzero is 1-D and
there is only one resulting index array.

However, this is a problem for Numeric old code that uses nonzero
and expects an array instead of a tuple. The sensible thing to do
is to retain  compatibilty with the Numeric nonzero (and likewise
only  support 1-d arrays). So we need a new name for our new nonzero
function. To tell you the truth, I always felt that nonzero
was a terrible name anyway and much preferred the name that
IDL used for that function, namely "where". Alas, "where" is
already used for another purpose in Numeric. But to illustrate,
which is clearer?

x[nonzero(x > 0.)] = 0.

or

x[where(x > 0.)] = 0

So, we considered some other alternatives, in particular

nz
wheretrue 

but our favorite is: "at". For example:

x[at(x > 0.)] = 0.

This has the benefit of being short and having a fairly clear
meaning. Short is important since it will keep expressions
of arrays using index arrays short and more readable.

True, this is more likely to conflict with existing user variable or
function names,  but if one is simply trying to use existing files,
then there is no problem since they won't need the "at" function since
its functionality was not previously available.

Any objections to using "at"? Any better suggestions.

Of course, these changes will break existing numarray code that
use the nonzero function.

Perry Greenfield





More information about the Numpy-discussion mailing list