Matrix multiplication (@)
The @ operator multiplies matrices the proper linear-algebra way.
In this page:
Syntax
result = a @ b
result = np.matmul(a, b)
Matrix multiplication (@)
Introduced in Python 3.5, @ maps to np.matmul. For 2-D arrays it is the standard row-by-column product. For stacks of matrices it broadcasts over the leading dimensions.
Note:
A @ B is clearer than np.dot(A, B) when chaining several products.
Example: Matrix multiplication (@)
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[0, 1], [1, 0]])
print(A @ B)
print(B @ A)
print(A * B)
# Output:
# [[2 1]
# [4 3]]
# [[3 4]
# [1 2]]
# [[0 2]
# [3 0]]
Related Topics
Common Mistakes
- Using * instead of @
- Mismatched inner dimensions
- Assuming A @ B equals B @ A
Chapter Summary
- @ is matrix multiplication
- * is element-wise
- (m, n) @ (n, p) gives (m, p)
- Not commutative
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: