Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def pyamg_solve(A, b, tolerance=1e-12):
print("\nSetting up pyamg solver...")
ml = pyamg.aggregation.smoothed_aggregation_solver(A, max_levels=1000)
print(ml)
M = ml.aspreconditioner(cycle='W') # pre-conditioner
residuals = []
def callback(x):
# residuals calculated in the solve function is a pre-conditioned residual
#residuals.append(np.linalg.norm(b - A.dot(x)) ** 0.5)
print(" {:4d} residual = {:.5e} x0-residual = {:.5e}".format(len(residuals) - 1, residuals[-1], residuals[-1] / residuals[0]))
x = ml.solve(b, tol=tolerance, callback=callback, residuals=residuals, accel="cg", maxiter=1e7)
print('Done solving the Poisson equation!')
return x