Skip to content

Commit af692ce

Browse files
ewmoorecharris
authored andcommitted
ENH/BUG: try hard not to fall back to a ufunc call for scalar ops
When evaluating np.type1(2) op np.type2(4) don't fall back to calling the op ufunc if the output type is neither of np.type1 or np.type2. Defer the op call to that of the correct output type. This speeds up things like F * d -> D by about 5x and with the previous changes to the safely castable case, causes the scalar power operators to always return a floating point type when raising integer types to negative powers. Fixes numpygh-7449.
1 parent a8f6307 commit af692ce

3 files changed

Lines changed: 63 additions & 12 deletions

File tree

‎doc/release/1.12.0-notes.rst‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ The following functions are changed: ``sum``, ``product``,
119119
The previous identity was 1, it is now -1. See entry in `Improvements`_ for
120120
more explanation.
121121

122+
Scalar-Scalar power result type changed
123+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124+
Raising an integer scalar to a negative integer power will always result in a
125+
floating point scalar type. Previously when using mixed integer types an
126+
integer type would be produced, while the pure type case produced a floating
127+
point type.
128+
122129
FutureWarning to changed behavior
123130
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124131

@@ -328,6 +335,12 @@ an intermediate array to reduce this scaling to ``N^3`` or effectively
328335
been applied to the general einsum summation notation. See ``np.einsum_path``
329336
for more details.
330337

338+
Mixed type scalar-scalar operators should be faster
339+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
340+
Rather than defering to the ufunc, mixed type scalar-scalar operators are
341+
handled in the scalar operator resulting in a speedup.
342+
343+
331344
Changes
332345
=======
333346

‎numpy/core/src/umath/scalarmath.c.src‎

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -689,18 +689,31 @@ _@name@_convert2_to_ctypes(PyObject *a, @type@ *arg1,
689689
return ret_b;
690690
}
691691
else {
692-
/* couldn't cast b to @name@. Since converting a returned 0, it is
693-
* convertable to @name@. See if instead we can convert it to b.
694-
* then defer the operator call to that type.
695-
*/
696-
PyArray_Descr *descr_a;
697-
int ret = -1;
698-
descr_a = PyArray_DescrFromTypeObject((PyObject *)Py_TYPE(a));
699-
if (PyArray_CanCastSafely(descr_a->type_num, ret_b)) {
700-
ret = ret_b;
701-
}
702-
Py_DECREF(descr_a);
703-
return ret;
692+
/* couldn't cast b to @name@ but it is a numeric scalar. Since
693+
* converting a returned 0, it is convertable to @name@. See if
694+
* instead we can convert it to b then defer the operator call to
695+
* that type.
696+
*/
697+
PyArray_Descr *descr_a;
698+
int ret = -1;
699+
descr_a = PyArray_DescrFromTypeObject((PyObject *)Py_TYPE(a));
700+
if (PyArray_CanCastSafely(descr_a->type_num, ret_b)) {
701+
ret = ret_b;
702+
}
703+
else {
704+
/* neither of a and b can be safely converted to the type of the
705+
* other. Since they are both numeric scalars, use the type
706+
* promotion table to find the correct type to use.
707+
*/
708+
PyArray_Descr *descr_b, *descr_result;
709+
descr_b = PyArray_DescrFromType(ret_b);
710+
descr_result = PyArray_PromoteTypes(descr_a, descr_b);
711+
ret = descr_result->type_num;
712+
Py_DECREF(descr_b);
713+
Py_DECREF(descr_result);
714+
}
715+
Py_DECREF(descr_a);
716+
return ret;
704717
}
705718
}
706719
else {
@@ -716,6 +729,18 @@ _@name@_convert2_to_ctypes(PyObject *a, @type@ *arg1,
716729
if (PyArray_CanCastSafely(ret_a, descr_b->type_num)) {
717730
ret = descr_b->type_num;
718731
}
732+
else {
733+
/* neither of a and b can be safely converted to the type of the
734+
* other. Since they are both numeric scalars, use the type
735+
* promotion table to find the correct type to use.
736+
*/
737+
PyArray_Descr *descr_a, *descr_result;
738+
descr_a = PyArray_DescrFromType(ret_a);
739+
descr_result = PyArray_PromoteTypes(descr_a, descr_b);
740+
ret = descr_result->type_num;
741+
Py_DECREF(descr_a);
742+
Py_DECREF(descr_result);
743+
}
719744
Py_DECREF(descr_b);
720745
}
721746
return ret;

‎numpy/core/tests/test_scalarmath.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ def test_mixed_types(self):
158158
else:
159159
assert_almost_equal(result, 9, err_msg=msg)
160160

161+
def test_mixed_integer_types_negative_power(self):
162+
# should always result in a floating type
163+
signed_int_types = [np.int8, np.int16, np.int32, np.int64, int]
164+
all_int_types = signed_int_types + [np.uint8, np.uint16, np.uint32, np.uint64]
165+
for base_type in all_int_types:
166+
for exponent_type in signed_int_types:
167+
result = base_type(4) ** exponent_type(-4)
168+
msg = "4 ** -4 == {} using {} ** {} -> {}".format(result,
169+
base_type.__name__, exponent_type.__name__,
170+
result.__class__.__name__)
171+
assert_(isinstance(result, (np.floating, float)), msg)
172+
assert_almost_equal(result, 0.00390625)
173+
161174

162175
class TestModulus(TestCase):
163176

0 commit comments

Comments
 (0)