Table of Contents
ToggleMatrix multiplication is an operation in linear algebra that involves multiplying two matrices. It is not just a simple element-wise multiplication, but follows specific mathematical rules.
In this operation, the rows of the first matrix are multiplied with the columns of the second matrix, and the results are summed up to form a new matrix.
Matrix multiplication is useful in various fields like physics, computer graphics, and machine learning. It is a key operation in transformations and solving systems of linear equations.
Before performing matrix multiplication, it is important to ensure that the two matrices meet certain conditions −
The formula for matrix multiplication is as follows −
For matrices A of shape (m, n) and B of shape (n, p), the resulting matrix C will have shape (m, p) and each element cij is calculated by:
cij = Σ (aik * bkj) where i = 1 to m, j = 1 to p, and k = 1 to n
In NumPy, matrix multiplication can be performed using various methods −
np.dot() function, this function is specifically designed for matrix multiplication.In this example, we will demonstrate matrix multiplication using all the methods mentioned above −
# Open Compiler
import numpy as np
# Define two matrices
matrix_1 = np.array([[1, 2], [3, 4]])
matrix_2 = np.array([[5, 6], [7, 8]])
# Matrix multiplication using *
result_1 = matrix_1 * matrix_2
# Matrix multiplication using @
result_2 = matrix_1 @ matrix_2
# Matrix multiplication using np.dot()
result_3 = np.dot(matrix_1, matrix_2)
# Matrix multiplication using np.matmul()
result_4 = np.matmul(matrix_1, matrix_2)
print("Matrix Multiplication (*):\n", result_1)
print("Matrix Multiplication (@):\n", result_2)
print("Matrix Multiplication (np.dot()):\n", result_3)
print("Matrix Multiplication (np.matmul()):\n", result_4)Output:
Following is the output obtained −
Matrix Multiplication (*):
[[ 5 12]
[21 32]]
Matrix Multiplication (@):
[[19 22]
[43 50]]
Matrix Multiplication (np.dot()):
[[19 22]
[43 50]]
Matrix Multiplication (np.matmul()):
[[19 22]
[43 50]]
In the output above, you will see that the result of matrix multiplication using different methods is the same, but the approach varies. Here is what each result means −
np.dot() function computes the dot product of the two matrices, which in the case of 2D arrays, equals matrix multiplication.np.dot() but is specifically designed for matrix multiplication.Let us consider a larger example where we have matrices of shapes (2, 3) and (3, 4), and we will multiply them. The resulting matrix will have the shape (2, 4). Here is the code −
# Open Compiler import numpy as np matrix_1 = np.array([[1, 2, 3], [4, 5, 6]]) matrix_2 = np.array([[7, 8, 9, 10], [11, 12, 13, 14], [15, 16, 17, 18]]) result = np.matmul(matrix_1, matrix_2) print(result)
Output:
Here, the result matrix has the shape (2, 4), which is consistent with the rules of matrix multiplication.
[[ 74 80 86 92]
[173 188 203 218]]
Each element in the resulting matrix is the sum of the products of corresponding elements from the rows of the first matrix and the columns of the second matrix.
Matrix multiplication is a core operation in various real-world applications −
Key Takeaway: Master matrix multiplication with NumPy at Vista Academy!
