Feature request: function to get minimum and maximum values simultaneously (as a tuple)

A function to get the minimum and maximum values of an array simultaneously could be very useful, from both a convenience and performance point of view. Especially when arrays get larger the performance benefit could be significant, and even more if the array doesn't fit in L2/L3 cache or even memory. There are many cases where not either the minimum or the maximum of an array is required, but both. Think of clipping an array, getting it's range, checking for outliers, normalizing, making a plot like a histogram, etc. This function could be called aminmax() for example, and also be called like ndarray.minmax(). It should return a tuple (min, max) with the minimum and maximum values of the array, identical to calling (ndarray.min(), ndarray.max()). With such a function, numpy.ptp() and the special cases of numpy.quantile(a, q=[0,1]) and numpy.percentile(a, q=[0,100]) could also potentially be speeded up, among others. Potentially argmin and argmax could get the same treatment, being called argminmax(). There is also a very extensive post on Stack Overflow (a bit old already) with discussion and benchmarks: https://stackoverflow.com/questions/12200580/numpy-function-for-simultaneous...

On Thu, Jun 30, 2022, at 22:23, Ewout ter Hoeven wrote:
A function to get the minimum and maximum values of an array simultaneously could be very useful, from both a convenience and performance point of view. Especially when arrays get larger the performance benefit could be significant, and even more if the array doesn't fit in L2/L3 cache or even memory.
Hi, There's an open issue asking for this feature: https://github.com/numpy/numpy/issues/9836 András
There are many cases where not either the minimum or the maximum of an array is required, but both. Think of clipping an array, getting it's range, checking for outliers, normalizing, making a plot like a histogram, etc.
This function could be called aminmax() for example, and also be called like ndarray.minmax(). It should return a tuple (min, max) with the minimum and maximum values of the array, identical to calling (ndarray.min(), ndarray.max()).
With such a function, numpy.ptp() and the special cases of numpy.quantile(a, q=[0,1]) and numpy.percentile(a, q=[0,100]) could also potentially be speeded up, among others.
Potentially argmin and argmax could get the same treatment, being called argminmax().
There is also a very extensive post on Stack Overflow (a bit old already) with discussion and benchmarks: https://stackoverflow.com/questions/12200580/numpy-function-for-simultaneous... _______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: deak.andris@gmail.com

Hi, There is an implementation in silx.math.combo.minmax(): https://github.com/silx-kit/silx/blob/master/src/silx/math/combo.pyx#L266 Cheers Jerome On Thu, 30 Jun 2022 22:50:12 +0200 "Andras Deak" <deak.andris@gmail.com> wrote:
On Thu, Jun 30, 2022, at 22:23, Ewout ter Hoeven wrote:
A function to get the minimum and maximum values of an array simultaneously could be very useful, from both a convenience and performance point of view. Especially when arrays get larger the performance benefit could be significant, and even more if the array doesn't fit in L2/L3 cache or even memory.
Hi,
There's an open issue asking for this feature: https://github.com/numpy/numpy/issues/9836
András
There are many cases where not either the minimum or the maximum of an array is required, but both. Think of clipping an array, getting it's range, checking for outliers, normalizing, making a plot like a histogram, etc.
This function could be called aminmax() for example, and also be called like ndarray.minmax(). It should return a tuple (min, max) with the minimum and maximum values of the array, identical to calling (ndarray.min(), ndarray.max()).
With such a function, numpy.ptp() and the special cases of numpy.quantile(a, q=[0,1]) and numpy.percentile(a, q=[0,100]) could also potentially be speeded up, among others.
Potentially argmin and argmax could get the same treatment, being called argminmax().
There is also a very extensive post on Stack Overflow (a bit old already) with discussion and benchmarks: https://stackoverflow.com/questions/12200580/numpy-function-for-simultaneous... _______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: deak.andris@gmail.com
_______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: jerome.kieffer@esrf.fr

