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


_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to