Beginners can have knowledge on how to multiply two or more numbers process pretty well but trying to code on How to multiply matrices in Python is a little complicated. But, we are here to make the process simple yet faster by explaining the different methods to multiply two matrices using python. Also, you can check out the process of multiplication matrices in NumPy with an example program from this ultimate Multiply Matrices Python Tutorial.
The Tutorial of Multiple Matrices in Python includes the following topics:
- How to Multiply Matrices in Python?
- Methods to Multiply Two Matrices in Python
- 1.Using explicit for loops
- 2. Using NumPy
- How to Multiply Matrices in NumPy?
- Python Program to Multiply Matrices in NumPy
A matrix, as you may know, is basically just a nested list, or a number of lists inside of another list. When working with a matrix, each individual list inside the main list can be considered a row, and each value within a row can be considered a column. Here’s what a matrix might look like:
x = [[4, 3], [88, 7], [56, 31]]
The matrix in the example above has three rows, each with two columns.
If you want to try to multiply two matrices (x and y) by each other, you’ll need to make sure that the number of columns in x is equal to the number of rows in y, otherwise, the equation won’t work properly. For the sake of this tutorial, let’s multiply two matrices by each other that each has three rows, with three columns in each — so 3×3 matrices.
Also Check: Quick Tip Transpose Matrix Using Python
Keep in mind that when you multiply two matrices by each other, the resulting matrix will have as many columns as the biggest matrix in you’re equation, so for example, if you’re multiplying a 3×3 by a 3×4, the resulting matrix will be a 3×4.
For the purposes of this tutorial, we’ll be multiplying a 3×3 by a 3×3. Let’s take a look at the example below to see how it works:
X = [[34,1,77], [2,14,8], [3 ,17,11]] Y = [[6,8,1], [9,27,5], [2,43,31]] result = [[0,0,0], [0,0,0], [0,0,0]] for i in range(len(X)): for j in range(len(Y)): for k in range(len(Y)): result[i][j] += X[i][k] * Y[k][j] for r in result: print(r)
In the example above, we first have to define our matrices. Then we need to define a result matrix that will represent the matrix that holds the answers to our equations. Because our two matrices are 3×3, our result matrix is 3×3 also.
Next, we iterate through the rows of the x matrix, then the columns of the y matrix (this is done using y), and finally through the rows of the y matrix. Then the arithmetic is performed.
The output of the example above would be as follows:
r =[[367, 3610, 2428], [154, 738, 320], [193, 956, 429]]
This basic information should be enough to get you started on multiplying matrices on your own. Once you’ve mastered multiplying like matrices, be sure to challenge yourself and try multiplying ones that don’t have an equal number of columns and rows.
# input two matrices of size n x m matrix1 = [[12,7,3], [4 ,5,6], [7 ,8,9]] matrix2 = [[5,8,1], [6,7,3], [4,5,9]] res = [[0 for x in range(3)] for y in range(3)] # explicit for loops for i in range(len(matrix1)): for j in range(len(matrix2)): for k in range(len(matrix2)): # resulted matrix res[i][j] += matrix1[i][k] * matrix2[k][j] print (res)
[[114 160 60] [ 74 97 73] [119 157 112]]
2. Using NumPy: Multiplication of matrices using Numpy also called vectorization. The main objective is to reduce or eliminate the explicit use of For loops in the program by which computation becomes quicker. For multiply matrices operations, we use the numpy python package which is 1000 times faster than the iterative one method.
# We need install numpy in order to import it import numpy as np # input two matrices mat1 = ([1, 6, 5],[3 ,4, 8],[2, 12, 3]) mat2 = ([3, 4, 6],[5, 6, 7],[6,56, 7]) # This will return dot product res = np.dot(mat1,mat2) # print resulted matrix print(res)
[[ 63 320 83] [ 77 484 102] [ 84 248 117]]
To multiply two matrices in python, we use the dot() function of NumPy. You need to give only two 2 arguments and it returns the product of two matrices.
The general syntax is:
where x and y are two matrices of size a * M and M * b, respectively.
import numpy as np # two dimensional arrays m1 = np.array([[1,4,7],[2,5,8]]) m2 = np.array([[1,4],[2,5],[3,6]]) m3 = np.dot(m1,m2) print(m3) # three dimensional arrays m1 = ([1, 6, 5],[3 ,4, 8],[2, 12, 3]) m2 = ([3, 4, 6],[5, 6, 7],[6,56, 7]) m3 = np.dot(m1,m2) print(m3)
[[30 66] [36 81]] [[ 63 320 83] [ 77 484 102] [ 84 248 117]]