mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-06 20:03:10 +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
|
@staticmethod
|
||||||
def generate_random_matrix_and_vector(size):
|
def generate_random_matrix_and_vector(size):
|
||||||
A = np.random.uniform(-1, 1, (size, 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)
|
b = np.random.uniform(-1, 1, size)
|
||||||
return A, b
|
return A, b
|
||||||
|
|
||||||
|
|||||||
@ -15,22 +15,29 @@ def test_richardson_vs_cg(n):
|
|||||||
solution_cg, info = cg(A, b)
|
solution_cg, info = cg(A, b)
|
||||||
|
|
||||||
if info == 0: # SciPy CG converged
|
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
|
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":
|
if solution_richardson == "Richardson method for those values will NOT converge":
|
||||||
print("Richardson did not converge, while SciPy did")
|
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"
|
assert False, "Richardson did not converge, while SciPy did"
|
||||||
else:
|
else:
|
||||||
difference = np.linalg.norm(solution_richardson - solution_cg)
|
difference = np.linalg.norm(solution_richardson - solution_cg)
|
||||||
print(f"Difference between Richardson and CG solutions: {difference:.8f}")
|
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}"
|
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":
|
if solution_richardson == "Richardson method for those values will NOT converge":
|
||||||
print("Richardson and SciPy did not converge")
|
print("Richardson and SciPy did not converge")
|
||||||
else:
|
else:
|
||||||
print("Richardson converged while SciPy did not:", solution_richardson)
|
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"
|
assert False, "Richardson converged while SciPy did not"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user