Matrix multiplication is a computation of a matrix that involves a combination of two matrices together to create a new matrix.
Think of matrices as grids of numbers arranged in rows and columns.
When we multiply two matrices together, we take each row from the first matrix and multiply it by each column of the second matrix.
The resulting matrix has the same number of rows as the first matrix and the same number of columns as the second matrix.
Formula to multiply a matrix
The formula for matrix multiplication involves taking the dot product of each row of the first matrix with each column of the second matrix.
Let’s say we have two matrices, A and B, with dimensions m x n and n x p respectively. The resulting matrix, C, will have dimensions m x p.
To compute the elements of C, we use the following formula:
C(i,j) = Σ A(i,k) * B(k,j)
where Σ represents the summation over k from 1 to n.
Method 1: Using Nested Loops to multiply a matrix
In this program, we multiply two matrices using nested loops. It first initializes an empty matrix that will store the result of the multiplication.
For each row in the first matrix, and for each column in the second matrix, the program computes the dot product of the row and the column and saves the result in the third matrix.
# Define the two matrices
matrix1 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
matrix2 = [[9, 8, 7],
[6, 5, 4],
[3, 2, 1]]
# Define the result matrix as a zero matrix of appropriate size
result_matrix = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
# Iterate through each row of the first matrix
for i in range(len(matrix1)):
# Iterate through each column of the second matrix
for j in range(len(matrix2[0])):
# Iterate through each element of the second matrix's row
for k in range(len(matrix2)):
# Multiply the corresponding elements and accumulate the sum
result_matrix[i][j] += matrix1[i][k] * matrix2[k][j]
# Print the result matrix
for row in result_matrix:
print(row)
Output
[30, 24, 18]
[84, 69, 54]
[138, 114, 90]
Using List Comprehension
In this program, we multiply the matrix using the list comprehension method instead of nested loops. List comprehension is a concise way of writing code that generates a list based on another list.
In this case, the program first initializes an empty matrix that will store the result of the multiplication. Then we use list comprehension which generates the values of the resultant matrix by iterating over every combination of rows from the first matrix and columns from the second matrix.
For each combination, the program calculates the dot product by multiplying the corresponding elements of the row and the column, and adding up the results. It then stores this result in the appropriate position of the result matrix.
# Matrix multiplication using list comprehension
# Define the two matrices
matrix1 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
matrix2 = [[9, 8, 7],
[6, 5, 4],
[3, 2, 1]]
# Define the result matrix using list comprehension
result_matrix = [[sum(matrix1[i][k] * matrix2[k][j] for k in range(len(matrix2)))
for j in range(len(matrix2[0]))]
for i in range(len(matrix1))]
# Print the result matrix
for row in result_matrix:
print(row)
Output
[30, 24, 18]
[84, 69, 54]
[138, 114, 90]
Using NumPy Library
In this program we use a NumPy library, which is a powerful tool for scientific computing in Python, to multiply two matrices. Firstly, we import the NumPy library and initialize two matrices as NumPy arrays. Next, we use the dot() method of NumPy arrays to multiply the matrices together.
The dot() method is a built-in function of NumPy arrays that performs matrix multiplication.
The program takes two lists as arguments and returns a new nested list that is the product of the two matrices.
# Matrix multiplication using NumPy library
import numpy as np
# Define the two matrices as NumPy arrays
matrix1 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
matrix2 = np.array([[9, 8, 7],
[6, 5, 4],
[3, 2, 1]])
# Multiply the matrices using the dot product function of NumPy
result_matrix = np.dot(matrix1, matrix2)
# Print the result matrix
print(result_matrix)
Output
[[ 30 24 18]
[ 84 69 54]
[138 114 90]]
Conclusion
Here in this article, we have explored three different methods for computing matrix multiplication in Python. The first method used nested loops to iterate over the rows and columns of the two matrices and compute their dot product. The second method used list comprehension to generate a list of dot products for each combination of rows and columns. Finally, the third method used the powerful NumPy library to perform matrix multiplication using its built-in dot() method.
The using of different methods has its own advantages and disadvantages, and the choice of method will depend on the specific requirements of the problem at hand.
- The nested loops method is simple and easy to understand but can be slow for very large matrices.
- The list comprehension method is more concise and readable, but may not be as efficient as the nested loops method for large matrices.
- The NumPy method is the most efficient and convenient but requires the installation of the NumPy library.