numexpr icon indicating copy to clipboard operation
numexpr copied to clipboard

Add minimum, maximum functions

Open FrancescAlted opened this issue 11 years ago • 15 comments

From [email protected] on June 07, 2012 16:43:59

It would be useful to add np.mininum and np.maximum functions. 'maximum(x-a, 0)' would be faster and clearer than 'where(x-a>0,x-a,0)

Original issue: http://code.google.com/p/numexpr/issues/detail?id=86

FrancescAlted avatar Jan 22 '14 10:01 FrancescAlted

From [email protected] on August 30, 2013 13:04:32

Interested in a numpy.min numpy.max equiv.

FrancescAlted avatar Jan 22 '14 10:01 FrancescAlted

This would be extremely useful, especially when searching a "nearest value", eg: abs(x-val)==min(abs(x-val)).

mythsmith avatar Nov 12 '14 08:11 mythsmith

In the context of min and max I would also suggest to implement clip. While clip(a,b,c) is equivalent to min(max(a,b),c), the former is easier to read.

Ablinne avatar Jul 06 '18 13:07 Ablinne

+1 for minimum and clip

But at least i am happy to read that i can use this where function.

vallsv avatar Sep 28 '19 15:09 vallsv

Just found this thread still open. Any progress? Interested in a np.minimum() and np.maximum() style element wise min / max!

wilko339 avatar May 30 '20 21:05 wilko339

Min and max were implemented awhile ago. There is no room left for more opcodes, so clip cannot be implemented in NumExpr 2.7. I do believe numexpr3 could have clip implemented.

robbmcleod avatar Jun 01 '20 22:06 robbmcleod

Really! That's super nice. How can we use it?

I tried all this sentences:

a = numpy.array([2,3,4])
b = numpy.array([3,2,4])

> numexpr.evaluate("min(a,2)")
ValueError: reduction axis is out of bounds

> numexpr.evaluate("min(a,b)")
TypeError: You can't use Python's standard boolean operators in NumExpr expressions.
You should use their bitwise counterparts instead: '&' instead of 'and', '|' instead of 'or',
and '~' instead of 'not'.

> numexpr.evaluate("minimum(a,2)")
TypeError: 'VariableNode' object is not callable

> numexpr.evaluate("a.min(b)")
AttributeError: 'VariableNode' object has no attribute 'min'

> numexpr.version.version                                                        
'2.7.1'

vallsv avatar Jun 02 '20 06:06 vallsv

Oh, sorry, they are reductions.

Element-wise could be done for float32 and float64 but not for integers.

robbmcleod avatar Jun 02 '20 16:06 robbmcleod

Hi,

Sorry if I misunderstood: does it mean that element-wise min and max should work with float32 and float64 arrays? I tried @vallsv expressions with a and b as float arrays, but got the same issue as for integer.

Are element-wise min and max implemented, and do you have an example?

Thanks

sebbelese avatar May 27 '21 07:05 sebbelese

Element-wise are not implemented at this time. However, you can use the where function instead. It's just uglier.

robbmcleod avatar May 27 '21 21:05 robbmcleod

Really! That's super nice. How can we use it?

I tried all this sentences:

a = numpy.array([2,3,4])
b = numpy.array([3,2,4])

> numexpr.evaluate("min(a,2)")
ValueError: reduction axis is out of bounds

> numexpr.evaluate("min(a,b)")
TypeError: You can't use Python's standard boolean operators in NumExpr expressions.
You should use their bitwise counterparts instead: '&' instead of 'and', '|' instead of 'or',
and '~' instead of 'not'.

> numexpr.evaluate("minimum(a,2)")
TypeError: 'VariableNode' object is not callable

> numexpr.evaluate("a.min(b)")
AttributeError: 'VariableNode' object has no attribute 'min'

> numexpr.version.version                                                        
'2.7.1'

Hi, Is there a way to use all these in the current version of numexpr? I tried these and same error appears.

shubham2941 avatar May 13 '22 11:05 shubham2941

Such as this:

import numexpr as ne
import numpy as np

a = np.array([2,3,4])
b = np.array([3,2,4])

out1 = ne.evaluate('where(a < 2, a, 2)')
out2 = ne.evaluate('where(a < b, a, b)')

robbmcleod avatar May 13 '22 16:05 robbmcleod

Thanks, @robbmcleod. I somehow missed that function earlier.

Is there a way to define your own operator in numexpr? For eg., if you want to do multiple operations on two parameters in a user-defined function. If there is a post somewhere or some documentation, can someone point to it? Any suggestions is welcome.

shubham2941 avatar May 16 '22 01:05 shubham2941

No, I'd suggest you look at numba for such a task.

robbmcleod avatar May 16 '22 01:05 robbmcleod

I am not sure how to use numba with numexpr.evaluate and user-defined function.

Let me explain my issue with numexpr.evaluate in detail:

I have a string function in the form with data in variables A and B in data dictionary form: def ufunc(A,B): ............ return var The evaluation function goes like this: numexpr.evaluate( 'A + B**2 + A*B + sin(A) + ufunc(A,B)' , data_dict) The problem is I don't how to send the 'ufunc' information to 'numexpr.evaluate'. I am not sure how 'numba' will help with that.

shubham2941 avatar May 16 '22 02:05 shubham2941

I was evaluating this library and not having min or max operator is a big negative aspect... is there any plan to implement them? It's very strange not using min or max in the formula and have to use the where function.

Message to comment on stale issues. If none provided, will not mark issues stale

github-actions[bot] avatar Mar 09 '24 01:03 github-actions[bot]