Matrix Multiplication in Python using Function

Matrix Multiplication In Python using Function | Here, we will discuss how to multiply two matrices in Python using function. Matrix multiplication is a binary operation that multiplies two matrices, as in addition and subtraction both the matrices should be of the same size, but here in multiplication matrices need not be of the same size, but to multiply two matrices the row value of the first matrix should be equal to the column value of the second matrix. Multiplying is a bit more complex than other multiplicative operations.

Matrix Multiplication in Python using Function

In python, we can define our own functions. The main aim for this is to reuse the code hence it reduces the number of lines. To define a function we use the def keyword.

# Python program to multiply two matrices using function

MAX = 100
def matrixPrint(M, row, col):
   for i in range(row):
      for j in range(col):
         print(M[i][j], end=" ")
      print()

def matrixMultiply(row1, col1, m1, row2, col2, m2):
   res = [[0 for i in range(MAX)] for j in range(MAX)]
   if(row2 != col1):
      print("Matrix multiplication not possible")
      return
   for i in range(row1):
      for j in range(col2):
         res[i][j] = 0
         for k in range(row2):
            res[i][j] += m1[i][k] * m2[k][j]
   print("Result:")
   matrixPrint(res, row1, col2)

# main
if __name__ == "__main__":
   m1 = [[0 for i in range(MAX)] for j in range(MAX)]
   m2 = [[0 for i in range(MAX)] for j in range(MAX)]
   row1 = int(input("Enter the number of rows in matrix 1: "))
   col1 = int(input("Enter the number of columns in matrix 1: "))
   print("Enter the elements of matrix 1:")
   for i in range(row1):
      for j in range(col1):
         m1[i][j] = int(input("m1[" + str(i) + "][" + str(j) + "]: "))

   row2 = int(input("Enter the number of rows in matrix 2: "))
   col2 = int(input("Enter the number of columns in matrix 2: "))

   print("Enter the elements of matrix 2: ")
   for i in range(row2):
      for j in range(col2):
         m2[i][j] = int(input("m2[" + str(i) + "][" + str(j) + "]: "))
   print("Matrix 1: ")
   matrixPrint(m1, row1, col1)
   print("Matrix 2: ")
   matrixPrint(m2, row2, col2)
   matrixMultiply(row1, col1, m1, row2, col2, m2)

Output for the input values test-case-1:-

Enter the number of rows in matrix 1: 2
Enter the number of columns in matrix 1: 2
Enter the elements of matrix 1:
m1[0][0]: 20
m1[0][1]: 21
m1[1][0]: 22
m1[1][1]: 23
Enter the number of rows in matrix 2: 2
Enter the number of columns in matrix 2: 2
Enter the elements of matrix 2:
m2[0][0]: 1
m2[0][1]: 2
m2[1][0]: 3
m2[1][1]: 4
Matrix 1:
20 21
22 23
Matrix 2:
1 2
3 4
Result:
83 124
91 136

Output for the input values test-case-2:-

Enter the number of rows in matrix 1: 3
Enter the number of columns in matrix 1: 3
Enter the elements of matrix 1:
m1[0][0]: 9
m1[0][1]: 8
m1[0][2]: 7
m1[1][0]: 6
m1[1][1]: 5
m1[1][2]: 4
m1[2][0]: 3
m1[2][1]: 2
m1[2][2]: 1
Enter the number of rows in matrix 2: 3
Enter the number of columns in matrix 2: 3
Enter the elements of matrix 2:
m2[0][0]: 20
m2[0][1]: 21
m2[0][2]: 22
m2[1][0]: 23
m2[1][1]: 24
m2[1][2]: 25
m2[2][0]: 26
m2[2][1]: 27
m2[2][2]: 28
Matrix 1:
9 8 7
6 5 4
3 2 1
Matrix 2:
20 21 22
23 24 25
26 27 28
Result:
546 570 594
339 354 369
132 138 144

In the above example, we have used two functions matrixPrint() and matrixMultiply(). The first function matrixPrint() takes 3 parameters:- matrix to be printed, rows and columns, this function is used to print the matrix. Then in the second function matrixMultiply(), we have taken 6 parameters; those are rows and columns of matrix1 and matrix2 and matrix1 and matrix2 as well.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *