Getting the number of iterations of an iterative scipy gmres method

I want to know how many iterations it scipy.sparse.linalg.gmrestakes to converge, but there is no argument for this. There is an argument maxiterthat can be set to complete the method, but nothing shows the number of iterations it takes. Can someone help me with this?

+4
source share
1 answer

To illustrate the @cel comment, you can implement a simple counter class as follows:

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)))

You would use it as follows:

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
+5
source

Source: https://habr.com/ru/post/1614373/


All Articles