mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 15:23:11 +02:00
fix: removed adding "size" to self values
This commit is contained in:
parent
ea98dc9712
commit
838d5ed563
@ -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
|
||||
|
||||
|
||||
@ -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"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user