You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
matmul @ of boolean arrays gives inconsistent results #14439
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:
importnumpyasnpA=M=np.array([[ True, False],[True,True]])
i=0while (A==M).all():
i+=1print(i, "A == M")
A=A @ Aprint(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]
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))
Activity
WarrenWeckesser commentedon 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:seberg commentedon Sep 8, 2019
Hmmm, this seems like a bug. Matmul should probably conform with
dot, or possibly upcast tolongorintp.charris commentedon 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.