获取scipy的gmres迭代方法的迭代次数
Getting the number of iterations of scipy's gmres iterative method
我想知道 scipy.sparse.linalg.gmres
需要多少次迭代才能收敛,但似乎没有理由证明这一点。可以设置一个 maxiter
参数来终止该方法,但没有显示它所进行的迭代次数。有人可以帮我解决这个问题吗?
为了说明@cel 的评论,您可以像这样实现一个简单的计数器 class:
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 %3i\trk = %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
我想知道 scipy.sparse.linalg.gmres
需要多少次迭代才能收敛,但似乎没有理由证明这一点。可以设置一个 maxiter
参数来终止该方法,但没有显示它所进行的迭代次数。有人可以帮我解决这个问题吗?
为了说明@cel 的评论,您可以像这样实现一个简单的计数器 class:
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 %3i\trk = %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