Skip to content

Commit 223ec73

Browse files
committed
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 f2fc804 commit 223ec73

3 files changed

Lines changed: 62 additions & 12 deletions

File tree

‎doc/release/1.12.0-notes.rst‎

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

84+
Scalar-Scalar power result type changed
85+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86+
Raising an integer scalar to a negative integer power will always result in a
87+
floating point scalar type. Previously when using mixed integer types an
88+
integer type would be produced, while the pure type case produced a floating
89+
point type.
90+
8491
FutureWarning to changed behavior
8592
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8693

@@ -175,6 +182,11 @@ The standard ``np.load``, ``np.save``, ``np.loadtxt``, ``np.savez``, and similar
175182
functions can now take ``pathlib.Path`` objects as an argument instead of a
176183
filename or open file object.
177184

185+
Mixed type scalar-scalar operators should be faster
186+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187+
Rather than defering to the ufunc, mixed type scalar-scalar operators are
188+
handled in the scalar operator resulting in a speedup.
189+
178190

179191
Changes
180192
=======

‎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
@@ -156,6 +156,19 @@ def test_mixed_types(self):
156156
else:
157157
assert_almost_equal(result, 9, err_msg=msg)
158158

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

160173
class TestModulus(TestCase):
161174

0 commit comments

Comments
 (0)