2024-10-20 18:27:51 +02:00
|
|
|
import pytest
|
2024-10-20 16:30:42 +02:00
|
|
|
import numpy as np
|
|
|
|
|
from scipy.sparse.linalg import cg
|
|
|
|
|
from matrix_generator import MatrixGenerator
|
|
|
|
|
from richardson_method import RichardsonMethod
|
|
|
|
|
|
2024-10-20 18:27:51 +02:00
|
|
|
@pytest.mark.parametrize("n", [2, 3, 4, 5, 10, 20, 50, 100])
|
|
|
|
|
def test_richardson_vs_cg(n):
|
2024-10-20 16:30:42 +02:00
|
|
|
tolerance = 1e-5
|
2024-10-20 18:27:51 +02:00
|
|
|
A, b = MatrixGenerator.generate_random_matrix_and_vector(n)
|
2024-10-20 16:30:42 +02:00
|
|
|
|
2024-10-20 18:27:51 +02:00
|
|
|
richardson_solver = RichardsonMethod(A, b, size=n, max_iterations=1000, tol=1e-7)
|
|
|
|
|
solution_richardson = richardson_solver.solve()
|
|
|
|
|
|
|
|
|
|
solution_cg, info = cg(A, b)
|
|
|
|
|
|
|
|
|
|
if info == 0: # SciPy CG converged
|
|
|
|
|
assert_scipy_converged(solution_richardson, solution_cg, tolerance)
|
|
|
|
|
else: # SciPy CG did not converge
|
|
|
|
|
assert_scipy_not_converged(solution_richardson)
|
2024-10-20 16:30:42 +02:00
|
|
|
|
2024-10-20 18:27:51 +02:00
|
|
|
def assert_scipy_converged(solution_richardson, solution_cg, tolerance):
|
|
|
|
|
if solution_richardson == "Richardson method for those values will NOT converge":
|
|
|
|
|
print("Richardson did not converge, while SciPy did")
|
|
|
|
|
assert False, "Richardson did not converge, while SciPy did"
|
|
|
|
|
else:
|
2024-10-20 16:30:42 +02:00
|
|
|
difference = np.linalg.norm(solution_richardson - solution_cg)
|
|
|
|
|
print(f"Difference between Richardson and CG solutions: {difference:.8f}")
|
2024-10-20 18:27:51 +02:00
|
|
|
assert difference < tolerance, f"The solutions are different! Difference: {difference:.8f}"
|
|
|
|
|
|
|
|
|
|
def assert_scipy_not_converged(solution_richardson):
|
|
|
|
|
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)
|
|
|
|
|
assert False, "Richardson converged while SciPy did not"
|