Merge pull request #8 from kuhyx/ola

fixing
This commit is contained in:
aleksandrasob 2024-11-12 20:55:46 +01:00 committed by GitHub
commit 6b067c9077
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 17 deletions

View File

@ -1,10 +1,7 @@
import numpy as np
class EigenvalueMethods: class EigenvalueMethods:
@staticmethod @staticmethod
def power_method(LinAlgType, A, max_iter, tol=1e-6): def power_method(LinAlgType, A, max_iter, tol=1e-6):
if isinstance(A, list): #słabe, szkoda czasu, trzeba przypilnować, żeby od razu każda macierz była tego samego typu n = len(A)
A = np.array(A)
n = A.shape[0]
x = [1] * n x = [1] * n
lambda_old = 0 lambda_old = 0
@ -20,17 +17,9 @@ class EigenvalueMethods:
@staticmethod @staticmethod
def inverse_power_method(LinAlgType, A, max_iter, tol=1e-6): def inverse_power_method(LinAlgType, A, max_iter, tol=1e-6):
import scipy n = len(A)
if scipy.sparse.issparse(A):
A = A.toarray() # Convert sparse matrix to dense array
if isinstance(A, list):
A = np.array(A) # Convert list to NumPy array if needed
n = A.shape[0]
I = [[1 if i == j else 0 for j in range(n)] for i in range(n)] I = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
A_inv = [LinAlgType.gaussian_elimination(A.tolist(), I_col) for I_col in I] A_inv = [LinAlgType.gaussian_elimination(A.tolist(), I_col) for I_col in I]
A_inv = list(map(list, zip(*A_inv))) A_inv = list(map(list, zip(*A_inv)))
return 1 / EigenvalueMethods.power_method(LinAlgType, A_inv, max_iter, tol) return 1 / EigenvalueMethods.power_method(LinAlgType, A_inv, max_iter, tol)

View File

@ -29,10 +29,12 @@ class MatrixGenerator:
def get_matrix_from_file(file_path, problem): def get_matrix_from_file(file_path, problem):
mat_contents = scipy.io.loadmat(file_path) mat_contents = scipy.io.loadmat(file_path)
problem_record = mat_contents['Problem'][0][0] problem_record = mat_contents['Problem'][0][0]
A = np.array(problem_record[problem]) A = problem_record[problem]
if scipy.sparse.issparse(A): if scipy.sparse.issparse(A):
A = A.toarray() A_dense = A.todense()
return A else:
A_dense = A
return np.array(A_dense)
@staticmethod @staticmethod
def generate_matrix_and_vector(type, size=None): def generate_matrix_and_vector(type, size=None):
@ -53,4 +55,3 @@ class MatrixGenerator:
raise ValueError("Invalid type specified. Choose 'spd', 'nemeth12', or 'poli3'.") raise ValueError("Invalid type specified. Choose 'spd', 'nemeth12', or 'poli3'.")
return matrix, vector return matrix, vector