On 6/30/22, Ewout ter Hoeven <e.m.terhoeven@student.tudelft.nl> wrote:
A function to get the minimum and maximum values of an array simultaneously could be very useful, from both a convenience and performance point of view. Especially when arrays get larger the performance benefit could be significant, and even more if the array doesn't fit in L2/L3 cache or even memory.
There are many cases where not either the minimum or the maximum of an array is required, but both. Think of clipping an array, getting it's range, checking for outliers, normalizing, making a plot like a histogram, etc.
This function could be called aminmax() for example, and also be called like ndarray.minmax(). It should return a tuple (min, max) with the minimum and maximum values of the array, identical to calling (ndarray.min(), ndarray.max()).
With such a function, numpy.ptp() and the special cases of numpy.quantile(a, q=[0,1]) and numpy.percentile(a, q=[0,100]) could also potentially be speeded up, among others.
Potentially argmin and argmax could get the same treatment, being called argminmax().
There is also a very extensive post on Stack Overflow (a bit old already) with discussion and benchmarks: https://stackoverflow.com/questions/12200580/numpy-function-for-simultaneous...
FYI, I have a fairly simple gufunc implementation of `minmax` in ufunclab (https://github.com/WarrenWeckesser/ufunclab), along with `arg_minmax`, `min_argmin` and `max_argmax`. See README.md starting here: https://github.com/WarrenWeckesser/ufunclab#minmax For those familiar with C and gufunc implementation details, you can find the implementations in https://github.com/WarrenWeckesser/ufunclab/blob/main/src/minmax/minmax_gufu.... You'll see that, as far as gufuncs go, these are not very sophisticated. They do not include implementations for all the NumPy data types, and I haven't yet spent much time on optimization. Warren
_______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: warren.weckesser@gmail.com

On Thu, Jun 30, 2022 at 10:56 PM Warren Weckesser < warren.weckesser@gmail.com> wrote:
A function to get the minimum and maximum values of an array simultaneously could be very useful, from both a convenience and performance point of view. Especially when arrays get larger the performance benefit could be significant, and even more if the array doesn't fit in L2/L3 cache or even memory.
There are many cases where not either the minimum or the maximum of an array is required, but both. Think of clipping an array, getting it's range, checking for outliers, normalizing, making a plot like a histogram, etc.
This function could be called aminmax() for example, and also be called
On 6/30/22, Ewout ter Hoeven <e.m.terhoeven@student.tudelft.nl> wrote: like
ndarray.minmax(). It should return a tuple (min, max) with the minimum and maximum values of the array, identical to calling (ndarray.min(), ndarray.max()).
With such a function, numpy.ptp() and the special cases of numpy.quantile(a, q=[0,1]) and numpy.percentile(a, q=[0,100]) could also potentially be speeded up, among others.
Potentially argmin and argmax could get the same treatment, being called argminmax().
There is also a very extensive post on Stack Overflow (a bit old already) with discussion and benchmarks:
https://stackoverflow.com/questions/12200580/numpy-function-for-simultaneous...
FYI, I have a fairly simple gufunc implementation of `minmax` in ufunclab (https://github.com/WarrenWeckesser/ufunclab), along with `arg_minmax`, `min_argmin` and `max_argmax`. See README.md starting here: https://github.com/WarrenWeckesser/ufunclab#minmax
For those familiar with C and gufunc implementation details, you can find the implementations in
https://github.com/WarrenWeckesser/ufunclab/blob/main/src/minmax/minmax_gufu... . You'll see that, as far as gufuncs go, these are not very sophisticated. They do not include implementations for all the NumPy data types, and I haven't yet spent much time on optimization.
Thanks for sharing Warren. While that is interesting code, for inclusion in NumPy purposes I'd much prefer to see something along the suggestions by Eric and Marten on https://github.com/numpy/numpy/issues/9836 about making it easier to combine existing ufuncs. Adding 500 LoC for a `minmax` (separate from the discussion on whether we want such fused operators) does not seem healthy. It's also a new potential source of bugs, because `minmax` isn't going to yield exactly the same numerical values as `min` and `max` used separately if you do it as a gufunc, and any bug fixes will need to be made in two places. Also, given that `min` and `max` use SIMD instructions, the gufunc `minmax` as you have it now is probably substantially slower - see benchmarks in https://github.com/numpy/numpy/pull/20131. Cheers, Ralf
participants (5)
-
Andras Deak
-
Ewout ter Hoeven
-
Jerome Kieffer
-
Ralf Gommers
-
Warren Weckesser