Getting the number of iterations that were performed for a sparse linear solver in SciPy

How to get how many iterations reached a certain tolerance level in SciPy sparse linear system solvers ?

+3
source share
2 answers

Solvers support the keyword argument callback, which is called after each iteration. So you can do something like this:

def solve_sparse(A, b):
  num_iters = 0

  def callback(xk):
    num_iters += 1

  # call the solver on your data
  return scipy.sparse.linalg.cg(A, b, callback=callback)[0]
+1
source

For Python 3, the following actions are performed:

    def solve_sparse(A, b):
      num_iters = 0

      def callback(xk):
         nonlocal num_iters
         num_iters+=1

      x,status=scipy.sparse.linalg.cg(A, b,tol=1e-15, callback=callback)
      return x,status,num_iters
+2
source

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


All Articles