Friday, January 7, 2022

Python Program to create matrices based on user Input and find their Product

Python Program to to accept user input for two Matrices and find their product
import sys
from numpy import *

r1, c1 = [int(i) for i in input('Enter the no. of rows, cols of first Matrix: ').split()]
print('The no.of rows, cols of first Matrix is: ', r1, c1)

r2, c2 = [int(j) for j in input('Enter the no. of rows, cols of second Matrix: ').split()]
print('The no.of rows, cols of second Matrix is: ', r2, c2)

# Checking matrix multiplication possibility
if c1 != r2:
    print('The multiplication is not possible')
    sys.exit()
else:
    str1 = input('Enter the first matrix elements:\n')
    x = reshape(matrix(str1), (r1, c1))
    print('The first matrix elements are:\n', x)

    str2 = input('Enter the second matrix elements:\n')
    y = reshape(matrix(str2), (r2, c2))
    print('The first matrix elements are:\n', y)

    print('The product of matrices is:\n')
    z = x * y
    print(z)

# ------------------------------------------------------------------------------------------------------------------------ #
# Input & Output :
Enter the no. of rows, cols of first Matrix: 3 2
The no. of rows, cols of first Matrix is:  3 2
Enter the no .of rows, cols of second Matrix: 2 3
The no. of rows, cols of second Matrix is:  2 3

Enter the first matrix elements:
4 2 6 2 1 3

The first matrix elements are:
 [[4 2]
 [6 2]
 [1 3]]

Enter the second matrix elements:
-2 4 -3 0 1 6

The second matrix elements are:
 [[-2  4 -3]
 [ 0  1  6]]

The product of matrices is:
[[ -8  18   0]
 [-12  26  -6]
 [ -2   7  15]]

Process finished with exit code 0

#--------------------------------------------------------------Thanks--------------------------------------------------------------#

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog