result += v1[i]*v1[i]
result = np.sqrt(result)
return result
def cgsolver(A,b,x0,maxit:int,tol=1.0e-8):
if tol < 0:
print(“argument tol is not good (negitive)”)
if maxit <= 0:
maxit = 1000
xk = x0
rk = b-matvec(A,xk)
dk = rk
for i in range(maxit):
print(“iteration:{}, norm(x)= {}, norm(rk) = {}”.format(i, vecnorm2(xk),vecnorm2(rk)))
if vecnorm2(rk) <1.0e-16:
print(“A good approximation solution is obtained”)
return xk
Adk = matvec(A,dk)
alphak = product(rk,rk)/product(dk,Adk)
xkp = xk + alphak*dk
res = xkp-xk
if vecnorm2(res) < tol:
print(“A good approximation solution is obtained”)
return xk
xk = xkp
rkTrk = product(rk,rk)
rk = rk -alphak*Adk
betak = product(rk,rk)/rkTrk
dk =