# Matrix Multiplication in Python Consumer Enter – Know Program

Matrix Multiplication in Python Consumer Enter | Right here, we’ll focus on how to multiply two matrices in Python utilizing consumer inputs. Matrix multiplication is a binary operation that multiplies two matrices, as as well as and subtraction each the matrices needs to be of the identical measurement, however right here in multiplication matrices needn’t be of the identical measurement, however to multiply two matrices the row worth of the primary matrix needs to be equal to the column worth of the second matrix. Multiplying is a little more complicated than different multiplicative operations.

We’ll see these beneath Python program examples:–

1. Matrix multiplication in python utilizing numpy
2. Python matrix multiplication with out numpy
3. Matrix multiplication in python utilizing perform
4. Matrix multiplication in python utilizing for loop
5. Matrix multiplication in python utilizing checklist

## Python Program to Multiply Two Matrices utilizing Consumer Enter

Right here, we take consumer enter to multiply two matrices; we consumption row values and column values from the consumer after which proceed with the weather.

``````# Python program to multiply two matrices utilizing numpy

# import NumPy library
import numpy

# take first matrix inputs
print("Enter order of matrix 1:")
m, n = checklist(map(int, enter().cut up()))
print("Enter row values")
m1 = []
for i in vary(m):
print("Enter row ", i, " values:")
row = checklist(map(int, enter().cut up()))
m1.append(row)

# take second matrix inputs
print("Enter order of matrix 2:")
p, q = checklist(map(int, enter().cut up()))
print("Enter row values")
m2 = []
for j in vary(p):
print("Enter row ", j, "values: ")
row = checklist(map(int, enter().cut up()))
m2.append(row)

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

# multiply matrix
print("Matrix Multiplication: ")
end result = numpy.dot(m1, m2)
for row in end 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]

Within the above code, matrix multiplication in python consumer enter. we’re taking two inputs collectively that’s m, n = checklist(map(int, enter().cut up())) right here, we’ve got taken two inputs collectively as row and column for the primary matrix equally, the identical factor is finished for second matrix p, q are rows and columns respectively. Then, multiply the matrix utilizing numpy.dot() and retailer it within the end result.

## Python Matrix Multiplication utilizing Consumer Enter

Within the earlier program, we used NumPy to multiply two matrices however on this program, we’ll multiply two matrices with out NumPy.

``````# Python program to multiply two matrices with out numpy

# take first matrix inputs
print("Enter the order of matrix 1:")
m, n = checklist(map(int, enter().cut up()))
print("Enter row values")
m1 = []
for i in vary(m):
print("Enter row",  i, "values:")
row = checklist(map(int, enter().cut up()))
m1.append(row)

# take second matrix inputs
print("Enter the order of matrix2:")
p, q = checklist(map(int, enter().cut up()))
print("Enter row values")
m2 = []
for j in vary(p):
print("Enter row", j, "worth:")
row = checklist(map(int, enter().cut up()))
m2.append(row)

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

# multiply matrix
end result = []
for i in vary(m):
row = []
for j in vary(q):
row.append(0)
end result.append(row)
print("Matrix Multiplication:")
for i in vary(m):
for j in vary(q):
for okay in vary(n):
end result[i][j] += m1[i][k] * m2[k][j]
for row in end 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 worth:
7 9 5
Enter row 1 worth:
3 4 6
Enter row 2 worth:
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 Consumer Enter

In python, we are able to outline our personal features. The principle purpose for that is to reuse the code therefore it reduces the variety of strains. To outline a perform we use the def key phrase.

``````# Python program to multiply two matrices utilizing perform

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

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

# essential
if __name__ == "__main__":
m1 = [[0 for i in range(MAX)] for j in vary(MAX)]
m2 = [[0 for i in range(MAX)] for j in vary(MAX)]
row1 = int(enter("Enter the variety of rows in matrix 1: "))
col1 = int(enter("Enter the variety of columns in matrix 1: "))
print("Enter the weather of matrix 1:")
for i in vary(row1):
for j in vary(col1):
m1[i][j] = int(enter("m1[" + str(i) + "][" + str(j) + "]: "))

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

print("Enter the weather of matrix 2: ")
for i in vary(row2):
for j in vary(col2):
m2[i][j] = int(enter("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 variety of rows in matrix 1: 2
Enter the variety of columns in matrix 1: 3
Enter the weather of matrix 1:
m1: 12
m1: 23
m1: 20
m1: 15
m1: 21
m1: 18
Enter the variety of rows in matrix 2: 3
Enter the variety of columns in matrix 2: 2
Enter the weather of matrix 2:
m2: 4
m2: 9
m2: 8
m2: 7
m2: 5
m2: 4
Matrix 1:
12 23 20
15 21 18
Matrix 2:
4 9
8 7
5 4
End result:
332 349
318 354

Within the above instance, we’ve got used two features (i) matrixPrint() (ii) matrixMultiply(). The primary perform matrixPrint() takes 3 parameters:- matrix to be printed, rows and columns, this perform is used to print the matrix. Then within the second perform matrixMultiply(), we’ve got taken 6 parameters; these are rows and columns of matrix1 and matrix2 and matrix1 and matrix2 as nicely.

In case you loved this publish, share it with your mates. Do you need to share extra details about the subject mentioned above or do you discover something incorrect? Tell us within the feedback. Thanks!