From 9f5f33495ae418be956ecdc402d81091a8d7ba82 Mon Sep 17 00:00:00 2001 From: aleksandrasob Date: Mon, 11 Nov 2024 17:25:36 +0100 Subject: [PATCH] fixing --- code/eigenvalue_methods.py | 15 ++------------- code/matrix_generator.py | 9 +++++---- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/code/eigenvalue_methods.py b/code/eigenvalue_methods.py index 49f2da8e..78b723d1 100644 --- a/code/eigenvalue_methods.py +++ b/code/eigenvalue_methods.py @@ -1,10 +1,7 @@ -import numpy as np class EigenvalueMethods: @staticmethod 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 - A = np.array(A) - n = A.shape[0] + n = len(A) x = [1] * n lambda_old = 0 @@ -20,17 +17,9 @@ class EigenvalueMethods: @staticmethod def inverse_power_method(LinAlgType, A, max_iter, tol=1e-6): - import scipy - 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] + n = len(A) 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 = list(map(list, zip(*A_inv))) return 1 / EigenvalueMethods.power_method(LinAlgType, A_inv, max_iter, tol) diff --git a/code/matrix_generator.py b/code/matrix_generator.py index a6107f39..51446da4 100644 --- a/code/matrix_generator.py +++ b/code/matrix_generator.py @@ -29,10 +29,12 @@ class MatrixGenerator: def get_matrix_from_file(file_path, problem): mat_contents = scipy.io.loadmat(file_path) problem_record = mat_contents['Problem'][0][0] - A = np.array(problem_record[problem]) + A = problem_record[problem] if scipy.sparse.issparse(A): - A = A.toarray() - return A + A_dense = A.todense() + else: + A_dense = A + return np.array(A_dense) @staticmethod def generate_matrix_and_vector(type, size=None): @@ -53,4 +55,3 @@ class MatrixGenerator: raise ValueError("Invalid type specified. Choose 'spd', 'nemeth12', or 'poli3'.") return matrix, vector -