Skip to content

Commit 27f9354

Browse files
committed
BUG: Only convert 0d arrays to scalars in power
Operations such as `x**array([2])` would convert the 2 into an integer and loose the dimension information, because the array (at this time, it is deprecated), supports `__index__` even though it is not 0-d. This fixes it, by not trying the index machinery when it was an array, since it is unnecessary. Closes numpygh-4145
1 parent e5139b7 commit 27f9354

2 files changed

Lines changed: 33 additions & 19 deletions

File tree

‎numpy/core/src/multiarray/number.c‎

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -315,32 +315,41 @@ is_scalar_with_conversion(PyObject *o2, double* out_exponent)
315315
*out_exponent = PyFloat_AsDouble(o2);
316316
return NPY_FLOAT_SCALAR;
317317
}
318-
if ((PyArray_IsZeroDim(o2) &&
319-
((PyArray_ISINTEGER((PyArrayObject *)o2) ||
320-
(optimize_fpexps && PyArray_ISFLOAT((PyArrayObject *)o2))))) ||
321-
PyArray_IsScalar(o2, Integer) ||
322-
(optimize_fpexps && PyArray_IsScalar(o2, Floating))) {
323-
temp = Py_TYPE(o2)->tp_as_number->nb_float(o2);
324-
if (temp != NULL) {
318+
if (PyArray_Check(o2)) {
319+
if ((PyArray_NDIM(o2) == 0) &&
320+
((PyArray_ISINTEGER((PyArrayObject *)o2) ||
321+
(optimize_fpexps && PyArray_ISFLOAT((PyArrayObject *)o2))))) {
322+
temp = Py_TYPE(o2)->tp_as_number->nb_float(o2);
323+
if (temp == NULL) {
324+
return NPY_NOSCALAR;
325+
}
325326
*out_exponent = PyFloat_AsDouble(o2);
326327
Py_DECREF(temp);
327-
if (PyArray_IsZeroDim(o2)) {
328-
if (PyArray_ISINTEGER((PyArrayObject *)o2)) {
329-
return NPY_INTPOS_SCALAR;
330-
}
331-
else { /* ISFLOAT */
332-
return NPY_FLOAT_SCALAR;
333-
}
328+
if (PyArray_ISINTEGER((PyArrayObject *)o2)) {
329+
return NPY_INTPOS_SCALAR;
334330
}
335-
else if PyArray_IsScalar(o2, Integer) {
336-
return NPY_INTPOS_SCALAR;
337-
}
338-
else { /* IsScalar(o2, Floating) */
331+
else { /* ISFLOAT */
339332
return NPY_FLOAT_SCALAR;
340333
}
341334
}
342335
}
343-
if (PyIndex_Check(o2)) {
336+
else if (PyArray_IsScalar(o2, Integer) ||
337+
(optimize_fpexps && PyArray_IsScalar(o2, Floating))) {
338+
temp = Py_TYPE(o2)->tp_as_number->nb_float(o2);
339+
if (temp == NULL) {
340+
return NPY_NOSCALAR;
341+
}
342+
*out_exponent = PyFloat_AsDouble(o2);
343+
Py_DECREF(temp);
344+
345+
if (PyArray_IsScalar(o2, Integer)) {
346+
return NPY_INTPOS_SCALAR;
347+
}
348+
else { /* IsScalar(o2, Floating) */
349+
return NPY_FLOAT_SCALAR;
350+
}
351+
}
352+
else if (PyIndex_Check(o2)) {
344353
PyObject* value = PyNumber_Index(o2);
345354
Py_ssize_t val;
346355
if (value==NULL) {

‎numpy/core/tests/test_umath.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ def test_fast_power(self):
173173
x = np.array([1, 2, 3], np.int16)
174174
assert_((x**2.00001).dtype is (x**2.0).dtype)
175175

176+
# Check that the fast path ignores 1-element not 0-d arrays
177+
res = x ** np.array([[[2]]])
178+
assert_equal(res.shape, (1, 1, 3))
179+
180+
176181
class TestLog2(TestCase):
177182
def test_log2_values(self) :
178183
x = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]

0 commit comments

Comments
 (0)