excel happened
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user