numexpr icon indicating copy to clipboard operation
numexpr copied to clipboard

Algebra for boolean values doesn't work on arrays (interpret as 0 / 1 integer)

Open FrancescAlted opened this issue 11 years ago • 3 comments

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

FrancescAlted avatar Jan 22 '14 10:01 FrancescAlted

A fix for this would be highly appreciated, or at least the hack should be documented somewhere.

filmor avatar Sep 19 '14 09:09 filmor

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")

kratsg avatar Sep 28 '15 13:09 kratsg

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. """

FrancescAlted avatar Sep 30 '15 14:09 FrancescAlted

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

github-actions[bot] avatar Feb 22 '24 01:02 github-actions[bot]