Programmin-in-Python

Determinant of a Matrix

Dec 21st, 2020 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. # I was able to only program for matrices of order 2 and 3
  2. # I'll soon improve and generalise it, for all order matrices
  3.  
  4. def main() :
  5.    
  6.     M1 = []
  7.     order = int(input("Enter the order of |A| (like 2 or 3) : "))
  8.    
  9.     if order in (2, 3) :
  10.         for i in range(order) :
  11.             temp = []
  12.             for j in range(order) :
  13.                 elem = eval(input("Enter the Element of the Determinant at ; Row : {} , Column : {} =====> ".format(i+1,j+1)))
  14.                 temp.append(elem)
  15.             M1.append(temp)
  16.        
  17.         print("\nThe Determinant : ")
  18.         for i in M1 :
  19.             print("| " , end = " ")
  20.             for j in i :
  21.                 print(j , end = "  ")
  22.             print("|")
  23.            
  24.         if order == 2 :
  25.             det = (M1[0][0]*M1[1][1]) - (M1[0][1]*M1[1][0])
  26.             print("\nThe Value of the Determinant : {}".format(det))
  27.         else :
  28.             row_1 = (M1[0][0]*((M1[1][1]*M1[2][2]) - (M1[1][2]*M1[2][1])))
  29.             row_2 = (M1[0][1]*((M1[1][0]*M1[2][2]) - (M1[1][2]*M1[2][0])))
  30.             row_3 = (M1[0][2]*((M1[1][0]*M1[2][1]) - (M1[1][1]*M1[2][0])))
  31.            
  32.             det = row_1 - row_2 + row_3
  33.             print("\nThe Value of the Determinant : {}".format(det))
  34.            
  35.     else : print("\nSorry for the Inconvinience!!! I haven't Programmed it to Calculate the Value of a Determinant of this order.")
  36.  
  37. main()
Advertisement
Add Comment
Please, Sign In to add comment