From 838d5ed56303a72eb00f5941ca2f82631cf078fb Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Sun, 20 Oct 2024 18:32:12 +0200 Subject: [PATCH] fix: removed adding "size" to self values --- code/matrix_generator.py | 1 - code/tests.py | 15 +++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/code/matrix_generator.py b/code/matrix_generator.py index 0b99273b..24ad3355 100644 --- a/code/matrix_generator.py +++ b/code/matrix_generator.py @@ -4,7 +4,6 @@ class MatrixGenerator: @staticmethod def generate_random_matrix_and_vector(size): A = np.random.uniform(-1, 1, (size, size)) - A = np.dot(A.T, A) + np.eye(size) * size # dodanie `size` do przekątnej zwiększa wartości własne -> obejście problemu z overflow b = np.random.uniform(-1, 1, size) return A, b diff --git a/code/tests.py b/code/tests.py index 3b3a89b9..949a8d32 100644 --- a/code/tests.py +++ b/code/tests.py @@ -15,22 +15,29 @@ def test_richardson_vs_cg(n): solution_cg, info = cg(A, b) if info == 0: # SciPy CG converged - assert_scipy_converged(solution_richardson, solution_cg, tolerance) + assert_scipy_converged(solution_richardson, solution_cg, tolerance, A, b) else: # SciPy CG did not converge - assert_scipy_not_converged(solution_richardson) + assert_scipy_not_converged(solution_richardson, A, b) -def assert_scipy_converged(solution_richardson, solution_cg, tolerance): +def assert_scipy_converged(solution_richardson, solution_cg, tolerance, A, b): if solution_richardson == "Richardson method for those values will NOT converge": print("Richardson did not converge, while SciPy did") + print("Matrix A:\n", A) + print("Vector b:\n", b) assert False, "Richardson did not converge, while SciPy did" else: difference = np.linalg.norm(solution_richardson - solution_cg) print(f"Difference between Richardson and CG solutions: {difference:.8f}") + if difference >= tolerance: + print("Matrix A:\n", A) + print("Vector b:\n", b) assert difference < tolerance, f"The solutions are different! Difference: {difference:.8f}" -def assert_scipy_not_converged(solution_richardson): +def assert_scipy_not_converged(solution_richardson, A, b): if solution_richardson == "Richardson method for those values will NOT converge": print("Richardson and SciPy did not converge") else: print("Richardson converged while SciPy did not:", solution_richardson) + print("Matrix A:\n", A) + print("Vector b:\n", b) assert False, "Richardson converged while SciPy did not"