There are exactly two ways of multiplying matrices.
- To multiply a matrix with a scalar. This is known as scalar multiplication.
- To multiply a matrix with another matrix. This is known as matrix multiplication.
This example will show a matrix multiplication using numpy arrays using the numpy matmul() method. So, let’s check out that method in detail.
np.matmul
The np.matmul() method is used to find out the matrix product of two arrays. The numpy matmul() function takes arr1 and arr2 as arguments and returns the matrix product of the input arrays.
To multiply two arrays in Python, use the np.matmul() method. In the case of 2D matrices, a regular matrix product is returned. If the provided matrices are of dimensionality greater than 2, it is treated as a stack of matrices residing in the last two indexes and broadcasted accordingly.
If any of the two arrays are one-dimensional, it is promoted into the matrix by appending 1 to its dimensions. After the matrix multiplication is performed, the added 1 is removed.
Syntax
numpy.matmul(arr1, arr2, out=None)
Parameters
The matmul() function takes at most three parameters:
arr1: array_like, the first input array
arr2: array_like, the second input array
out: ndarray, it is an optional parameter.
It is an n-dimensional array into which the output must be stored. Suppose arr1 has a shape (m,k), and arr2 has a shape (k,n); they must have a shape of (m,n). If this parameter is not provided or None, a freshly-allocated array is returned.
Return Value
The matmul() method returns the matrix product of the input arrays. A scalar is produced only when both arr1 and arr2 are 1-dimensional vectors.
Programming Example
Program to show the working of numpy.matmul() method in case of a usual 2-D matrix:
See the following code.
# importing the numpy module import numpy as np # first 2-D array arr1 arr1 = np.array([[2, 4], [6, 8]]) print("first array is :") print(arr1) print("Shape of first array is: ", arr1.shape) # second 2-D array arr1 arr2 = np.array([[1, 3], [5, 7]]) print("second array is :") print(arr2) print("Shape of second array is: ", arr2.shape) # calculating matrix product res = np.matmul(arr1, arr2) print("Resultant array is :") print(res) print("Shape of resultant array is: ", res.shape)
Output
first array is : [[2 4] [6 8]] Shape of first array is: (2, 2) second array is : [[1 3] [5 7]] Shape of second array is: (2, 2) Resultant array is : [[22 34] [46 74]] Shape of resultant array is: (2, 2)
Explanation
In the above program, we have taken two two-dimensional input arrays named arr1 and arr2; we have displayed output by displaying the matrix product of both the arrays. The resultant array will also have the shape of (2,2).
Program to show the working of numpy.matmul() method in case anyone of the matrices is a 1D matrix
See the following code.
# importing the numpy module import numpy as np # first 2-D array arr1 arr1 = np.array([[3, 0], [0, 4]]) print("first array is :") print(arr1) print("Shape of first array is: ", arr1.shape) # second 2-D array arr1 arr2 = np.array([1, 2]) print("second array is :") print(arr2) print("Shape of second array is: ", arr2.shape) # calculating matrix product res = np.matmul(arr1, arr2) print("Resultant array is :") print(res) print("Shape of resultant array is: ", res.shape)
Output
first array is : [[3 0] [0 4]] Shape of first array is: (2, 2) second array is : [1 2] Shape of second array is: (2,) Resultant array is : [3 8] Shape of resultant array is: (2,)
Explanation
In the above program, we have taken a two-dimensional input array named arr1 and another 1-dimensional vector named arr2. Then we have displayed output by displaying the matrix product of both the arrays. The resultant array will also have a shape of (2, ).
For a more in-depth understanding product, it can be understood as:
Since arr2 is a 1-dimensional vector, it is promoted into a matrix by appending 1 to it, and it can be shown as: [[11], [2,1]]. Now, after the conversion matrix product is calculated as usual and the result will be obtained as [ [3,3], [8, 4] ], but the appended column will again be removed; hence the final result will be [3,8] having a shape (2,).
That’s it for this tutorial.