Cramer\'s Rule on Python Write a Python code for a program indicated below. Use
ID: 3748320 • Letter: C
Question
Cramer's Rule on Python
Write a Python code for a program indicated below. Use variables, loops, if statements and own functions. Do no use any of the functions from python modules but math and copy. Add comments with explanation of each line.
Explanation / Answer
import copy
def genmatrix():
#creates matrix of two rows and columns
col,row = 2, 2;
A1 = [[0 for c in range(col)] for r in range(row)]
A1[0][0]=1
A1[0][1]=-3
A1[1][0]=2
A1[1][1]=-4
b1=[-1 ,1]
A2 = [[0 for c in range(col)] for r in range(row)]
A2[0][0]=5
A2[0][1]=-2
A2[1][0]=5
A2[1][1]=-4
b2=[2 ,-3]
return A1,A2,b1,b2
def det2(A):
#calculates determinant by (a11*a22)-(a12*a21)
return (A[0][0]*A[1][1])- (A[0][1]*A[1][0])
def slove2(A,b):
#implements cramers rule
detA=det2(A)
x_mat = copy.deepcopy(A)
#implements cramers rule by changing first column with b
x_mat[0][0]=b[0]
x_mat[1][0]=b[1]
detX=det2(x_mat)
y_mat = copy.deepcopy(A)
#implements cramers rule by changing second column with b
y_mat[0][1]=b[0]
y_mat[1][1]=b[1]
detY=det2(y_mat)
return detX/detA , detY/detA
A1,A2,b1,b2=genmatrix()
print("the value of X and Y for ist equation is :",slove2(A1,b1))
print("the value of X and Y for 2nd equation is :",slove2(A2,b2))