如何检索在SciPy sparse linear system solvers中运行了多少次迭代以达到指定的容差级别?
答案 0 :(得分:2)
对于Python 3,以下工作:
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
答案 1 :(得分:1)
解算器支持在每次迭代后调用的callback
关键字参数。所以你可以这样做:
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]