Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def gauss(A):
- A = np.array(A, dtype=float)
- n, m = A.shape
- def print_matrix(matrix):
- print(np.array_str(matrix, precision=5, suppress_small=True))
- print("Расширенная матрица СЛАУ:")
- print_matrix(A)
- print()
- for i in range(n):
- max_row = i + np.argmax(np.abs(A[i:, i]))
- A[[i, max_row]] = A[[max_row, i]]
- div = A[i, i]
- A[i] /= div
- for j in range(i + 1, n):
- factor = A[j, i]
- A[j] -= factor * A[i]
- print("Прямой ход метода Гаусса:")
- print_matrix(A)
- print()
- # Обратный ход
- for j in range(n - 1, -1, -1):
- for i in range(j - 1, -1, -1):
- d = A[i, j] / A[j, j]
- A[i, j:] -= A[j, j:] * d
- print("Обратный ход метода Гаусса:")
- print_matrix(A)
- print()
- s = [round(A[i, -1] / A[i, i], 4) for i in range(n)]
- print("Решение СЛАУ:")
- print(*s)
- n = 5
- v = 8
- A = [
- [(v + i) / 100 if i != j else v + i for j in range(n)]
- for i in range(n)
- ]
- V = [v + i for i in range(n)]
- B = [sum(A[i][j] * V[j] for j in range(n)) for i in range(n)]
- res_matrix = [A[i] + [B[i]] for i in range(n)]
- gauss(res_matrix)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement