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 rows and columns that are and 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 "roundedlist" value are different?
import numpy as np
row intinputEnter the number of rows:
cols intinputEnter the number of columns:
row intinputEnter the number of rows:
cols intinputEnter the number of columns:
printEnter the entries for the first matrix:"
mfloatinput for x in rangecols for y in rangerow
printEnter the entries for the second matrix:"
mfloatinput for x in rangecols for y in rangerow
def matrixmultiplyrecursivem m:
# check if matrices can be multiplied
if lenm lenm:
raise ValueErrorInvalid matrix dimensions"
# initialize result matrix with zeros
result for j in rangelenm for i in rangelenm
# recursive multiplication of matrices
def multiplym m result, i j k:
if i lenm:
return
if j lenm:
return multiplym m result, i
if k lenm:
return multiplym m result, i j
resultij mik mkj
multiplym m result, i j k
# perform matrix multiplication
multiplym m result,
return result
result matrixmultiplyrecursivem m
for row in result:
abslist absx for x in row
roundedlist roundx for x in abslist
printroundedlist
def isidentityroundedlist:
# Create the identity matrix of the same size
identity npeyerow cols
for row in identity:
printfloatvalue for value in row
yyy isidentityroundedlist
abc yyy
xyz nparrayroundedlist
abc nparrayabc
def comparematrixA B:
if nparrayequalroundedlist, yyy:
returnThe matrices are inverses to each other"
else:
returnThe matrices are not inverses of each other"
final comparematrixyyy roundedlist
printfinal
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
