#NEWLINE# def lgmres( A , b , x0 = None , tol = 1e-05 , maxiter = 1000 , M = None , callback = None , inner_m = 30 , outer_k = 3 , outer_v = None , store_outer_Av = True ) : #NEWLINE# #INDENT# '\n    Solve a matrix equation using the LGMRES algorithm.\n\n    The LGMRES algorithm [1]_ [2]_ is designed to avoid some problems\n    in the convergence in restarted GMRES, and often converges in fewer\n    iterations.\n\n    Parameters\n    ----------\n    A : {sparse matrix, dense matrix, LinearOperator}\n        The real or complex N-by-N matrix of the linear system.\n    b : {array, matrix}\n        Right hand side of the linear system. Has shape (N,) or (N,1).\n    x0  : {array, matrix}\n        Starting guess for the solution.\n    tol : float, optional\n        Tolerance to achieve. The algorithm terminates when either the relative\n        or the absolute residual is below `tol`.\n    maxiter : int, optional\n        Maximum number of iterations.  Iteration will stop after maxiter\n        steps even if the specified tolerance has not been achieved.\n    M : {sparse matrix, dense matrix, LinearOperator}, optional\n        Preconditioner for A.  The preconditioner should approximate the\n        inverse of A.  Effective preconditioning dramatically improves the\n        rate of convergence, which implies that fewer iterations are needed\n        to reach a given error tolerance.\n    callback : function, optional\n        User-supplied function to call after each iteration.  It is called\n        as callback(xk), where xk is the current solution vector.\n    inner_m : int, optional\n        Number of inner GMRES iterations per each outer iteration.\n    outer_k : int, optional\n        Number of vectors to carry between inner GMRES iterations.\n        According to [1]_, good values are in the range of 1...3.\n        However, note that if you want to use the additional vectors to\n        accelerate solving multiple similar problems, larger values may\n        be beneficial.\n    outer_v : list of tuples, optional\n        List containing tuples ``(v, Av)`` of vectors and corresponding\n        matrix-vector products, used to augment the Krylov subspace, and\n        carried between inner GMRES iterations. The element ``Av`` can\n        be `None` if the matrix-vector product should be re-evaluated.\n        This parameter is modified in-place by `lgmres`, and can be used\n        to pass "guess" vectors in and out of the algorithm when solving\n        similar problems.\n    store_outer_Av : bool, optional\n        Whether LGMRES should store also A*v in addition to vectors `v`\n        in the `outer_v` list. Default is True.\n\n    Returns\n    -------\n    x : array or matrix\n        The converged solution.\n    info : int\n        Provides convergence information:\n\n            - 0  : successful exit\n            - >0 : convergence to tolerance not achieved, number of iterations\n            - <0 : illegal input or breakdown\n\n    Notes\n    -----\n    The LGMRES algorithm [1]_ [2]_ is designed to avoid the\n    slowing of convergence in restarted GMRES, due to alternating\n    residual vectors. Typically, it often outperforms GMRES(m) of\n    comparable memory requirements by some measure, or at least is not\n    much worse.\n\n    Another advantage in this algorithm is that you can supply it with\n    \'guess\' vectors in the `outer_v` argument that augment the Krylov\n    subspace. If the solution lies close to the span of these vectors,\n    the algorithm converges faster. This can be useful if several very\n    similar matrices need to be inverted one after another, such as in\n    Newton-Krylov iteration where the Jacobian matrix often changes\n    little in the nonlinear steps.\n\n    References\n    ----------\n    .. [1] A.H. Baker and E.R. Jessup and T. Manteuffel,\n             SIAM J. Matrix Anal. Appl. 26, 962 (2005).\n    .. [2] A.H. Baker, PhD thesis, University of Colorado (2003).\n             http://amath.colorado.edu/activities/thesis/allisonb/Thesis.ps\n\n    ' #NEWLINE# ( A , M , x , b , postprocess ) = make_system ( A , M , x0 , b ) #NEWLINE# if ( not np . isfinite ( b ) . all ( ) ) : #NEWLINE# #INDENT# raise ValueError ( 'RHS must contain only finite numbers' ) #NEWLINE# #UNINDENT# matvec = A . matvec #NEWLINE# psolve = M . matvec #NEWLINE# if ( outer_v is None ) : #NEWLINE# #INDENT# outer_v = [ ] #NEWLINE# #UNINDENT# ( axpy , dot , scal ) = ( None , None , None ) #NEWLINE# nrm2 = get_blas_funcs ( 'nrm2' , [ b ] ) #NEWLINE# b_norm = nrm2 ( b ) #NEWLINE# if ( b_norm == 0 ) : #NEWLINE# #INDENT# b_norm = 1 #NEWLINE# #UNINDENT# for k_outer in xrange ( maxiter ) : #NEWLINE# #INDENT# r_outer = ( matvec ( x ) - b ) #NEWLINE# if ( callback is not None ) : #NEWLINE# #INDENT# callback ( x ) #NEWLINE# #UNINDENT# if ( axpy is None ) : #NEWLINE# #INDENT# if ( np . iscomplexobj ( r_outer ) and ( not np . iscomplexobj ( x ) ) ) : #NEWLINE# #INDENT# x = x . astype ( r_outer . dtype ) #NEWLINE# #UNINDENT# ( axpy , dot , scal , nrm2 ) = get_blas_funcs ( [ 'axpy' , 'dot' , 'scal' , 'nrm2' ] , ( x , r_outer ) ) #NEWLINE# trtrs = get_lapack_funcs ( 'trtrs' , ( x , r_outer ) ) #NEWLINE# #UNINDENT# r_norm = nrm2 ( r_outer ) #NEWLINE# if ( ( r_norm <= ( tol * b_norm ) ) or ( r_norm <= tol ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# vs0 = ( - psolve ( r_outer ) ) #NEWLINE# inner_res_0 = nrm2 ( vs0 ) #NEWLINE# if ( inner_res_0 == 0 ) : #NEWLINE# #INDENT# rnorm = nrm2 ( r_outer ) #NEWLINE# raise RuntimeError ( ( 'Preconditioner returned a zero vector; |v| ~ %.1g, |M v| = 0' % rnorm ) ) #NEWLINE# #UNINDENT# vs0 = scal ( ( 1.0 / inner_res_0 ) , vs0 ) #NEWLINE# vs = [ vs0 ] #NEWLINE# ws = [ ] #NEWLINE# y = None #NEWLINE# Q = np . ones ( ( 1 , 1 ) , dtype = vs0 . dtype ) #NEWLINE# R = np . zeros ( ( 1 , 0 ) , dtype = vs0 . dtype ) #NEWLINE# eps = np . finfo ( vs0 . dtype ) . eps #NEWLINE# for j in xrange ( 1 , ( ( 1 + inner_m ) + len ( outer_v ) ) ) : #NEWLINE# #INDENT# v_new = None #NEWLINE# if ( j < ( len ( outer_v ) + 1 ) ) : #NEWLINE# #INDENT# ( z , v_new ) = outer_v [ ( j - 1 ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( j == ( len ( outer_v ) + 1 ) ) : #NEWLINE# #INDENT# z = vs0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# z = vs [ ( - 1 ) ] #NEWLINE# #UNINDENT# #UNINDENT# if ( v_new is None ) : #NEWLINE# #INDENT# v_new = psolve ( matvec ( z ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# v_new = v_new . copy ( ) #NEWLINE# #UNINDENT# hcur = np . zeros ( ( j + 1 ) , dtype = Q . dtype ) #NEWLINE# for ( i , v ) in enumerate ( vs ) : #NEWLINE# #INDENT# alpha = dot ( v , v_new ) #NEWLINE# hcur [ i ] = alpha #NEWLINE# v_new = axpy ( v , v_new , v . shape [ 0 ] , ( - alpha ) ) #NEWLINE# #UNINDENT# hcur [ ( - 1 ) ] = nrm2 ( v_new ) #NEWLINE# with np . errstate ( over = 'ignore' , divide = 'ignore' ) : #NEWLINE# #INDENT# alpha = ( 1 / hcur [ ( - 1 ) ] ) #NEWLINE# #UNINDENT# if np . isfinite ( alpha ) : #NEWLINE# #INDENT# v_new = scal ( alpha , v_new ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( j > 1 ) : #NEWLINE# #INDENT# j -= 1 #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# vs . append ( v_new ) #NEWLINE# ws . append ( z ) #NEWLINE# Q2 = np . zeros ( ( ( j + 1 ) , ( j + 1 ) ) , dtype = Q . dtype , order = 'F' ) #NEWLINE# Q2 [ : j , : j ] = Q #NEWLINE# Q2 [ ( j , j ) ] = 1 #NEWLINE# R2 = np . zeros ( ( ( j + 1 ) , ( j - 1 ) ) , dtype = R . dtype , order = 'F' ) #NEWLINE# R2 [ : j , : ] = R #NEWLINE# ( Q , R ) = qr_insert ( Q2 , R2 , hcur , ( j - 1 ) , which = 'col' , overwrite_qru = True , check_finite = False ) #NEWLINE# inner_res = ( abs ( Q [ ( 0 , ( - 1 ) ) ] ) * inner_res_0 ) #NEWLINE# if ( inner_res <= ( tol * inner_res_0 ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# ( y , info ) = trtrs ( R [ : j , : j ] , Q [ 0 , : j ] . conj ( ) ) #NEWLINE# if ( info != 0 ) : #NEWLINE# #INDENT# raise RuntimeError ( 'QR solution failed' ) #NEWLINE# #UNINDENT# y *= inner_res_0 #NEWLINE# if ( not np . isfinite ( y ) . all ( ) ) : #NEWLINE# #INDENT# return ( postprocess ( x ) , ( k_outer + 1 ) ) #NEWLINE# #UNINDENT# dx = ( ws [ 0 ] * y [ 0 ] ) #NEWLINE# for ( w , yc ) in zip ( ws [ 1 : ] , y [ 1 : ] ) : #NEWLINE# #INDENT# dx = axpy ( w , dx , dx . shape [ 0 ] , yc ) #NEWLINE# #UNINDENT# nx = nrm2 ( dx ) #NEWLINE# if ( nx > 0 ) : #NEWLINE# #INDENT# if store_outer_Av : #NEWLINE# #INDENT# q = Q . dot ( R . dot ( y ) ) #NEWLINE# ax = ( vs [ 0 ] * q [ 0 ] ) #NEWLINE# for ( v , qc ) in zip ( vs [ 1 : ] , q [ 1 : ] ) : #NEWLINE# #INDENT# ax = axpy ( v , ax , ax . shape [ 0 ] , qc ) #NEWLINE# #UNINDENT# outer_v . append ( ( ( dx / nx ) , ( ax / nx ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# outer_v . append ( ( ( dx / nx ) , None ) ) #NEWLINE# #UNINDENT# #UNINDENT# while ( len ( outer_v ) > outer_k ) : #NEWLINE# #INDENT# del outer_v [ 0 ] #NEWLINE# #UNINDENT# x += dx #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( postprocess ( x ) , maxiter ) #NEWLINE# #UNINDENT# return ( postprocess ( x ) , 0 )