Matrix Multiplication in Python User Input using NumPy

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

Python Program to Multiply Two Matrices using NumPy

Now, let’s see an example to multiply two matrices using NumPy, that is NumPy matrix multiplication, as we know NumPy is a built-in library available in python which has a number of functions that simplify the python code.

The numpy.dot() function returns the dot product of two arrays or matrices. For 1-dimensional arrays, it is the inner product of the vectors. For 2-dimensional vectors, it is equivalent to the multiplication of matrices. For N-dimensional arrays, it is a sum-product over the last axis of a and the second-last axis of b.

# Python program to multiply two matrices using numpy

# import NumPy library
import numpy

# take inputs
m1 = [[9, 1],
      [3, 6]]

m2 = [[3, 9],
      [5, 7]]

res = [[0, 0],
       [0, 0]]

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

Output:-

Matrix Multiplication:
[32 88]
[39 69]

The above code works the length of the code has been reduced from the numpy.dot() function; this function directly multiplies two matrices. This is just a single-line implementation.

Matrix Multiplication in Python User Input using NumPy

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user. 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 for the input values test-case-1:-

Enter order of matrix 1:
3 3
Enter row values
Enter row 0 values:
1 2 3
Enter row 1 values:
8 6 2
Enter row 2 values:
7 3 4
Enter order of matrix 2:
3 3
Enter row values
Enter row 0 values:
9 9 8
Enter row 1 values:
7 2 3
Enter row 2 values:
5 4 8
Matrix 1: [[1, 2, 3], [8, 6, 2], [7, 3, 4]]
Matrix 2: [[9, 9, 8], [7, 2, 3], [5, 4, 8]]
Matrix Multiplication:
[38 25 38]
[124 92 98]
[104 85 97]

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

Enter order of matrix 1:
2 3
Enter row values
Enter row 0 values:
10 12 26
Enter row 1 values:
21 25 15
Enter order of matrix 2:
3 2
Enter row values
Enter row 0 values:
2 3
Enter row 1 values:
1 4
Enter row 2 values:
5 2
Matrix 1: [[10, 12, 26], [21, 25, 15]]
Matrix 2: [[2, 3], [1, 4], [5, 2]]
Matrix Multiplication:
[162 130]
[142 193]

In the above code, 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.

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 *