NumPy matmul – Matrix Product of Two Arrays

NumPy Matmul Cover Image

Hello and welcome to this tutorial on Numpy matmul. In this tutorial, we will be learning about the NumPy matmul() method and also seeing a lot of examples regarding the same. So let us begin!


What is NumPy matmul?

The matmul() method in NumPy returns the matrix product of two arrays. Here, the input arguments have to be arrays only, no scalar values are allowed. The input be can be 1-d arrays, 2-d arrays or a combination or both, or n-dimensional arrays as well.

We will see the examples for each of these in the upcoming sections of this tutorial.


Syntax of NumPy matmul

Let us have a look at the syntax of the matmul function.

numpy.matmul(x1, x2, out=None)
ParameterDescriptionRequired/Optional
x1Input array 1.Required
x2Input array 2.Required
outAn alternative output array in which to place the result. It must have the same shape as the expected output.Optional

If x1 is a n x m matrix and x2 is m x l matrix, then the resulting matrix after multiplication will be an n x l matrix.

Returns:
The matrix product of x1 nad x2. If x1 and x2 are both 1-d arrays, then the result will be a scalar value.

Raises:
If the last dimension of x1 does not match the second to last dimension of x2 or if a scalar value is passed as an argument.


Examples of using NumPy matmul

Let’s now look at a few examples to understand the function better.

Using NumPy matmul when both inputs are 1-d arrays

import numpy as np

a = [1, 5, 3]
b = [10, 2, 4]
# using matmul method to compute the matrix product
ans = np.matmul(a, b)
print("a =", a, "\nb =", b)
print("Result =", ans)

Output:

a = [1, 5, 3] 
b = [10, 2, 4]
Result = 32

Here, since both the input arguments are 1-dimensional arrays, their matrix multiplication results in a scalar value calculated as

ans = 1*10 + 5*2 + 3*4 = 10 + 10 + 12 = 32 

When both the inputs are 2-d arrays

import numpy as np

a = [[2, 6], [8, 4]]
b = [[3, 1], [5, 10]]
# using matmul method to compute the matrix product
ans = np.matmul(a, b)
print("a =", a, "\nb =", b)
print("Result =\n", ans)

Output:

a = [[2, 6], [8, 4]] 
b = [[3, 1], [5, 10]]
Result =
 [[36 62]
 [44 48]]

Since both the inputs are 2×2 matrices, the result is also a 2×2 matrix. The matrix multiplication is calculated as

ans[0][0] = a[0][0]*b[0][0] + a[0][1]*b[1][0] = 2*3 + 6*5 = 6 + 30 = 36
ans[0][1] = a[0][0]*b[0][1] + a[0][1]*b[1][1] = 2*1 + 6*10 = 2 + 60 = 62
ans[1][0] = a[1][0]*b[0][0] + a[1][1]*b[1][0] = 8*3 + 4*5 = 24 + 20 = 44
ans[1][1] = a[1][0]*b[0][1] + a[1][1]*b[1][1] = 8*1 + 4*10 = 8 + 40 = 48

Using NumPy matmul when one input is a 1-d array and the other a 2-d array

import numpy as np

a = [10, 20]
b = [[8, 9], [3, 1]]
# using matmul method to compute the matrix product
ans = np.matmul(a, b)
print("a =", a, "\nb =", b)
print("Matrix product of a and b =", ans)

Output:

a = [10, 20] 
b = [[8, 9], [3, 1]]
Matrix product of a and b = [140 110]

The shape of matrix a is 1×2 and that of b is 2×2, therefore the shape of the resulting matrix is 1×2. The matrix product is computed as follows:

ans[0][0] = a[0][0]*b[0][0] + a[0][1]*b[0][1] = 10*8 + 20*3 = 80 + 60 = 140
ans[0][1] = a[0][0]*b[1][0] + a[0][1]*b[1][1] = 10*9 + 20*1 = 90 + 20 = 110 

We can also reverse the order of matrices in the matmul function as below:

import numpy as np

a = [10, 20]
b = [[8, 9], [3, 1]]
# using matmul method to compute the matrix product
ans = np.matmul(b, a)
print("a =", a, "\nb =", b)
print("Matrix product of b and a =", ans)

Output:

a = [10, 20] 
b = [[8, 9], [3, 1]]

Here, the output is computed as:

ans[0][0] = b[0][0]*a[0][0] + b[1][0]*a[0][1] = 8*10 + 9*20 = 80 + 180 = 260
ans[0][1] = b[0][1]*a[0][0] + b[1][1]*a[0][1] = 3*10 + 1*20 = 30 + 20 = 50 

Conclusion

That’s all! In this tutorial, we learned about the Numpy matmul method and practiced different types of examples using the same.  If you want to learn more about NumPy, feel free to go through our NumPy tutorials.


Reference