Python program to multiply two matrices
In this tutorial, you will learn to multiply two matrices in Python. A matrix is a two-dimensional data structure where numbers are arranged into rows and columns. Python does not have a built-in type for matrices but we can treat a nested list or list of a list as a matrix. The List is an ordered set of elements enclosed in square brackets [ ]. Each element in the list will be then treated as a row of a matrix.
For example, A= [ [1, 2], [3, 4]] is a 2x2 matrix. First row can be selected as A[0] and the element in first row, first column can be selected as A[0][0].
Multiplication of two matrices is possible only if the number of columns in the first matrix is equal to the number of rows in the second matrix.
If m and n are the rows and columns of matrix A, p and q are the rows and columns of matrix B then, multiplication will be possible if, n=p and resultant matrix will be,
Input:
A=[ [1,2], [3,4] ]
B=[ [1,3], [2,5] ]
Output: [ [5, 13], [11, 29] ]
To multiply two matrices in Python, we can follow these approaches:
- Using nested loops
- Using nested list comprehension
- Using numpy module
Approach 1: nested loops
For this approach, we will use nested loops which are simply a loop within a loop, to multiply the matrices and store them in a resultant matrix. We will use three loops, the first loop will be for iterating through rows of matrix A and the second loop will be for iterating through the columns of matrix A and the third loop will iterate the rows of matrix B.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define a function that will multiply two matrixes
Step 2- In the function, declare a list that will store the result list
Step 3- Iterate through the rows and columns of matrix A and the row of matrix B
Step 4- Multiply the elements in the two matrices and store them in the result list
Step 5- Print the resultant list
Step 6- Declare and set values for two matrices
Step 7- Call the function, the result will be printed
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach. This program will work for a 3x3 matrix.
def Multiply(A,B):
result=[ [0,0,0],[0,0,0],[0,0,0] ]
#for rows
for i in range(len(A)):
#for columns
for j in range(len(B[0])):
#for rows of matrix B
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
for p in result:
print(p)
return 0
A = [ [1, 2, 3],[6, 7, 4], [8, 10, 11] ]
B = [[1, 5, 3],[2, 6, 5], [7, 4, 9] ]
print("Result: ")
Multiply(A,B)
Result:
[26, 29, 40]
[48, 88, 89]
[105, 144, 173]
Approach 2: nested list comprehension
List comprehension is a shorter syntax for creating a new list based on the values of an existing list. We will store the product of the two matrices and store it in a new list. We will use nested list comprehension to iterate through each element in the matrix. List comprehension allows us to write concise codes in Python.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define a function that will add two matrixes
Step 2- In the function declare a list that will store the result
Step 3- Through list comprehension multiply the corresponding elements and store them in the result list
Step 4- Print the resultant list
Step 5- Declare and set values for two matrices
Step 6- Call the function, the result will be printed
Python Program 2
Look at the program to understand the implementation of the above-mentioned approach. This program will work for a 3x3 matrix.
def Multiply(X,Y):
result=[ [0,0,0],[0,0,0],[0,0,0] ]
#list comprehension
result= [[sum(a*b for a,b in zip(X_row,Y_col)) for Y_col in zip(*Y)] for X_row in X]
for k in result:
print(k)
return 0
A = [ [6, 7, 2],[3, 5, 4], [1, 2, 3] ]
B = [[1, 5],[2, 5],[6, 3]]
print("Result: ")
Multiply(A,B)
Result:
[32, 71]
[37, 52]
[23, 24]
Approach 3: using NumPy module
Numpy module is a python package for the computation and processing of the multidimensional and single-dimensional list elements. The dot() function in this module calculates the dot product of the matrices.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Import NumPy module
Step 2- Declare and set values for two matrices
Step 3- Declare result list
Step 4- Use the dot() function to find a product of the matrix
Step 6- Store the product in the result
Step 7- Print the resultant list
Python Program 3
Look at the program to understand the implementation of the above-mentioned approach.
import numpy as np
A = [[12, 7, 3], [4, 5, 6], [7, 8, 9]]
B = [[5, 8, 1, 2], [6, 7, 3, 0], [4, 5, 9, 1]]
result= [[0,0,0,0], [0,0,0,0], [0,0,0,0]]
result= np.dot(A,B)
for p in result:
print(p)
Conclusion
In this tutorial, we have learned three ways for multiplying the two matrices in Python. We have learned how to use nested loops, list comprehension, and the NumPy module in Python.