[Numpy-discussion] Short-circuiting equivalent of np.any or np.all?

Allan Haldane allanhaldane at gmail.com
Thu Apr 26 15:21:18 EDT 2018


On 04/26/2018 12:45 PM, Nathan Goldbaum wrote:
> I'm curious if there's a way to get a fast early-terminating search in
> NumPy? Perhaps there's another package I can depend on that does this? I
> guess I could also write a bit of cython code that does this but so far
> this project is pure python and I don't want to deal with the packaging
> headache of getting wheels built and conda-forge packages set up on all
> platforms.
> 
> Thanks for your help!
> 
> -Nathan

A current PR that implements short-circuiting for "all"-like operations is:

https://github.com/numpy/numpy/pull/8528

Actually, I have a little dream that we will be able to implement this
kind of short-circuiting more generally in numpy soon, following the
idea in that PR of turning functions into gufuncs. We just need to add
some finishing touches on the gufunc implementation first.

We are almost there - the one important feature gufuncs are still
missing is support for "multiple axis" arguments. See
https://github.com/numpy/numpy/issues/8810.

Once that is done I also think there are some other new and useful
short-circuiting gufuncs we could add, like "count" and "first". See
some comments:

https://github.com/numpy/numpy/pull/8528#issuecomment-365358119

I am imagining we will end up with a "gufunc ecosystem", where there
are some core ufuncs like np.add, np.multiply, np.less_than,
and then a bunch of "associated" gufuncs for each of these, like reduce,
first, all, accessible as attributes of the core ufunc.

(It has long been vaguely planned to turn "reduce" into a gufunc too,
according to comments in the code. I'm excited for when that can happen!)

Allan


More information about the NumPy-Discussion mailing list