8000 BUG: add a specialized loop for boolean matmul by mattip · Pull Request #14464 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: add a specialized loop for boolean matmul #14464

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

Merged
merged 3 commits into from
Sep 12, 2019
Merged
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
Prev Previous commit
Next Next commit
MAINT: use tmp pointers to allow early break; add tests, release note
  • Loading branch information
mattip committed Sep 9, 2019
commit 4ee28be800ba23c2bda88b0dfa890eec9a9fc19f
6 changes: 6 additions & 0 deletions doc/release/upcoming_changes/14464.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
`numpy.matmul` with boolean output now converts to boolean values
-----------------------------------------------------------------
Calling `numpy.matmul` where the output is a boolean array would fill the array
with uint8 equivalents of the result, rather than 0/1. Now it forces the output
to 0 or 1 (``NPY_TRUE`` or ``NPY_FALSE``).

13 changes: 7 additions & 6 deletions numpy/core/src/umath/matmul.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,19 @@ BOOL_matmul_inner_noblas(void *_ip1, npy_intp is1_m, npy_intp is1_n,

for (m = 0; m < dm; m++) {
for (p = 0; p < dp; p++) {
npy_bool *ip1tmp = ip1;
npy_bool *ip2tmp = ip2;
*(npy_bool *)op = NPY_FALSE;
for (n = 0; n < dn; n++) {
npy_bool val1 = (*(npy_bool *)ip1);
npy_bool val2 = (*(npy_bool *)ip2);
npy_bool val1 = (*(npy_bool *)ip1tmp);
npy_bool val2 = (*(npy_bool *)ip2tmp);
if (val1 != 0 && val2 != 0) {
*(npy_bool *)op = NPY_TRUE;
break;
}
ip2 += is2_n;
ip1 += is1_n;
ip2tmp += is2_n;
ip1tmp += is1_n;
}
ip1 -= ib1_n;
ip2 -= ib2_n;
op += os_p;
ip2 += is2_p;
}
Expand Down
14 changes: 12 additions & 2 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -6279,10 +6279,20 @@ def __mul__(self, other):
def test_matmul_bool(self):
# gh-14439
a = np.array([[1, 0],[1, 1]], dtype=bool)
assert np.max(a.view(np.int8)) == 1
assert np.max(a.view(np.uint8)) == 1
b = np.matmul(a, a)
# matmul with boolean output should always be 0, 1
assert np.max(b.view(np.int8)) == 1
assert np.max(b.view(np.uint8)) == 1

rg = np.random.default_rng(np.random.PCG64(43))
d = rg.integers(2, size=4*5, dtype=np.int8)
d = d.reshape(4, 5) > 0
out1 = np.matmul(d, d.reshape(5, 4))
out2 = np.dot(d, d.reshape(5, 4))
assert_equal(out1, out2)

c = np.matmul(np.zeros((2, 0), dtype=bool), np.zeros(0, dtype=bool))
assert not np.any(c)


if sys.version_info[:2] >= (3, 5):
Expand Down
0