I am using scipy.sparse.linalg.cg to solve a large, sparse linear system, and it works fine, except that I would like to add a progress report, so that I can monitor the residual as the solver works. I’ve managed to set up a callback, but I can’t figure out how to access the current residual from inside the callback. Calculating the residual myself is possible, of course, but that is a rather heavy operation, which I’d like to avoid. Have I missed something, or is there no efficient way of getting at the residual?
I am using scipy.sparse.linalg.cg to solve a large, sparse linear system, and it works
Share
The callback is only sent
xk, the current solution vector. So you don’t have direct access to the residual. However, the source code showsresidis a local variable in thecgfunction.So, with CPython, it is possible to use the
inspectmodule to peek at the local variables in the caller’s frame: