Matrix Multiplication in Python User Input

Matrix Multiplication in Python User Input | Here, we will discuss how to multiply two matrices in Python using user inputs. 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.

We will see these below Python program examples:–

  • Matrix Multiplication In Python Using NumPy
  • Python Matrix Multiplication Without NumPy
  • Matrix Multiplication In Python Using Function
  • Matrix Multiplication In Python Using For Loop
  • Matrix Multiplication In Python Using List

Python Program to Multiply Two Matrices using User Input

Here, we take user input to multiply two matrices; we intake row values and column values from the user and then proceed with the elements.

# Python program to multiply two matrices using numpy

# import NumPy library
import numpy

# take first matrix inputs
print("Enter order of matrix 1:")
m, n = list(map(int, input().split()))
print("Enter row values")
m1 = []
for i in range(m):
   print("Enter row ", i, " values:")
   row = list(map(int, input().split()))
   m1.append(row)

# take second matrix inputs
print("Enter order of matrix 2:")
p, q = list(map(int, input().split()))
print("Enter row values")
m2 = []
for j in range(p):
   print("Enter row ", j, "values: ")
   row = list(map(int, input().split()))
   m2.append(row)

# print both matrices
print("Matrix 1: ", m1)
print("Matrix 2: ", m2)

# multiply matrix
print("Matrix Multiplication: ")
result = numpy.dot(m1, m2)
for row in result:
   print(row)

Output:-

Enter order of matrix 1:
2 2
Enter row values
Enter row 0 values:
2 4
Enter row 1 values:
3 5
Enter order of matrix 2:
2 2
Enter row values
Enter row 0 values:
9 7
Enter row 1 values:
6 5
Matrix 1: [[2, 4], [3, 5]]
Matrix 2: [[9, 7], [6, 5]]
Matrix Multiplication:
[42 34]
[57 46]

In the above code, matrix multiplication in python user input. we are taking two inputs together that is m, n = list(map(int, input().split())) here, we have taken two inputs together as row and column for the first matrix similarly, the same thing is done for second matrix p, q are rows and columns respectively. Then, multiply the matrix using numpy.dot() and store it in the result.

Python Matrix Multiplication using User Input

In the previous program, we used NumPy to multiply two matrices but in this program, we will multiply two matrices without NumPy.

# Python program to multiply two matrices without numpy

# take first matrix inputs
print("Enter the order of matrix 1:")
m, n = list(map(int, input().split()))
print("Enter row values")
m1 = []
for i in range(m):
   print("Enter row",  i, "values:")
   row = list(map(int, input().split()))
   m1.append(row)

# take second matrix inputs
print("Enter the order of matrix2:")
p, q = list(map(int, input().split()))
print("Enter row values")
m2 = []
for j in range(p):
   print("Enter row", j, "value:")
   row = list(map(int, input().split()))
   m2.append(row)

# print both matrices
print("Matrix 1:", m1)
print("Matrix 2:", m2)

# multiply matrix
result = []
for i in range(m):
   row = []
   for j in range(q):
      row.append(0)
   result.append(row)
print("Matrix Multiplication:")
for i in range(m):
   for j in range(q):
      for k in range(n):
         result[i][j] += m1[i][k] * m2[k][j]
for row in result:
   print(row)

Output:-

Enter the order of matrix 1:
3 3
Enter row values
Enter row 0 values:
4 5 6
Enter row 1 values:
3 5 9
Enter row 2 values:
7 6 3
Enter the order of matrix2:
3 3
Enter row values
Enter row 0 value:
7 9 5
Enter row 1 value:
3 4 6
Enter row 2 value:
2 8 9
Matrix 1: [[4, 5, 6], [3, 5, 9], [7, 6, 3]]
Matrix 2: [[7, 9, 5], [3, 4, 6], [2, 8, 9]]
Matrix Multiplication:
[55, 104, 104]
[54, 119, 126]
[73, 111, 98]

Matrix Multiplication in Python User Input

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:-

Enter the number of rows in matrix 1: 2
Enter the number of columns in matrix 1: 3
Enter the elements of matrix 1:
m1[0][0]: 12
m1[0][1]: 23
m1[0][2]: 20
m1[1][0]: 15
m1[1][1]: 21
m1[1][2]: 18
Enter the number of rows in matrix 2: 3
Enter the number of columns in matrix 2: 2
Enter the elements of matrix 2:
m2[0][0]: 4
m2[0][1]: 9
m2[1][0]: 8
m2[1][1]: 7
m2[2][0]: 5
m2[2][1]: 4
Matrix 1:
12 23 20
15 21 18
Matrix 2:
4 9
8 7
5 4
Result:
332 349
318 354

In the above example, we have used two functions (i) matrixPrint() (ii) 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 *