numexpr
numexpr copied to clipboard
Algebra for boolean values doesn't work on arrays (interpret as 0 / 1 integer)
From [email protected] on January 07, 2013 12:27:41
What steps will reproduce the problem? 1. a = arange(10) 2. numexpr.evaluate('(a == a) + (a == a)') What is the expected output? What do you see instead? boolean array of length 10 with values 2. Python also treats boolean values as integers for addition / multiplication What version of the product are you using? On what operating system? numexpr 1.4.2 on Ubuntu 12.04 64bit Please provide any additional information below. Workaround would be numexpr.evaluate('1 * (a == a) + (a == a)') which causes a cast to integer. In this specific case i used boolean algebra as a workaround to implement sign(): sign(a) := (a > 0) - (a < 0)
Original issue: http://code.google.com/p/numexpr/issues/detail?id=96
A fix for this would be highly appreciated, or at least the hack should be documented somewhere.
I've run into this issue doing something like
numexpr.evaluate("(True)+(True) >= 2")
and the current workaround is to do
numexpr.evaluate("((True)*1)+((True)*1)) >= 2")
Yes, this is a known limitation, as can be read in https://github.com/pydata/numexpr/wiki/Numexpr-Users-Guide:
"""
Also, the types in Numexpr conditions are somewhat stricter than those of Python. For instance, the only valid constants for booleans are True and False, and they are never automatically cast to integers.
"""
Message to comment on stale issues. If none provided, will not mark issues stale