Skip to content

Commit

Permalink
BUG: Only convert 0d arrays to scalars in power
Browse files Browse the repository at this point in the history
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 gh-4145
  • Loading branch information
juliantaylor committed Mar 2, 2014
1 parent e5139b7 commit 27f9354
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
47 changes: 28 additions & 19 deletions numpy/core/src/multiarray/number.c
Expand Up @@ -315,32 +315,41 @@ is_scalar_with_conversion(PyObject *o2, double* out_exponent)
*out_exponent = PyFloat_AsDouble(o2);
return NPY_FLOAT_SCALAR;
}
if ((PyArray_IsZeroDim(o2) &&
((PyArray_ISINTEGER((PyArrayObject *)o2) ||
(optimize_fpexps && PyArray_ISFLOAT((PyArrayObject *)o2))))) ||
PyArray_IsScalar(o2, Integer) ||
(optimize_fpexps && PyArray_IsScalar(o2, Floating))) {
temp = Py_TYPE(o2)->tp_as_number->nb_float(o2);
if (temp != NULL) {
if (PyArray_Check(o2)) {
if ((PyArray_NDIM(o2) == 0) &&
((PyArray_ISINTEGER((PyArrayObject *)o2) ||
(optimize_fpexps && PyArray_ISFLOAT((PyArrayObject *)o2))))) {
temp = Py_TYPE(o2)->tp_as_number->nb_float(o2);
if (temp == NULL) {
return NPY_NOSCALAR;
}
*out_exponent = PyFloat_AsDouble(o2);
Py_DECREF(temp);
if (PyArray_IsZeroDim(o2)) {
if (PyArray_ISINTEGER((PyArrayObject *)o2)) {
return NPY_INTPOS_SCALAR;
}
else { /* ISFLOAT */
return NPY_FLOAT_SCALAR;
}
if (PyArray_ISINTEGER((PyArrayObject *)o2)) {
return NPY_INTPOS_SCALAR;
}
else if PyArray_IsScalar(o2, Integer) {
return NPY_INTPOS_SCALAR;
}
else { /* IsScalar(o2, Floating) */
else { /* ISFLOAT */
return NPY_FLOAT_SCALAR;
}
}
}
if (PyIndex_Check(o2)) {
else if (PyArray_IsScalar(o2, Integer) ||
(optimize_fpexps && PyArray_IsScalar(o2, Floating))) {
temp = Py_TYPE(o2)->tp_as_number->nb_float(o2);
if (temp == NULL) {
return NPY_NOSCALAR;
}
*out_exponent = PyFloat_AsDouble(o2);
Py_DECREF(temp);

if (PyArray_IsScalar(o2, Integer)) {
return NPY_INTPOS_SCALAR;
}
else { /* IsScalar(o2, Floating) */
return NPY_FLOAT_SCALAR;
}
}
else if (PyIndex_Check(o2)) {
PyObject* value = PyNumber_Index(o2);
Py_ssize_t val;
if (value==NULL) {
Expand Down
5 changes: 5 additions & 0 deletions numpy/core/tests/test_umath.py
Expand Up @@ -173,6 +173,11 @@ def test_fast_power(self):
x = np.array([1, 2, 3], np.int16)
assert_((x**2.00001).dtype is (x**2.0).dtype)

# Check that the fast path ignores 1-element not 0-d arrays
res = x ** np.array([[[2]]])
assert_equal(res.shape, (1, 1, 3))


class TestLog2(TestCase):
def test_log2_values(self) :
x = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
Expand Down

0 comments on commit 27f9354

Please sign in to comment.