[SciPy-User] scipy.sparse.linalg.cg statistics?

Nico Schlömer nico.schloemer@gmail....
Fri Oct 8 10:42:10 CDT 2010

Hello again!

>    def callback(xk):
>        residuals.append(M*xk - b)

This approach works nicely from a code-beautification point of view,
thanks again for the hint.
There's a major drawback, though, which may not be easily fixed: To
calculate the residual, one has to compute an extra matrix-vector
multiplication *per step*, which effectively increases the runtime of
the CG algorithm by a factor of two (!). Ideally -- and classically --
the residual is retrieved directly from the CG method itself.
Now obviously, the callback method does not provide for residual
vector input, but I'm not quite sure what's the reason for that. In
the numerical computation world, this would be considered a major


On Fri, Oct 1, 2010 at 2:56 PM, Pauli Virtanen <pav@iki.fi> wrote:
> Fri, 01 Oct 2010 12:06:30 +0200, Nico Schlömer wrote:
>>> Use the 'callback' argument.
>> That works alright I guess.
>> What I do right now is creating a *global array that's filled up as the
>> callback function is called, after which I go ahead and plot it. Using a
>> global variable here seems somewhat ugly to me -- might there be a more
>> elegant solution at all?
> Use variables from the outer scope:
> def doit(M, b):
>    residuals = []
>    def callback(xk):
>        residuals.append(M*xk - b)
>    sol, info = scipy.sparse.linalg.cg(M, b, callback=callback)
>    return residuals
> http://docs.python.org/tutorial/classes.html#python-scopes-and-namespaces
> http://www.saltycrane.com/blog/2008/01/python-variable-scope-notes/
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

More information about the SciPy-User mailing list