Python Program To Multiply Two Matrices

In this post, I will discuss the python program to multiply two matrices. I will also show you different methods like for loops, list comprehension, numpy array to solve the problem.

1. Python Program To Multiply Two Matrices Using For Loop

Let’s use the nested for loop to multiply two matrices.

# Python program to multiply two matrices

#3x3 matrix
x = [[4, 1, 3],
    [6, 5, 8],
    [9, 0, 7]]

# 3x4 matrix
y = [[6, 7, 3, 2],
    [5, 1, 9, 0],
    [4, 5, 9, 1]]     #1

z = [[0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0]]     #2

# Using Nested For Loops
for i in range(len(x)): #3

    for j in range(len(y[0])): #4

        for k in range(len(y)): #5
            z[i][j] += x[i][k] * y[k][j] #6

for r in z: #7
    print(r)

Save and run the above code you should see the following output.

Output :

Python Program To Multiply Two Matrices output

Code Explanation :

  1. We take two input matrices (x and y) with dimensions 3×3 and 4×4 respectively.
  2. We initialize the result matrix of dimension 3×4 with zero values.
  3. We iterate through each row of the matrix x.
  4. We iterate through each column of matrix y.
  5. We iterate through each row of matrix y.
  6. We multiply the element and update the result matrix.
  7. Display each row of matrix z to the console.

2. Python Program To Multiply Two Matrices Using List Comprehension

In case you don’t know list comprehension is one of the most elegant feature of python programming language that allows you to create a list more concisely. here is the example.

# Without List Comp
old = [1,2,3]
square = []
for i in old:
    new.append(i**2)


# With List Comp
old = [1,2,3]
new = [i**2 for i in old]

It’s clear that how elegant list comprehensions are. so let’s use them in our program to do matrix multiplication. here is the code.

x = [[4, 1, 3],
    [6, 5, 8],
    [9, 0, 7]]

y = [[6, 7, 3, 2],
    [5, 1, 9, 0],
    [4, 5, 9, 1]]


result = [[sum(a * b for a, b in zip(x_row, y_col))
                        for y_col in zip(*y)]
                                for x_row in x]

for r in result:
    print(r)

Nice short and sweet!. save and run the above code you should see the exact same output.

Output :

[41, 44, 48, 11]
[93, 87, 135, 20]
[82, 98, 90, 25]

In the above example we also used some builtin functions like sum and zip . in case never heard of them check them now.

3. Program To Multiply Two Matrices Using Numpy

Numpy is a very powerful and popular external library mostly used for scientific computing. It’s way more efficient than our standard python list. So let’s use it to do matrix multiplication.

Please note numpy is the external package you have to install it manually using pip.

import numpy as np

x = [[4, 1, 3],
    [6, 5, 8],
    [9, 0, 7]]

y = [[6, 7, 3, 2],
    [5, 1, 9, 0],
    [4, 5, 9, 1]]

result = np.dot(x,y)

for r in result:
    print(r)

That’s it for this tutorial I hope you have learned something new. If you have doubts or want to give any suggestion please feel free to comment below.

Leave a Reply