Python program to multiply two matrices
In this post, you will learn different ways to perform matrix multiplication using the 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 a matrix to process linear transformations to render images. In robotics and automation, matrices are the fundamental building blocks for robot development. The contributions for controlling robots are acquired based on the calculations from matrices.
The different operations on the matrix are also very important for us. Let us discuss different ways to perform matrix multiplication.
Matrix multiplication Python using Simple Nested Loop
A nested loop is a loop inside a loop. In the given Python program, we have used the nested for loops to iterate through each row and each column and perform multiplication on them, and store the multiplication result in the third matrix.
# 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]
Matrix multiplication Python using List Comprehension
Nested List Comprehensions are nothing but a list comprehension within another list comprehension. It is quite similar to a 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 a list of tuples. Furthermore, we have unpacked the argument list using the * 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
Inverse of a matrix in Python
Transpose of a matrix in python
Matrix addition in Python
Python Numpy Array Shape
Python Contour Plot Examples
Geometric Transformation OpenCV Python
Python weather api
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