Python program to multiply two matrices
In this post, you will learn different ways to perform matrix multiplication using Python programming language.
Matrix, a set of numbers arranged in rows and columns so as to form a rectangular array. Matrices are utilized substantially more in everyday life than individuals would have suspected. A square matrix can represent a linear transformation of a geometric object. A real life example is Adobe Photoshop, it uses matrix to process linear transformations to render images. In robotics and automation, matrices are the fundamental segments for the robot developments. The contributions for controlling robots are acquired depending on the calculations from matrices.
The different operations on matrix are also very important for us. Let us discuss different ways to perform matrix multiplication -
Using Simple Nested Loop
A nested loop is a loop inside a loop. In the following program, we have used the nested for loops to iterate through each row and each column.
# Program to multiply two matrices
X = [[2, 5, 1],
[9, 2, 8],
[2, 5, 3]]
# take a 3x4 matrix
Y = [[3, 1, 9, 2],
[5, 1, 8, 2],
[5, 1, 2, 9]]
result = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
# iterating by row of X
for i in range(len(X)):
# iterating by coloum by Y
for j in range(len(Y[0])):
# iterating by rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)
Output of the above code -
[36, 8, 60, 23]
[77, 19, 113, 94]
[46, 10, 64, 41]
Using List Comprehension
Nested List Comprehensions are nothing but a list comprehension within another list comprehension. It is quite similar to nested loop. It uses the zip() function to make an iterator that will aggregate elements from at least two iterables. It accepts several iterable objects and returns the list of tuples. Further, we have unpacked the argument list using * operator. The following program uses a nested list comprehension to perform matrix multiplication-
# Program to multiply two matrices
X = [[4, 1, 8],
[7, 8, 3],
[1, 4, 9]]
# take a 3x4 matrix
Y = [[2, 8, 5, 3],
[6, 7, 1, 9],
[6, 2, 4, 8]]
# result will be 3x4
result = [[sum(X * Y for X, Y in zip(A_row, B_col))
for B_col in zip(*Y)]
for A_row in X]
for r in result:
print(r)
Output of the above code -
[62, 55, 53, 85]
[80, 118, 55, 117]
[80, 54, 45, 111]
Using Vectorized implementation
Python provides a powerful numpy.dot() function for matrix computation. Here, we got the dot product of two matrices. It is equivalent to matrix multiplication.
import numpy as np
# take a 3x3 matrix
X = [[4, 1, 6],
[7, 8, 2],
[5, 2, 8]]
# take a 3x4 matrix
Y = [[1, 4, 2, 7],
[7, 5, 9, 2],
[2, 8, 1, 1]]
# result will be 3x4
result= [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
result = np.dot(X,Y)
for r in result:
print(r)
Output of the above code -
[23 69 23 36]
[67 84 88 67]
[35 94 36 47]
Related Articles
Convert list to dictionary Python
Convert array to list Python
numpy dot product
glob in Python
Python heap implementation
zip function in Python
Remove last element from list Python
Check if list is empty Python
Remove element from list Python
Python split multiple delimiters
Python loop through list
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword