8000 MAINT, ENH: Refactor numpy ** operators for numpy scalar integer powers by charris · Pull Request #8221 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT, ENH: Refactor numpy ** operators for numpy scalar integer powers #8221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
MAINT: Make numpy integers to negative numpy integer raise.
This change is the upshot of a long discussion on the numpy mailing list
where is was decided that numpy scalar integers should raise a
ValueError when raised to a negative integer power. This behavior
differs from the Python case where the result is of float type, but
because numpy arrays cannot contain value dependent types and the desire
was to be consistent with numpy scalar arrays, we diverge from Python in
this regard. Previously numpy arrays would return zero in this case, but
because that is not a correct value they now also raise a ValueError.
  • Loading branch information
charris committed Oct 28, 2016
commit b8904aaf3cec091bee02544f48ba8ca55749ea9e
28 changes: 28 additions & 0 deletions numpy/core/src/umath/scalarmath.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,33 @@ static PyObject *
* #one = 1*10, NPY_HALF_ONE, 1*6#
*/

static PyObject *
@name@_power(PyObject *a, PyObject *b, PyObject *NPY_UNUSED(c))
{
PyObject *ret;
@type@ arg1, arg2;

switch (_@name@_convert2_to_ctypes(a, &arg1, b, &arg2)) {
case 0:
case -1:
return PyArray_Type.tp_as_number->nb_power(a, b, NULL);
case -2:
/* use default handling */
if (PyErr_Occurred()) {
return NULL;
}
return PyGenericArrType_Type.tp_as_number->nb_power(a, b, NULL);
case -3:
/*
* special case for longdouble and clongdouble
* because they have a recursive getitem in their dtype
*/
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
}

#if 0
#if @cmplx@
static PyObject *
@name@_power(PyObject *a, PyObject *b, PyObject *NPY_UNUSED(c))
Expand Down Expand Up @@ -1183,6 +1210,7 @@ static PyObject *
return ret;
}

#endif
#endif

/**end repeat**/
Expand Down
52 changes: 35 additions & 17 deletions numpy/core/tests/test_scalarmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,41 @@ def test_large_types(self):
else:
assert_almost_equal(b, 6765201, err_msg=msg)

def test_negative_power(self):
typelist = [np.int8, np.int16, np.int32, np.int64]
for t in typelist:
a = t(2)
b = t(-4)
result = a**b
msg = ("error with %r:"
"got %r, expected %r") % (t, result, 0.0625)
assert_(result == 0.0625, msg)

c = t(4)
d = t(-15)
result = c**d
expected = 4.0**-15.0
msg = ("error with %r:"
"got %r, expected %r") % (t, result, expected)
assert_almost_equal(result, expected, err_msg=msg)
def test_integers_to_negative_integer_power(self):
# Note that the combination of uint64 with a signed integer
# has common type np.float. The other combinations should all
# raise a ValueError for integer ** negative integer.
exp = [np.array(-1, dt)[()] for dt in 'bhilq']

# 1 ** -1 possible special case
base = [np.array(1, dt)[()] for dt in 'bhilqBHILQ']
for i1, i2 in itertools.product(base, exp):
if i1.dtype.name != 'uint64':
assert_raises(ValueError, operator.pow, i1, i2)
else:
res = operator.pow(i1, i2)
assert_(res.dtype.type is np.float64)
assert_almost_equal(res, 1.)

# -1 ** -1 possible special case
base = [np.array(-1, dt)[()] for dt in 'bhilq']
for i1, i2 in itertools.product(base, exp):
if i1.dtype.name != 'uint64':
assert_raises(ValueError, operator.pow, i1, i2)
else:
res = operator.pow(i1, i2)
assert_(res.dtype.type is np.float64)
assert_almost_equal(res, -1.)

# 2 ** -1 perhaps generic
base = [np.array(2, dt)[()] for dt in 'bhilqBHILQ']
for i1, i2 in itertools.product(base, exp):
if i1.dtype.name != 'uint64':
assert_raises(ValueError, operator.pow, i1, i2)
else:
res = operator.pow(i1, i2)
assert_(res.dtype.type is np.float64)
assert_almost_equal(res, .5)

def test_mixed_types(self):
typelist = [np.int8, np.int16, np.float16,
Expand Down
0