[Scipy-tickets] [SciPy] #414: Wrong behaviour of where() function in NumExpr
SciPy
scipy-tickets@scipy....
Mon May 7 12:46:26 CDT 2007
#414: Wrong behaviour of where() function in NumExpr
---------------------+------------------------------------------------------
Reporter: ivilata | Owner: cookedm
Type: defect | Status: new
Priority: low | Milestone:
Component: numexpr | Version:
Severity: normal | Keywords: numexpr where boolean constant
---------------------+------------------------------------------------------
It seems that Numexpr taking 1 and 0 as boolean by defaul causes some
trouble with its implementation of `where()`:
{{{
>>> a = numpy.arange(0,5)
>>> numexpr.evaluate('where(a < 2, 2, 0)')
array([2, 2, 0, 0, 0])
>>> numexpr.evaluate('where(a < 2, 1, 0)')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "numexpr/compiler.py", line 625, in evaluate
compiled_ex = _numexpr_cache[numexpr_key] = \
File "numexpr/compiler.py", line 513, in numexpr
threeAddrProgram, inputsig, tempsig, constants, input_names = \
File "numexpr/compiler.py", line 465, in precompile
ast = typeCompileAst(ast)
File "numexpr/compiler.py", line 129, in typeCompileAst
raise NotImplementedError(
NotImplementedError: couldn't find matching opcode for 'where_bbbb'
}}}
Numexpr turns 1 and 0 into boolean values. `a < 2` is also a boolean
operation, so the signature of that call should be `where_bbbb` (upcast to
`where_bfbb` to be consistent with other Numexpr `where` declarations).
There is no such definition, though, which makes sense since `where(a < 2,
True, False)` is equivalent to `a < 2`.
I've worked out attachment:numexpr_where_bfbb.diff to make this work. It
implements a `where_bfbb` function with the hope that the result will be
upcast to an integer. A more elegant solution would be to accept True and
False as boolean constants.
This has also been debated in [http://www.pytables.org/trac/ticket/56
PyTables ticket 56]. BTW, the version of Numexpr in !PyTables supports
boolean constants, though its boolean type is no longer considered an
integer.
--
Ticket URL: <https://projects.scipy.org/scipy/scipy/ticket/414>
SciPy <http://www.scipy.org/>
SciPy is open-source software for mathematics, science, and engineering.
More information about the Scipy-tickets
mailing list