我想知道scipy.sparse.linalg.gmres
需要多少次迭代才能收敛,但似乎没有任何论据。有一个maxiter
参数可以设置为终止该方法,但没有显示它所进行的迭代次数。有人能帮我吗?
为了说明@cel的注释,您可以实现一个简单的计数器类,如下所示:
class gmres_counter(object):
def __init__(self, disp=True):
self._disp = disp
self.niter = 0
def __call__(self, rk=None):
self.niter += 1
if self._disp:
print('iter %3itrk = %s' % (self.niter, str(rk)))
你会这样使用它:
import numpy as np
from scipy.sparse.linalg import gmres
A = np.random.randn(10, 10)
b = np.random.randn(10)
counter = gmres_counter()
x, info = gmres(A, b, callback=counter)
# iter 1 rk = 0.999558746968
# iter 2 rk = 0.960490282387
# iter 3 rk = 0.945887432101
# iter 4 rk = 0.931790454216
# iter 5 rk = 0.877655067142
# iter 6 rk = 0.739596963239
# iter 7 rk = 0.677886023198
# iter 8 rk = 0.52015135005
# iter 9 rk = 0.168298366785
# iter 10 rk = 9.22692033803e-16
print(counter.niter)
# 10