Dot vs Matmul in NumPy: Key Differences and Usage
dot and matmul both perform matrix multiplication but differ in behavior with higher-dimensional arrays. matmul follows strict matrix multiplication rules and supports broadcasting, while dot is more general and can also compute inner products and sums for 1D arrays.Quick Comparison
This table summarizes the main differences between dot and matmul in NumPy.
| Feature | numpy.dot | numpy.matmul |
|---|---|---|
| Primary use | Dot product, inner product, matrix multiplication | Matrix multiplication only |
| Supports 1D arrays | Yes (inner product) | Yes (vector-matrix multiplication) |
| Supports 2D arrays | Yes (matrix multiplication) | Yes (matrix multiplication) |
| Supports ND arrays | Yes, but behaves like sum-product over last and second-last axes | Yes, follows strict matrix multiplication rules with broadcasting |
| Broadcasting support | No | Yes |
| Operator overload | No | Yes (using @ operator) |
Key Differences
numpy.dot is a versatile function that computes the dot product of two arrays. For 1D arrays, it returns the inner product (a scalar). For 2D arrays, it performs standard matrix multiplication. For higher-dimensional arrays, it sums the product over the last axis of the first array and the second-last axis of the second array, which can lead to different results than matrix multiplication.
numpy.matmul is designed specifically for matrix multiplication. It supports 1D and 2D arrays and extends naturally to N-dimensional arrays by treating the last two dimensions as matrices and broadcasting over the others. This makes matmul more consistent for batch matrix multiplication and it also supports the @ operator for cleaner syntax.
In summary, dot is more general and can be used for inner products and sums, while matmul strictly follows matrix multiplication rules and supports broadcasting, making it better for linear algebra tasks involving batches of matrices.
Code Comparison
Here is how numpy.dot performs matrix multiplication on 2D arrays.
import numpy as np A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) result_dot = np.dot(A, B) print(result_dot)
Matmul Equivalent
The equivalent matrix multiplication using numpy.matmul or the @ operator.
import numpy as np A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) result_matmul = np.matmul(A, B) print(result_matmul) # Using @ operator result_operator = A @ B print(result_operator)
When to Use Which
Choose numpy.dot when you need a general dot product or inner product, especially with 1D arrays or when working with sums over axes in higher dimensions.
Choose numpy.matmul when you want clear, strict matrix multiplication behavior, especially with 2D or higher-dimensional arrays where broadcasting over batches is needed. Also, use matmul for cleaner syntax with the @ operator.
Key Takeaways
matmul is specialized for matrix multiplication and supports broadcasting.dot is more general and can compute inner products and sums over axes.matmul for batch matrix multiplication and when using the @ operator.dot behaves differently on higher-dimensional arrays compared to matmul.