In this article, we are going to learn about the differences between numpy.dot product and numpy.matmul matrix product.These two functions are very helpful and powerful when it comes to array multiplication and dot product operations. If you look at both these functions then Numpy dot and Numpy matmul are similar, but they behave differently when we test them with the types of input we pass to them as arguments.Let us check the differences one by one:
numpy.dot vs numpy.matmul
- Scalar arguments multiplication
- Multiplication of higher dimensional arrays
1.Scalar arguments multiplication
1.1numpy.dot() with Scalar agruments multiplication
The numpy.dot() function works perfectly fine when it comes to multiplying scalars. In this example, we are just doing the dot product of a scaler number with another scaler number which will work as a simple multiplication of two numbers.
import numpy as np
num1 = 5
num2 = 4
product = np.dot(num1, num2)
print ("Multiplication Result is : ", product)
Output :
Multiplication Result is : 20
1.2 numpy.matmul() with Scalar arguments multiplication error
But when we try the same code with matmul() function to multiply two scalars it does not work.In this example, we are trying to use matmul() function to multiply a scaler number with a linear array.As per the documentation of matmul() this will not work. You can see below the code example we are getting an error.Scaler multiplications are not allowed using matmul() function.
import numpy as np
num1 = 5
arr1 = np.array([1, 2, 3] )
product = np.matmul(num1, arr1)
print ("Multiplication Result is : ", product)
Output :
Traceback (most recent call last):
File "C:/Users/Contract/Desktop/Programs/NumpyTest.py", line 5, in
product = np.matmul(num1, arr1)
ValueError: matmul: Input operand 0 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)
2.Multiplication of higher dimensional arrays
The second difference that we see between matmul() and dot() function is they behave differently when we pass arguments that are different in size.
2.1 numpy.dot():Multiplication of higher dimensional arrays
If arr1 is an N-D array and arr2 is a 1-D array, it is a sum-product over the last axis of arr1 and arr2.
import numpy as np
arr1 = np.array([[1, 2, 3], [4, 5, 6],[7, 8, 9]])
arr2 = np.array([1, 2, 3] )
print ("1st Input array : ", arr1)
print ("2nd Input array : ", arr2)
product = np.dot(arr1, arr2)
print ("Dot product Result of 2D arrays is : ", product)
Output:
1st Input array : [[1 2 3]
[4 5 6]
[7 8 9]]
2nd Input array : [1 2 3]
Dot product Result of 2D arrays is : [14 32 50]
If arr1 is an N-D array and arr2 is an M-D array (where M>=2), it is a sum-product over the last axis of arr1 and the second-to-last axis of arr2.
import numpy as np
arr1 = np.array([[1, 2, 3],[4, 5, 6]] )
arr2 = np.array([[1, 2, 3], [4, 5, 6],[7, 8, 9]])
print ("1st Input array : ", arr1)
print ("2nd Input array : ", arr2)
product = np.dot(arr1, arr2)
print ("Dot product Result of 2D arrays is : ", product)
Output:
1st Input array : [[1 2 3]
[4 5 6]]
2nd Input array : [[1 2 3]
[4 5 6]
[7 8 9]]
Dot product Result of 2D arrays is : [[30 36 42]
[66 81 96]]
2.2 numpy.matmul():Multiplication of higher dimensional arrays
Now if we see the behavior of matmul() function:If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.
import numpy as np
arr1 = np.array([[1, 2, 3], [4, 5, 6]] )
arr2 = np.array([[1, 2, 3], [4, 5, 6],[7, 8, 9]])
print ("1st Input array : ", arr1)
print ("2nd Input array : ", arr2)
product = np.matmul(arr1, arr2)
print ("Matmul product Result of 2D arrays is : ", product)
Output:
1st Input array : [[1 2 3]
[4 5 6]]
2nd Input array : [[1 2 3]
[4 5 6]
[7 8 9]]
Matmul product Result of 2D arrays is : [[30 36 42]
[66 81 96]]