Matrix multiplication in Python

Anna Scott
Analytics Vidhya
Published in
3 min readNov 26, 2019

We often encounter data arranged into tables consisting of rows and columns such data structure is called matrix.

We can perform mathematical operations on matrices, however there are certain rules that we have to adhere. We can only multiply two matrices when the the number of columns in the first matrix equals the number of rows in the second matrix. First , we need to multiply the elements of each row of the first matrix by the elements of each column in the second matrix. Then, we need to add the products.

Python doesn’t have a built-in type for matrices. We can implement matrix as a 2D list (list inside list). We can start by initializing two matrices, using the following lines of code:

Notice how the number of columns in the first matrix is the same as the number of rows in the second matrix.

However, to really see the difference in performance speed, we need much larger matrices that would require significantly bigger number of computations. We are going to implement the following function that uses list comprehension to initialize our matrices:

With flexibility of Python we can perform almost any task in a number of way. Let’s look at different ways of ways we can perform matrix multiplication.

We start by multiplying two matrices using list comprehension:

And just using for loop:

The code for list comprehension version of matrix multiplication is more concise, and it also runs faster. Python interpreter spots predictable patterns during looping making list comprehension faster than for loop.

Let’s use Numpy library to speed up the multiplication of our matrices. Numpy utilizes vectorization, which is basically replacing loops(operations on single element of data) with operations that happen simultaneously on the number of elements of data.

To use power of NumPy we need to convert our lists to NumPy arrays.

Now we perform NumPy matrix multiplication and indeed we see the speed up of computations!

It is time even for more speed!

We are going use Pytorch library to move our calculations to GPU, which has parallel architecture that is perfect for calculation of multiplication of large matrices. First, we have to convert our lists to tensors — that is what will allow us to use GPU as well as CPU.

Time to multiply:

Conclusion: if you have an app that has a large number of matrix multiplication, you can significantly speed it up by moving your computations to GPU.

The colab notebook with the complete code could be found here https://colab.research.google.com/drive/1F3EKMP3Vq6oylh8w8HLowYRwpU9x9tAi

and on GitHub

Happy coding my friends!

--

--