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

Pauli Virtanen pav@iki...
Fri Oct 1 07:56:31 CDT 2010


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/



More information about the SciPy-User mailing list