Advertisement
myloyo

№5 Метод Гаусса решения СЛАУ

Nov 20th, 2024 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def gauss(A):
  4.     A = np.array(A, dtype=float)
  5.     n, m = A.shape
  6.  
  7.     def print_matrix(matrix):
  8.         print(np.array_str(matrix, precision=5, suppress_small=True))
  9.  
  10.     print("Расширенная матрица СЛАУ:")
  11.     print_matrix(A)
  12.     print()
  13.  
  14.     # Прямой ход
  15.     for j in range(m - 1):
  16.         for i in range(j + 1, n):
  17.             d = A[i][j] / A[j][j]
  18.             A[i, j:] -= A[j, j:] * d
  19.  
  20.     print("Прямой ход метода Гаусса:")
  21.     print_matrix(A)
  22.     print()
  23.  
  24.     # Обратный ход
  25.     for j in range(n - 1, -1, -1):
  26.         for i in range(j - 1, -1, -1):
  27.             d = A[i, j] / A[j, j]
  28.             A[i, j:] -= A[j, j:] * d
  29.  
  30.     print("Обратный ход метода Гаусса:")
  31.     print_matrix(A)
  32.     print()
  33.  
  34.     s = [round(A[i, -1] / A[i, i], 4) for i in range(n)]
  35.     print("Решение СЛАУ:")
  36.     print(*s)
  37.  
  38.  
  39. n = 5
  40. v = 8
  41. A = [
  42.     [(v + i) / 100 if i != j else v + i for j in range(n)]
  43.     for i in range(n)
  44. ]
  45. V = [v + i for i in range(n)]
  46. B = [sum(A[i][j] * V[j] for j in range(n)) for i in range(n)]
  47. res_matrix = [A[i] + [B[i]] for i in range(n)]
  48.  
  49. gauss(res_matrix)
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement