8000 matmul @ of boolean arrays gives inconsistent results · Issue #14439 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

matmul @ of boolean arrays gives inconsistent results #14439

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
winsm opened this issue Sep 6, 2019 · 3 comments · Fixed by #14464
Closed

matmul @ of boolean arrays gives inconsistent results #14439

winsm opened this issue Sep 6, 2019 · 3 comments · Fixed by #14464

Comments

@winsm
Copy link
winsm commented Sep 6, 2019

Since the matrix A is its own square, the loop below should be infinite, but it terminates after 8 iterations. If A = A.dot(A) is used in place of A = A @ A, the loop is infinite as expected.

Reproducing code example:

import numpy as np

A = M = np.array([[ True, False],[True,True]])
i = 0
while (A == M).all():
    i += 1
    print(i, "A == M")
    A = A @ A
print(A)

# 1 A == M
# 2 A == M
# 3 A == M
# 4 A == M
# 5 A == M
# 6 A == M
# 7 A == M
# 8 A == M
# [[ True False]
#  [False  True]]

Numpy/Python version information:

1.16.0rc1 3.7.2 (default, Jan 3 2019, 02:55:40)
[GCC 8.2.0]

@WarrenWeckesser
Copy link
Member
WarrenWeckesser commented Sep 6, 2019

It looks like the multiplication is being performed with the underlying integer data (i.e. False = 0, True = 1), and the resulting underlying values are not converted back to 0 and 1 afterwards. Eventually an underlying integer value overflows. This snippets prints A.view(np.int8) to show what is going on with the underlying data:

A = M = np.array([[ True, False],[True,True]])
i = 0
while (A == M).all():
    i += 1
    A = A @ A
    print("A:\n", A.view(np.int8))
A:
 [[1 0]
 [2 1]]
A:
 [[1 0]
 [4 1]]
A:
 [[1 0]
 [8 1]]
A:
 [[ 1  0]
 [16  1]]
A:
 [[ 1  0]
 [32  1]]
A:
 [[ 1  0]
 [64  1]]
A:
 [[   1    0]
 [-128    1]]
A:
 [[1 0]
 [0 1]]

@seberg
Copy link
Member
seberg commented Sep 8, 2019

Hmmm, this seems like a bug. Matmul should probably conform with dot, or possibly upcast to long or intp.

@charris
Copy link
Member
charris commented Sep 8, 2019

The dot behavior was discussed long ago, turned out that boolean matrix multiplication was useful for some subjects, so we should keep it.

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Sep 8, 2019
@charris charris added this to the 1.16.6 milestone Sep 8, 2019
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jul 19, 2020
@charris charris removed this from the 1.16.6 milestone Jul 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
0