numexpr icon indicating copy to clipboard operation
numexpr copied to clipboard

BUG: power with integer arrays giving confusing error

Open jorisvandenbossche opened this issue 3 years ago • 0 comments

Consider the following example:

import numpy as np
import numexpr as na

The power operation with an integer array and a scalar works:

In [60]: array = np.arange(20)
    ...: print(ne.evaluate("array ** 1"))
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

But with two integer arrays I get a (wrong) error message:

In [61]: array = np.arange(20)
    ...: print(ne.evaluate("array ** array"))
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/miniconda3/envs/dev/lib/python3.7/site-packages/numexpr/necompiler.py in evaluate(ex, local_dict, global_dict, out, order, casting, **kwargs)
    826     try:
--> 827         compiled_ex = _numexpr_cache[numexpr_key]
    828     except KeyError:

KeyError: ('array ** array', (('optimization', 'aggressive'), ('truediv', False)), (('array', <class 'numpy.int64'>),))

During handling of the above exception, another exception occurred:
...

~/miniconda3/envs/dev/lib/python3.7/site-packages/numexpr/expressions.py in pow_op(a, b)
    287         b.astKind in ('int', 'long') and
    288         a.astKind in ('int', 'long') and
--> 289         numpy.any(b.value < 0)):
    290 
    291         raise ValueError(

TypeError: '<' not supported between instances of 'str' and 'int'

While power with 2 float arrays works fine:

In [62]: array = np.arange(20, dtype="float64")
    ...: print(ne.evaluate("array ** array"))
[1.00000000e+00 1.00000000e+00 4.00000000e+00 2.70000000e+01
 2.56000000e+02 3.12500000e+03 4.66560000e+04 8.23543000e+05
 1.67772160e+07 3.87420489e+08 1.00000000e+10 2.85311671e+11
 8.91610045e+12 3.02875107e+14 1.11120068e+16 4.37893890e+17
 1.84467441e+19 8.27240262e+20 3.93464081e+22 1.97841966e+24]

It could be that array ** array isn't implemented for integer arrays, but then the error message is a bit confusing.

I am using numexpr 2.7.3 on Linux (Ubuntu), installed from conda-forge.

jorisvandenbossche avatar Apr 01 '21 11:04 jorisvandenbossche