TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Multiply Two Matrices

Python Multiply Two Matrices for beginners and professionals with programs on basics, controls, loops, functions, native data types etc.

<< Back to PYTHON

Python Program to Multiply Two Matrices

We will write a Python program to get the multiplication of two input matrices and print the result in output. This Python program specifies how to multiply two matrices, having some certain values.

Before writing the Python program, let's first look at the overview of the multiplication of two matrices.

Matrix multiplication

Matrix multiplication is a binary operation that uses a pair of matrices to produce another matrix. The elements within the matrix are multiplied according to elementary arithmetic.

In the multiplication of two matrices, the row elements of the first matrix are multiplied to the column elements of the second matrix.

Example: Suppose we have given following two A and B matrices:

 
A = [[5, 4, 3]
         [2, 4, 6]
         [4, 7, 9]]
and, B = [[3, 2, 4]
                  [4, 3, 6]
                  [2, 7, 5]]

C would be the addition of above given two matrices, i.e., C = A+B, and therefore C should be:

 
C = [[37, 43, 59]
        [34, 58, 62]
        [58, 92, 103]]

As we can see that the resulting matrix C, which is also known as matrix product, has the same number of rows as the first matrix (A matrix) and the same number of columns as the second matrix (B matrix). We also know this type of multiplication of matrices as dot product of matrices.

Multiplication of two matrices

Now, we will write a Python program for the multiplication of two matrices where we perform the multiplication as we have performed in the above-given example. We can use various methods to write a Python program like this, but in this tutorial, we will only use the following two methods:

  1. Using nested for loop method
  2. Using nested list comprehension method

In both methods, we will write an example program to understand their implementation for multiplying two matrices.

Method 1: Using nested for loop method:

In this method, we are going to use nested for loop on two matrices and perform multiplication on them and store multiplication result in the third matrix as the result value.

Let's understand implementation of this method through the following example.

Example: Look at the following Python program:

 
# Define two matrix A and B in program
A = [[5, 4, 3],
     [2, 4, 6],
     [4, 7, 9]]  
B = [[3, 2, 4],
     [4, 3, 6],
     [2, 7, 5]] 
# Define an empty matrix to store multiplication result
multiResult = [[0, 0, 0],  
               [0, 0, 0],  
               [0, 0, 0]]
# Using nested for loop method on A & B matrix
for m in range(len(A)):  
   for n in range(len(B[0])):  
          for o in range(len(B)):  
               multiResult[m][n] += A[m][o] * B[o][n] # Storing multiplication result in empty matrix
# Printing multiplication result in the output
print("The multiplication result of matrix A and B is: ")
for res in multiResult:  
   print(res)

Output:

The multiplication result of matrix A and B is: 
[37, 43, 59]
[34, 58, 62]
[58, 92, 103]

Method 2: Using nested list comprehension method:

In this method, we will use nested list comprehension to get the multiplication result of two input matrices. While using the list comprehension method in the program, we will also use 'zip in Python' on the nested list. Let's understand the implementation of this method through the following example.

Example: Look at the following Python program:

 
# Define two matrix A & B in the program
A = [[5, 4, 3],
         [2, 4, 6],
         [4, 7, 9]]  
B = [[3, 2, 4],
         [4, 3, 6],
         [2, 7, 5]]
# Using nested list method with zip in Python
multiResult = [[sum(a * b for a, b in zip(Arow, Bcol)) 
                       for Bcol in zip(*B)]
                                for Arow in A]
# Printing multiplication result in the output
print("The multiplication result of matrix A and B is: ")
for res in multiResult:
    print(res)

Output:

The multiplication result of matrix A and B is: 
[37, 43, 59]
[34, 58, 62]
[58, 92, 103]





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf