[Numpy-discussion] Type of 1st argument in Numexpr where()
Tim Hochberg
tim.hochberg at ieee.org
Wed Dec 20 10:20:01 CST 2006
Ivan Vilata i Balaguer wrote:
> Hi all,
>
> I noticed that the set of ``where()`` functions defined by Numexpr all
> have a signature like ``xfxx``, i.e. the first argument is a float and
> the return, second and third arguments are of the same type (whatever it
> is).
>
> Since the first argument effectively represents a condition, wouldn't it
> make more sense for it to be a boolean? Booleans are already supported
> by Numexpr, maybe the old signatures are just a legacy from the time
> when Numexpr didn't support them.
>
>
Actually, this is on purpose. Numpy.where (and most other switching
constructs in Python) will switch on almost anything. In particular, any
number that is nonzero is considered True, zero is considered False. By
changing the signature, you're restricting where to only accepting
booleans. Since booleans and ints can by freely cast to doubles in
numexpr, always using float for the condition saves us a couple of opcodes.
[I just realized that numpy.where also handles complex conditions, and I
suspect that numexpr.where will refuse those. That should probably be
fixed at some point I suppose]
Anyway, in theory it would be more efficient to supply a separate
boolean version of the opcode in *addition* to the float version (and
potentially an int version as well although that is less compelling),
since it would save a cast. However, I'm always worried that increasing
the opcode count is going to slow down the numexpr interpreter, so I
tend to push back on those unless it's demonstrably a speed win.
regards
-tim
More information about the Numpy-discussion
mailing list