fork download
  1. # Name : Ananda Das;
  2. # ID : 230241053;
  3. # Lab : 3 , Problem : 03;
  4. def gauss_jordan():
  5. n = int(input("Enter the order of square matrix: "))
  6.  
  7. print("Enter the elements of augmented matrix row-wise:")
  8. a = []
  9. for i in range(n):
  10. row = list(map(float, input().split()))
  11. a.append(row)
  12.  
  13. for i in range(n):
  14. if a[i][i] == 0.0:
  15. for j in range(i+1, n):
  16. if a[j][i] != 0.0:
  17. a[i], a[j] = a[j], a[i]
  18. break
  19.  
  20. pivot = a[i][i]
  21. for j in range(n + 1):
  22. a[i][j] /= pivot
  23.  
  24. for k in range(n):
  25. if k != i:
  26. factor = a[k][i]
  27. for j in range(n + 1):
  28. a[k][j] -= factor * a[i][j]
  29.  
  30. print("\nFinal Row-Echelon Form [A|B]:")
  31. for row in a:
  32. for val in row:
  33. print(f"{val:.4f}", end=" ")
  34. print()
  35.  
  36. print("\nThe solution is:")
  37. for i in range(n):
  38. print(f"x{i+1} = {a[i][n]:.6f}")
  39.  
  40. gauss_jordan()
  41.  
Success #stdin #stdout 0.09s 14144KB
stdin
3
1 1 1 5
2 3 5 8
4 0 5 2
stdout
Enter the order of square matrix: Enter the elements of augmented matrix row-wise:

Final Row-Echelon Form [A|B]:
1.0000 0.0000 0.0000 3.0000 
0.0000 1.0000 0.0000 4.0000 
0.0000 0.0000 1.0000 -2.0000 

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