John Farrelly
John Farrelly

Reputation: 7459

numpy.matmul giving different answers than @

I was converting some pre python-3.5 code, and decided to use @ instead of numpy.matmul for readability. However, I noticed that they don't always give the same answer. Here's an example:

A = np.array([[-5.30378554e-01, 5.48418433e-01, -6.46479552e-01],
              [-7.72981719e-01, 3.13526179e-04, 6.34428218e-01],
              [3.48134817e-01, 8.36203996e-01, 4.23751136e-01]])
B = np.array([[0.84927215, 0., 0.],
              [0., 0.52771296, 0.],
              [0., 0., 0., ]])
C = np.array([[0.59677163, -0.05442458, 0.80056329],
              [0.38445308, 0.89512016, -0.22573375],
              [0.70431488, -0.44249052, -0.55510602]])
foo = A @ B @ C
bar = np.matmul(A, np.matmul(B, C))
print(foo)
print(bar)
print(np.array_equal(foo, bar))
print(foo - bar)

The output of this code is:

[[-0.15754366  0.28356928 -0.42593136]
 [-0.39170017  0.0358763  -0.52558461]
 [ 0.34609202  0.37890353  0.13708469]]
[[-0.15754366  0.28356928 -0.42593136]
 [-0.39170017  0.0358763  -0.52558461]
 [ 0.34609202  0.37890353  0.13708469]]
False
[[ 0.00000000e+00  0.00000000e+00  0.00000000e+00]
 [ 0.00000000e+00  6.93889390e-18 -1.11022302e-16]
 [ 0.00000000e+00  0.00000000e+00  0.00000000e+00]]

You can see that there is a small difference between the calculations. I had expected that using @ was an exact replacement for numpy.matmul, but given the above there must be some difference between them.

Upvotes: 0

Views: 119

Answers (1)

nonin
nonin

Reputation: 724

According to PEP 465, @ is left-associative. Therefore A @ B @ C is equivalent to np.matmul(np.matmul(A, B), C) and not to np.matmul(A, np.matmul(B, C)).

Upvotes: 2

Related Questions