From 26d60be369719861fac76cbdeebc65870c2b6034 Mon Sep 17 00:00:00 2001 From: lionarius Date: Mon, 25 Mar 2024 05:38:36 +0300 Subject: [PATCH] excel happened --- Л3-В6/Программа/example.txt | 9 +++++++ Л3-В6/Программа/src/solution.py | 44 ++++++++++++++++++++++++++++++--- Л3-В6/Программа/src/window.py | 18 +++++++++++++- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/Л3-В6/Программа/example.txt b/Л3-В6/Программа/example.txt index e9a8bb2..4275bd5 100644 --- a/Л3-В6/Программа/example.txt +++ b/Л3-В6/Программа/example.txt @@ -20,3 +20,12 @@ 23.4 -14.2 -5.4 2.1 6.6 6.3 -13.2 -6.5 14.3 9.4 5.6 8.8 -6.7 -23.8 7.3 + +# some textbook +100 11 0 4 -5 -4 -6 11 +13 200 -2 -2 -4 -2 -1 18 +-7 1 5 12 -4 -5 3 -18 +-5 16 4 -1 0 -3 17 -4 +22 -9 -3 2 300 0 -8 -21 +-5 -3 15 3 15 4 16 -1 +0 -2 -11 -2 -2 10 -150 -6 diff --git a/Л3-В6/Программа/src/solution.py b/Л3-В6/Программа/src/solution.py index 4a5999a..d3fecab 100644 --- a/Л3-В6/Программа/src/solution.py +++ b/Л3-В6/Программа/src/solution.py @@ -4,7 +4,7 @@ import numpy as np def iterative_method( - A: np.matrix, B: np.matrix, eps: float = 0.0001, max_iterations: int = 10000 + A: np.matrix, B: np.matrix, eps: float = 0.0001, max_iterations: int = 100_000 ) -> np.matrix: if A.shape[0] != A.shape[1] or A.shape[0] != B.shape[0] or B.shape[1] != 1: return None @@ -25,6 +25,11 @@ def iterative_method( swaps.extend(swaps_) + _make_diagonal_dominant(alpha, beta) + + if algorithm.det(alpha) == 0: + return None + for i in range(n): beta[i, 0] = beta[i, 0] / alpha[i, i] @@ -37,9 +42,6 @@ def iterative_method( alpha[i, i] = 0 - if algorithm.norm(alpha) >= 1.0: - return None - i = 0 X = np.copy(beta) while i < max_iterations: @@ -51,12 +53,46 @@ def iterative_method( if np.sum(np.abs(X - X_old)) < eps: break + print(f"iterations: {i}") + for i, j in swaps: X[[i, j], :] = X[[j, i], :] return X +# WTF EVEN IS THIS +# EXCEL MOMENT: https://elib.bsu.by/bitstream/123456789/52531/1/42-47.pdf +def _make_diagonal_dominant(a: np.matrix, b: np.matrix): + n = a.shape[0] + a_inv = np.linalg.inv(a.T) + + do_changes = [0] * n + for i in range(n): + if np.sum(np.abs(a[i, :])) > 2 * np.abs(a[i, i]): + do_changes[i] = 1 + + helper = np.ones((n, n)) + for i in range(n): + helper[i, i] = 2 * n + + koef = np.zeros((n, n)) + for i in range(n): + for j in range(n): + if not do_changes[j]: + continue + koef[i, j] = np.sum(a_inv[i, :] * helper[:, j]) * b[i, 0] + + koef_sum = np.sum(koef, axis=0) + + for i in range(n): + if not do_changes[i]: + continue + a[i, :] = np.ones(n) + a[i, i] = 2 * n + b[i, 0] = koef_sum[i] + + def _swap_rows(a: np.matrix, b: np.matrix) -> bool: n = a.shape[0] for i in range(n): diff --git a/Л3-В6/Программа/src/window.py b/Л3-В6/Программа/src/window.py index bc573c2..7bdc9f3 100644 --- a/Л3-В6/Программа/src/window.py +++ b/Л3-В6/Программа/src/window.py @@ -220,6 +220,8 @@ class MainWindow(QMainWindow): def setupUI(self): self.setWindowTitle("Лабораторная работа №3") + self.resize(700, 200) + self.central_widget = QWidget() self.setCentralWidget(self.central_widget) @@ -244,6 +246,10 @@ class MainWindow(QMainWindow): self.paste_button.clicked.connect(self.on_paste_button_clicked) buttons_row_layout.addWidget(self.paste_button) + self.random_button = QPushButton("Случайная", self) + self.random_button.clicked.connect(self.on_random_button_clicked) + buttons_row_layout.addWidget(self.random_button) + buttons_row.setLayout(buttons_row_layout) layout.addWidget(buttons_row) @@ -268,6 +274,13 @@ class MainWindow(QMainWindow): self.central_widget.setLayout(layout) + def on_random_button_clicked(self): + n = int(self.n_input.get_value()) + self.matrix_a = np.round(np.random.rand(n, n) * 10, 2) + self.matrix_b = np.round(np.random.rand(n, 1) * 10, 2) + + self.set_matrix(self.matrix_a, self.matrix_b) + def on_find_solution_clicked(self): x = solution.iterative_method(self.matrix_a, self.matrix_b) @@ -368,9 +381,12 @@ class MainWindow(QMainWindow): try: self.det_display.set_text(f"{algorithm.det(self.matrix_a):.5f}") - self.cond_display.set_text(f"{algorithm.cond(self.matrix_a):.5f}") except: self.det_display.set_text("Невозможно вычислить") + + try: + self.cond_display.set_text(f"{algorithm.cond(self.matrix_a):.5f}") + except: self.cond_display.set_text("Невозможно вычислить") def set_matrix(self, A: np.matrix, B: np.matrix):