#
# "Matrix Machine"
#------------------------
#
# Written By: Wilson and
# GPT-3
#
#######################
frommathimport*fromrandomimportchoice,randintasRANDfromtimeimportsleepdefcalculate_cofactor(matrix,i,j):submatrix=[row[:j]+row[j+1:]forrowin(matrix[:i]+matrix[i+1:])]sign=(-1)**(i+j)returnsign*calculate_determinant(submatrix)defcalculate_determinant(matrix):iflen(matrix)==2:returnmatrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0]a,b,c=matrix[0]d,e,f=matrix[1]g,h,i=matrix[2]returna*(e*i-f*h)-b*(d*i-f*g)+c*(d*h-e*g)try:a=float(input("a: "))except:a=1try:b=float(input("b: "))except:b=1try:c=float(input("c: "))except:c=1try:d=float(input("d: "))except:d=1try:e=float(input("e: "))except:e=1try:f=float(input("f: "))except:f=1try:g=float(input("g: "))except:g=1try:h=float(input("h: "))except:h=1try:i=float(input("i: "))except:i=1A=[[a,b,c],[d,e,f],[g,h,i]]cofact_A=[[calculate_cofactor(A,0,0),calculate_cofactor(A,0,1),calculate_cofactor(A,0,2)],[calculate_cofactor(A,1,0),calculate_cofactor(A,1,1),calculate_cofactor(A,1,2)],[calculate_cofactor(A,2,0),calculate_cofactor(A,2,1),calculate_cofactor(A,2,2)]]sleep(1)print("\nOriginal Matrix:\n")sleep(1)print("================")forrowinA:print(row)print("================")sleep(1)print("\nDeterminant of Matrix:\n")sleep(1)print("================")print(det_A=calculate_determinant(A))print("================")sleep(2)print("\nNew Cofactored Matrix: \n")sleep(1)print("================")forrowincofact_A:print(row)print("================")transpose=[[cofact_A[j][i]forjinrange(len(cofact_A))]foriinrange(len(cofact_A[0]))]# adj(A) = transpose of the # cofactored A
adj_A=transposesleep(2)print("\nAdj(A):\n")sleep(1)print("================")forrowinadj_A:print(row)print("================")sleep(2)ifdet_A!=0:# Calculate the inverse of A
inverse_A=[[adj_A[i][j]/det_Aforjinrange(3)]foriinrange(3)]# Print the inverse matrix
print("\nInverse of A:\n","black")sleep(1)# for row in inverse_A:
# print(row)
forrowininverse_A:rounded_row=[round(element,3)forelementinrow]print(rounded_row)else:print("The matrix A is not invertible since its determinant is zero.")print("\n\n=====Finished.=========")
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.