NumPy - Matrix Multiplication



What is Matrix Multiplication?

Matrix 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.

Conditions for Matrix Multiplication

Before performing matrix multiplication, it is important to ensure that the two matrices meet certain conditions −

  • Number of columns in the first matrix must be equal to the number of rows in the second matrix.
  • Resulting Matrix will have dimensions where the number of rows is taken from the first matrix, and the number of columns is taken from the second matrix.

For example, a matrix of shape (2, 3) can be multiplied by a matrix of shape (3, 4), resulting in a matrix of shape (2, 4).

Matrix Multiplication Formula

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

Matrix Multiplication in NumPy

In NumPy, matrix multiplication can be performed using various methods −

  • Using the * operator: This operator can be used for element-wise multiplication, but it can also be used for matrix multiplication when applied to two 2D arrays.
  • Using @ operator (Python 3.5 and above): The @ operator is designed for matrix multiplication, making the code cleaner and easier to understand.
  • Using np.dot() function: This function computes the dot product of two arrays. For 2D arrays, it performs matrix multiplication.
  • Using np.matmul() function: Similar to np.dot() function, this function is specifically designed for matrix multiplication.

Example of Matrix Multiplication in NumPy

In this example, we will demonstrate matrix multiplication using all the methods mentioned above −

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)

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 −

  • Matrix Multiplication (*): This performs element-wise multiplication, which is not the same as matrix multiplication. It multiplies corresponding elements of the two matrices.
  • Matrix Multiplication (@): This performs true matrix multiplication, following the linear algebra rules. It multiplies the rows of the first matrix by the columns of the second matrix.
  • Matrix Multiplication (np.dot()): The np.dot() function computes the dot product of the two matrices, which in the case of 2D arrays, equals matrix multiplication.
  • Matrix Multiplication (np.matmul()): This function works similarly to np.dot() but is specifically designed for matrix multiplication.

Matrix Multiplication with Larger Matrices

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 −

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)

Here, the result matrix has the shape (2, 4), which is consistent with the rules of matrix multiplication.

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.

[[ 74  80  86  92]
 [173 188 203 218]]

Real-World Applications

Matrix multiplication is a core operation in various real-world applications −

  • Machine Learning: In machine learning, especially in neural networks, matrix multiplication is used to calculate activations in layers of the network. The weights of the network are represented as matrices, and multiplication helps to compute the output.
  • Graphics Rendering: In computer graphics, matrix multiplication is used to transform objects in space. The transformation matrix is multiplied with the objects coordinates to perform scaling, rotation, or translation.
  • Economics: In economics, matrices are used to model systems of linear equations and solve optimization problems like supply and demand models.
Advertisements