In this post, we will see a how to take matrix input from the user and perform matrix multiplication in Python.
[toc]
Let’s see the example first.
Example:
Input : matrix1 = [[1, 2],
[2, 3]]
matrix2 = [[1, 2],
[2, 3]]
Output : [5, 8]
[8, 13]
Now, let’s see the different ways to do this task:
Using Nested loops(for / while).
In this method, we have to iterate through each row and each column items that’s why we use nested loops here.
Below is the Python code given:
print("Enter order of 1st matrix:")
# take integer inputs in one line
m,n = list(map(int,input().split()))
print("Enter Row wise values")
# empty list for 1st matrix
mat1 = []
# row wise insertion in the matrix
for i in range(m) :
print("Enter row",i,"value:")
# take 1d- integer array input in one line
row = list(map(int,input().split()))
mat1.append(row)
print("Enter order of 2nd matrix:")
# take integer inputs in one line
p,q = list(map(int,input().split()))
print("Enter Row wise values")
# empty list for 2nd matrix
mat2 = []
# row wise insertion in the matrix
for j in range(p) :
print("Enter row",j,"value:")
# take 1d- integer array input in one line
row = list(map(int,input().split()))
mat2.append(row)
# showing 1st and 2nd matrix
print("Matrix 1:",mat1)
print("Matrix 2:",mat2)
# empty list for resulatant matrix
resultant = []
# create a 0-matrix of order m x q
for i in range(m):
row = []
for j in range(q):
row.append(0)
resultant.append(row)
print("Matrix Multiplication: ")
# perform matrix multiplication
# using nested for loops
for i in range(m):
for j in range(q):
for k in range(n) :
resultant[i][j] += mat1[i][k] * mat2[k][j]
# matrix printing row wise
for row in resultant:
print(row)
Output:
Enter order of 1st matrix: 2 2 Enter Row wise values Enter row 0 value: 1 2 Enter row 1 value: 2 3 Enter order of 2nd matrix: 2 2 Enter Row wise values Enter row 0 value: 1 2 Enter row 1 value: 2 3 Matrix 1: [[1, 2], [2, 3]] Matrix 2: [[1, 2], [2, 3]] Matrix Multiplication: [5, 8] [8, 13]
Using dot() method of numpy library.
In this method, dot() method of numpy is used. dot() method is used to find out the dot product of two matrices. dot product is nothing but a simple matrix multiplication in Python using numpy library. We have to pass two matrices in this method for which we have required dot product.
Below is the Python code given:
# import numpy library
import numpy
print("Enter order of 1st matrix:")
m,n = list(map(int,input().split()))
print("Enter Row wise values")
mat1 = []
# row wise insertion in the matrix
for i in range(m) :
print("Enter row",i,"value:")
row = list(map(int,input().split()))
mat1.append(row)
print("Enter order of 2nd matrix:")
p,q = list(map(int,input().split()))
print("Enter Row wise values")
mat2 = []
# row wise insertion in the matrix
for j in range(p) :
print("Enter row",j,"value:")
row = list(map(int,input().split()))
mat2.append(row)
print("Matrix 1:",mat1)
print("Matrix 2:",mat2)
print("Matrix Multiplication: ")
# perform matrix multiplication using
# dot method of numpy library
resultant = numpy.dot(mat1,mat2)
# matrix printing row wise
for row in resultant:
print(row)
Output:
Enter order of 1st matrix: 2 2 Enter Row wise values Enter row 0 value: 1 2 Enter row 1 value: 2 3 Enter order of 2nd matrix: 2 2 Enter Row wise values Enter row 0 value: 1 2 Enter row 1 value: 2 3 Matrix 1: [[1, 2], [2, 3]] Matrix 2: [[1, 2], [2, 3]] Matrix Multiplication: [5, 8] [8, 13]
Using list-comprehension and zip() function.
In this method, zip() function is used to combine list and generated zip object which is the object of list of tuples and list-comprehension is used for creating list and perform operation in one-line code.
Below is the Python code given:
print("Enter order of 1st matrix:")
m,n = list(map(int,input().split()))
print("Enter Row wise values")
mat1 = []
# row wise insertion in the matrix
for i in range(m) :
print("Enter row",i,"value:")
row = list(map(int,input().split()))
mat1.append(row)
print("Enter order of 2nd matrix:")
p,q = list(map(int,input().split()))
print("Enter Row wise values")
mat2 = []
# row wise insertion in the matrix
for j in range(p) :
print("Enter row",j,"value:")
row = list(map(int,input().split()))
mat2.append(row)
print("Matrix 1:",mat1)
print("Matrix 2:",mat2)
print("Matrix Multiplication: ")
# perfrom matrix multiplication using
# list comprehension
resultant = [[ sum(ele1*ele2 for ele1,ele2 in zip(row,col))
for col in zip(*mat2) ]
for row in mat1 ]
# matrix printing row wise
for row in resultant:
print(row)
Output:
Enter order of 1st matrix: 2 2 Enter Row wise values Enter row 0 value: 1 2 Enter row 1 value: 3 4 Enter order of 2nd matrix: 2 2 Enter Row wise values Enter row 0 value: 5 6 Enter row 1 value: 7 8 Matrix 1: [[1, 2], [3, 4]] Matrix 2: [[5, 6], [7, 8]] Matrix Multiplication: [19, 22] [43, 50]
That’s all about Matrix multiplication in Python using user input.