fork download
  1. # Name : Ananda Das;
  2. # ID : 230241053;
  3. # Lab : 3 , Problem : 01;
  4.  
  5. n = int(input("Enter the order of square matrix: "))
  6.  
  7. a = []
  8. print("Enter the elements of augmented matrix row-wise:")
  9. for i in range(n):
  10. row = list(map(float, input().split()))
  11. a.append(row)
  12.  
  13. for j in range(n):
  14.  
  15. max_row = j
  16. for i in range(j + 1, n):
  17. if abs(a[i][j]) > abs(a[max_row][j]):
  18. max_row = i
  19.  
  20.  
  21. a[j], a[max_row] = a[max_row], a[j]
  22.  
  23.  
  24. if abs(a[j][j]) < 1e-10:
  25. print("\nThe system has no unique solution (singular matrix).")
  26. exit()
  27.  
  28. for i in range(j + 1, n):
  29. ratio = a[i][j] / a[j][j]
  30. for k in range(j, n + 1):
  31. a[i][k] -= ratio * a[j][k]
  32.  
  33.  
  34. x = [0] * n
  35. for i in range(n - 1, -1, -1):
  36. x[i] = a[i][n]
  37. for j in range(i + 1, n):
  38. x[i] -= a[i][j] * x[j]
  39. x[i] /= a[i][i]
  40.  
  41.  
  42. print("\nThe solution is:")
  43. for i in range(n):
  44. print(f"x{i+1} = {x[i]:.6f}")
Success #stdin #stdout 0.09s 14148KB
stdin
3
2 1 -1 8
-3 -1 2 -11
-2 1 2 -3
stdout
Enter the order of square matrix: Enter the elements of augmented matrix row-wise:

The solution is:
x1 = 2.000000
x2 = 3.000000
x3 = -1.000000