Question: This code is comparing two matrices and seeing if they are inverses by multiplying them together and seeing if they produce an inverse matrices. I

This code is comparing two matrices and seeing if they are inverses by multiplying them together and seeing if they produce an inverse matrices. I have it set up so you can see both matrices when the code is run. With two matrices both 2 rows and 2 columns that are [1,2][3,4] and [-2,1][1.5,-.5](those are inverses) the result should be "The matrices are inverses to each other".
Why is this code saying that the "yyy" value and the "rounded_list" value are different?
import numpy as np
row1= int(input("Enter the number of rows: "))
cols1= int(input("Enter the number of columns: "))
row2= int(input("Enter the number of rows: "))
cols2= int(input("Enter the number of columns: "))
print("Enter the entries for the first matrix:")
m1=[[float(input()) for x in range(cols1)] for y in range(row1)]
print("Enter the entries for the second matrix:")
m2=[[float(input()) for x in range(cols2)] for y in range(row2)]
def matrix_multiply_recursive(m1, m2):
# check if matrices can be multiplied
if len(m1[0])!= len(m2):
raise ValueError("Invalid matrix dimensions")
# initialize result matrix with zeros
result =[[0 for j in range(len(m2[0]))] for i in range(len(m1))]
# recursive multiplication of matrices
def multiply(m1, m2, result, i, j, k):
if i >= len(m1):
return
if j >= len(m2[0]):
return multiply(m1, m2, result, i+1,0,0)
if k >= len(m2):
return multiply(m1, m2, result, i, j+1,0)
result[i][j]+= m1[i][k]* m2[k][j]
multiply(m1, m2, result, i, j, k+1)
# perform matrix multiplication
multiply(m1, m2, result, 0,0,0)
return result
result = matrix_multiply_recursive(m1, m2)
for row in result:
abs_list =[abs(x) for x in row]
rounded_list =[round(x,8) for x in abs_list]
print(rounded_list)
def is_identity(rounded_list):
# Create the identity matrix of the same size
identity = np.eye(row1, cols2)
for row in identity:
print([float(value) for value in row])
yyy = is_identity([rounded_list])
abc = yyy
xyz = np.array([rounded_list])
abc = np.array(abc)
def compare_matrix(A, B):
if (np.array_equal(rounded_list, yyy)):
return("The matrices are inverses to each other")
else:
return("The matrices are not inverses of each other")
final = compare_matrix(yyy, rounded_list)
print(final)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!