Skip to content

Commit

Permalink
Merge pull request #4419 from juliantaylor/scalar-0d
Browse files Browse the repository at this point in the history
backport 0d array power fix to 1.8
  • Loading branch information
juliantaylor committed Mar 2, 2014
2 parents e5139b7 + 43f7ff7 commit f816b0a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
1 change: 1 addition & 0 deletions doc/release/1.8.1-notes.rst
Expand Up @@ -46,3 +46,4 @@ Issues fixed
* gh-4408: Fix stride_stricks.as_strided function for object arrays
* gh-4225: fix log1p and exmp1 return for np.inf on windows compiler builds
* gh-4359: Fix infinite recursion in str.format of flex arrays
* gh-4145: Incorrect shape of broadcast result with the exponent operator
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 f816b0a

Please sign in to comment.