1
0
This commit is contained in:
Suiranoil
2024-03-20 18:09:50 +03:00
parent 6ed61aeab9
commit a4ac272d64
5 changed files with 216 additions and 55 deletions

View File

@@ -2,4 +2,4 @@ sympy
numpy numpy
matplotlib matplotlib
pyqt6 pyqt6
scipy scipy

View File

@@ -1,3 +1,4 @@
sympy sympy
numpy numpy
scipy scipy
pyqt6

View File

@@ -0,0 +1,100 @@
from typing import List, Tuple
import numpy as np
def det(matrix: np.matrix):
if matrix.shape[0] != matrix.shape[1]:
return None
if matrix.shape[0] == 1:
return matrix[0, 0]
return _det(matrix)
def _det(matrix: np.matrix):
matrix, swaps = triangle(np.copy(matrix))
if matrix is None:
return 0.0
n = matrix.shape[0]
determinant = (-1) ** swaps
for i in range(n):
determinant *= matrix[i, i]
return determinant
def triangle(matrix: np.matrix):
swaps = 0
n = matrix.shape[0]
for i in range(n):
if matrix[i, i] == 0:
if not swap_first_non_zero(matrix, i)[0]:
return None, swaps
swaps += 1
for j in range(i + 1, n):
matrix[j, :] = matrix[j, :] - matrix[i, :] * (matrix[j, i] / matrix[i, i])
return matrix, swaps
def swap_first_non_zero(matrix, i):
n = matrix.shape[0]
for j in range(i, n):
if matrix[j, i] != 0:
if i != j:
matrix[[i, j], :] = matrix[[j, i], :]
return True, j
return False, -1
def swap_max_row(matrix, i):
n = matrix.shape[0]
col_max = abs(matrix[i, i])
max_j = i
for j in range(i, n):
if abs(matrix[j, i]) > col_max:
col_max = abs(matrix[j, i])
max_j = j
if i != max_j:
matrix[[i, max_j], :] = matrix[[max_j, i], :]
return max_j
def swap_max_column(matrix, i):
n = matrix.shape[0]
row_max = abs(matrix[i, i])
max_j = i
for j in range(i, n):
if abs(matrix[i, j]) > row_max and abs(matrix[i, i]) - abs(matrix[j, j]) > 0.0:
row_max = abs(matrix[i, j])
max_j = j
if i != max_j:
matrix[:, [i, max_j]] = matrix[:, [max_j, i]]
return max_j
def norm(matrix: np.matrix):
n = matrix.shape[0]
m = matrix.shape[1]
m_norm = float("-inf")
for i in range(n):
s = 0.0
for j in range(m):
s += abs(matrix[i, j])
if s > m_norm:
m_norm = s
return m_norm
def cond(matrix: np.matrix):
return norm(matrix) * norm(np.linalg.inv(matrix))

View File

@@ -1,68 +1,63 @@
import numpy as np import numpy as np
import solution
def det(matrix: np.matrix):
if matrix.shape[0] != matrix.shape[1]:
return None
if matrix.shape[0] == 1:
return matrix[0, 0]
return _det(matrix)
def _det(matrix: np.matrix):
matrix, swaps = triangle(np.copy(matrix))
if matrix is None:
return 0.0
n = matrix.shape[0]
determinant = (-1) ** swaps
for i in range(n):
determinant *= matrix[i, i]
return determinant
def triangle(matrix: np.matrix):
swaps = 0
n = matrix.shape[0]
for i in range(n):
if matrix[i, i] == 0:
if not swap_first_non_zero(matrix, i):
return None, swaps
swaps += 1
for j in range(i + 1, n):
matrix[j, :] = matrix[j, :] - matrix[i, :] * (matrix[j, i] / matrix[i, i])
return matrix, swaps
def swap_first_non_zero(matrix, i):
n = matrix.shape[0]
for j in range(i + 1, n):
if matrix[j, i] != 0:
matrix[[i, j], :] = matrix[[j, i], :]
return True
return False
A = np.matrix( A = np.matrix(
[ [
[4.3, -12.1, 23.2, -14.1], [4.3, -12.1, 23.2, -14.1],
[2.4, -4.4, 3.5, 5.5], [2.4, -4.4, 3.5, 5.5],
[5.4, 8.3, -7.4, 12.7], [5.4, 8.3, -7.4, -12.7],
[6.3, -7.6, 1.34, 3.7], [6.3, -7.6, 1.34, 3.7],
], ],
dtype=np.float64, dtype=np.float64,
) )
print(det(A)) B = np.matrix([[15.5, 2.5, 8.6, 12.1]], dtype=np.float64).T
print(np.linalg.det(A))
print(A) # A = np.matrix(
# [
# [14.4, -5.3, 14.3, -12.7],
# [23.4, -14.2, -5.4, 2.1],
# [6.3, -13.2, -6.5, 14.3],
# [5.6, 8.8, -6.7, -23.8],
# ],
# dtype=np.float64,
# )
B = np.matrix([[-14.4, 6.6, 9.4, 7.3]], dtype=np.float64).T # B = np.matrix([[-14.4, 6.6, 9.4, 7.3]], dtype=np.float64).T
# X = np.linalg.inv(A) * B
# A = np.matrix(
# [
# [-1, 11, -1, 3],
# [10, -1, 2, 0],
# [2, -1, 10, -1],
# [0, 3, -1, 8],
# ],
# dtype=np.float64,
# )
# B = np.matrix([[25, 6, -11, 15]], dtype=np.float64).T
# print(algorithm.det(A))
# print(np.linalg.det(A))
# print(algorithm.norm(A))
# print(np.linalg.norm(A, ord=float("inf")))
# print(algorithm.cond(A))
# print(np.linalg.cond(A, p=float("inf")))
# koef = np.matrix([[1 / 10000000000000, 1/100000000000, 1, 1]], dtype=np.float64)
# for i in range(A.shape[0]):
# A[i, :] *= koef[0, i]
# B[i, :] *= koef[0, i]
# print(A)
# print(algorithm.cond(A))
# print(B)
X = np.linalg.inv(A) * B
print(solution.iterative_method(A, B))
print(X)

View File

@@ -0,0 +1,65 @@
import algorithm
import numpy as np
def iterative_method(
A: np.matrix, B: np.matrix, eps: float = 0.0001, max_iterations: int = 10000
) -> np.matrix:
if A.shape[0] != A.shape[1] or A.shape[0] != B.shape[0] or B.shape[1] != 1:
return None
n = A.shape[0]
alpha = np.copy(A)
beta = np.copy(B)
for i in range(n):
j = algorithm.swap_max_row(alpha, i)
if alpha[i, i] == 0:
return None
beta[[i, j], :] = beta[[j, i], :]
swaps = []
for i in range(n):
j = algorithm.swap_max_column(alpha, i)
if alpha[i, i] == 0:
return None
swaps.append((i, j))
for i in range(n):
beta[i, 0] = beta[i, 0] / alpha[i, i]
X = np.copy(beta)
for i in range(n):
for j in range(n):
if i == j:
continue
alpha[i, j] = -(alpha[i, j] / alpha[i, i])
alpha[i, i] = 0
print(algorithm.cond(alpha))
if algorithm.cond(alpha) >= 1.0:
return None
i = 0
while i < max_iterations:
i += 1
X_old = np.copy(X)
X = np.add(np.matmul(alpha, X), beta)
if np.sum(np.abs(X - X_old)) < eps:
break
for i, j in swaps:
X[[i, j], :] = X[[j, i], :]
return X