Multiplication of two Matrices in Python

Multiplication is one of the basic operations that we perform on matrices. The only condition for the multiplication of two matrices is that the number of columns in the first matrix must be equal to the number of rows in the second matrix.

Suppose the first matrix A is an mxn and the second matrix B is an nxk then the product of these two matrices i.e. AB will be a mxk matrix.

Example –

matrix a

matrix B

matrix AB

Please note here A is a 2×3 matrix, B is a 3×2 matrix So AB is a 2X2 matrix.

Multiplication of two matrices

There are multiple ways in which you can write a program to multiply two matrices in Python. We will discuss the following methods –

  • Using nested for loops
  • Using list comprehension
  • Using Numpy array

So let’s see –

Matrix multiplication using nested for loops

In this method, we iterate through the matrix rows and columns by using nested for loop multiply matrix elements and placing them into a third matrix.

# Python program to multiply two matrices using nested for loops

# 2x3 matrix
A = [[1,2,3],
    [4 ,5,6]]
# 3x2 matrix
B = [[10,11],
    [20,21],
    [30,31]]
# resultant matrix will be 2x2
resultant_matrix = [[0,0],
                   [0,0]]

# Iterate through rows of matrix A
for i in range(len(A)):
   # Iterate through columns of matrix B
   for j in range(len(B[0])):
       # Iterate through rows of B
       for k in range(len(B)):
           resultant_matrix[i][j] += A[i][k] * B[k][j]

for r in resultant_matrix:
   print(r)

Save this program with the .py extension and execute it like given in below –

python3 /home/lalit/python_programs/matrix_multiplication.py

You can see the output of this command in the given image –

matrix mulitplication python

Matrix multiplication using list comprehension

Using list comprehension you can write the same code in only a few lines. You can see its usage in the multiplication of two matrices in Python.

# Python program to multiply two matrices using list comprehension

# The given matrix is a 3x3 matrix
A = [[-1,0,9],
    [8,3,6],
    [7,5,2]]

# The given matrix is 3x4 matrix
B = [[3,0,1,2],
    [1,0,3,7],
    [1,4,1,1]]

# result will be a 3x4 matrix
resultant_matrix = [[sum(a*b for a,b in zip(A_row,B_col)) for B_col in zip(*B)] for A_row in A]

for r in resultant_matrix:
   print(r)

When you execute this code it will display the product of matrices A and B. The following image shows the output of this program –

product of A and B

Matrix multiplication using NumPy

The following program shows the multiplication of two matrices using Numpy in Python.

# Python program to multiply two matrices using Numpy array

import numpy as np

#  3x2 matrix
A = [[2, 6],
    [9, -1],
    [0, 8]]

# 2x3 matrix
B = [[1, 4, 3],
    [0, 8, 5]]

# result will be 3x3

result= [[0,0,0],
         [0,0,0],
         [0,0,0]]

result = np.dot(A,B)

for r in result:
	print(r)

On executing the above code it will display the given output in your terminal –

product

So here we have discussed different ways to multiply two matrices. Now for any queries you can write us in the comments below.

Leave a Comment