Followers

Showing posts with label Product of two Matrices. Show all posts
Showing posts with label Product of two Matrices. Show all posts

Thursday, April 27, 2023

A Python program that prompts the user for the order and elements of two matrices, calculates their product, and displays the matrices and their product


Here's a Python program that prompts the user for the order and elements of two matrices, calculates their product, and displays the matrices and their product:

python
# prompt user for order of matrices rows1 = int(input("Enter number of rows for matrix 1: ")) cols1 = int(input("Enter number of columns for matrix 1: ")) rows2 = int(input("Enter number of rows for matrix 2: ")) cols2 = int(input("Enter number of columns for matrix 2: ")) # validate that matrices can be multiplied if cols1 != rows2: print("Error: matrices cannot be multiplied because the number of columns in matrix 1 does not match the number of rows in matrix 2.") exit() # initialize matrices matrix1 = [] matrix2 = [] product_matrix = [] # prompt user for elements of first matrix print("Enter elements of matrix 1:") for i in range(rows1): row = [] for j in range(cols1): element = int(input(f"Enter element [{i}][{j}]: ")) row.append(element) matrix1.append(row) # prompt user for elements of second matrix print("Enter elements of matrix 2:") for i in range(rows2): row = [] for j in range(cols2): element = int(input(f"Enter element [{i}][{j}]: ")) row.append(element) matrix2.append(row) # calculate product of matrices for i in range(rows1): row = [] for j in range(cols2): element = 0 for k in range(cols1): element += matrix1[i][k] * matrix2[k][j] row.append(element) product_matrix.append(row) # display matrices and product print("Matrix 1:") for row in matrix1: print(row) print("Matrix 2:") for row in matrix2: print(row) print("Product of matrices:") for row in product_matrix: print(row)

In this program, the user is prompted to enter the number of rows and columns for each matrix, and then is prompted to enter the elements of each matrix. The program validates that the matrices can be multiplied (i.e., that the number of columns in matrix 1 matches the number of rows in matrix 2). The matrices are then multiplied together using the standard matrix multiplication algorithm to produce a third matrix, which is displayed along with the original two matrices.


Here are a few examples of the output of the program:

Example 1:

Python
Enter number of rows for matrix 1: 2 Enter number of columns for matrix 1: 3 Enter number of rows for matrix 2: 3 Enter number of columns for matrix 2: 2 Enter elements of matrix 1: Enter element [0][0]: 1 Enter element [0][1]: 2 Enter element [0][2]: 3 Enter element [1][0]: 4 Enter element [1][1]: 5 Enter element [1][2]: 6 Enter elements of matrix 2: Enter element [0][0]: 7 Enter element [0][1]: 8 Enter element [1][0]: 9 Enter element [1][1]: 10 Enter element [2][0]: 11 Enter element [2][1]: 12 Matrix 1:     [1, 2, 3]     [4, 5, 6] Matrix 2:     [7, 8]     [9, 10]     [11, 12] Product of matrices:     [58, 64]     [139, 154]

Example 2:

Python
Enter number of rows for matrix 1: 3 Enter number of columns for matrix 1: 2 Enter number of rows for matrix 2: 2 Enter number of columns for matrix 2: 4 Enter elements of matrix 1: Enter element [0][0]: 1 Enter element [0][1]: 2 Enter element [1][0]: 3 Enter element [1][1]: 4 Enter element [2][0]: 5 Enter element [2][1]: 6 Enter elements of matrix 2: Enter element [0][0]: 7 Enter element [0][1]: 8 Enter element [0][2]: 9 Enter element [0][3]: 10 Enter element [1][0]: 11 Enter element [1][1]: 12 Enter element [1][2]: 13 Enter element [1][3]: 14 Matrix 1:     [1, 2]     [3, 4]     [5, 6] Matrix 2:     [7, 8, 9, 10]     [11, 12, 13, 14] Product of matrices:     [29, 32, 35, 38]     [65, 72, 79, 86]     [101, 112, 123, 134]

Example 3:

Python
Enter number of rows for matrix 1: 2 Enter number of columns for matrix 1: 2 Enter number of rows for matrix 2: 2 Enter number of columns for matrix 2: 2 Enter elements of matrix 1: Enter element [0][0]: 1 Enter element [0][1]: 2 Enter element [1][0]: 3 Enter element [1][1]: 4 Enter elements of matrix 2: Enter element [0][0]: 5 Enter element [0][1]: 6 Enter element [1][0]: 7 Enter element [1][1]: 8 Matrix 1:     [1, 2]     [3, 4] Matrix 2:     [5, 6]     [7, 8] Product of matrices:     [19, 22]     [43, 50]