0
0
NumpyComparisonBeginner · 3 min read

Dot vs Matmul in NumPy: Key Differences and Usage

In NumPy, 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.

Featurenumpy.dotnumpy.matmul
Primary useDot product, inner product, matrix multiplicationMatrix multiplication only
Supports 1D arraysYes (inner product)Yes (vector-matrix multiplication)
Supports 2D arraysYes (matrix multiplication)Yes (matrix multiplication)
Supports ND arraysYes, but behaves like sum-product over last and second-last axesYes, follows strict matrix multiplication rules with broadcasting
Broadcasting supportNoYes
Operator overloadNoYes (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.

python
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)
Output
[[19 22] [43 50]]
↔️

Matmul Equivalent

The equivalent matrix multiplication using numpy.matmul or the @ operator.

python
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)
Output
[[19 22] [43 50]] [[19 22] [43 50]]
🎯

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.
Use matmul for batch matrix multiplication and when using the @ operator.
dot behaves differently on higher-dimensional arrays compared to matmul.
For simple 2D matrix multiplication, both produce the same result.