'Retrieving number of iterations that ran for sparse linear solver in SciPy
How to retrieve how many iterations ran to achieve specified tolerance level in SciPy sparse linear system solvers?
Solution 1:[1]
For Python 3, the following works:
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
Solution 2:[2]
A tiny change perimosocordiae's answer addressing the "UnboundLocalError":
def solve_sparse(A, b):
solve_sparse.num_iters = 0
def callback(xk):
solve_sparse.num_iters += 1
# call the solver on your data
return scipy.sparse.linalg.cg(A, b, callback=callback)[0]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Abhilash Reddy M |
| Solution 2 | sk7w4tch3r |
