def bicgstab(A, b, x0=None, tol=1e-5, maxiter=None, xtype=None, M=None, callback=None): A,M,x,b,postprocess = make_system(A,M,x0,b,xtype) n = len(b) if maxiter is None: maxiter = n*10 matvec = A.matvec psolve = M.matvec ltr = _type_conv[x.dtype.char] revcom = getattr(_iterative, ltr + 'bicgstabrevcom') stoptest = getattr(_iterative, ltr + 'stoptest2') resid = tol ndx1 = 1 ndx2 = -1 work = np.zeros(7*n,dtype=x.dtype) ijob = 1 info = 0 ftflag = True bnrm2 = -1.0 iter_ = maxiter while True: olditer = iter_ x, iter_, resid, info, ndx1, ndx2, sclr1, sclr2, ijob = \ revcom(b, x, work, iter_, resid, info, ndx1, ndx2, ijob) if callback is not None and iter_ > olditer: callback(x) slice1 = slice(ndx1-1, ndx1-1+n) slice2 = slice(ndx2-1, ndx2-1+n) if (ijob == -1): if callback is not None: callback(x) break elif (ijob == 1): if matvec is None: matvec = get_matvec(A) work[slice2] *= sclr2 work[slice2] += sclr1*matvec(work[slice1]) elif (ijob == 2): if psolve is None: psolve = get_psolve(A) work[slice1] = psolve(work[slice2]) elif (ijob == 3): if matvec is None: matvec = get_matvec(A) work[slice2] *= sclr2 work[slice2] += sclr1*matvec(x) elif (ijob == 4): if ftflag: info = -1 ftflag = False bnrm2, resid, info = stoptest(work[slice1], b, bnrm2, tol, info) ijob = 2 if info > 0 and iter_ == maxiter and resid > tol: #info isn't set appropriately otherwise info = iter_ return postprocess(x), info
def lgmres(A, b, x0=None, tol=1e-5, maxiter=1000, M=None, callback=None, inner_m=30, outer_k=3, outer_v=None, store_outer_Av=True): """ Solve a matrix equation using the LGMRES algorithm. The LGMRES algorithm [BJM]_ [BPh]_ is designed to avoid some problems in the convergence in restarted GMRES, and often converges in fewer iterations. Parameters ---------- A : {sparse matrix, dense matrix, LinearOperator} The real or complex N-by-N matrix of the linear system. b : {array, matrix} Right hand side of the linear system. Has shape (N,) or (N,1). x0 : {array, matrix} Starting guess for the solution. tol : float Tolerance to achieve. The algorithm terminates when either the relative or the absolute residual is below `tol`. maxiter : integer Maximum number of iterations. Iteration will stop after maxiter steps even if the specified tolerance has not been achieved. M : {sparse matrix, dense matrix, LinearOperator} Preconditioner for A. The preconditioner should approximate the inverse of A. Effective preconditioning dramatically improves the rate of convergence, which implies that fewer iterations are needed to reach a given error tolerance. callback : function User-supplied function to call after each iteration. It is called as callback(xk), where xk is the current solution vector. Additional parameters --------------------- inner_m : int, optional Number of inner GMRES iterations per each outer iteration. outer_k : int, optional Number of vectors to carry between inner GMRES iterations. According to [BJM]_, good values are in the range of 1...3. However, note that if you want to use the additional vectors to accelerate solving multiple similar problems, larger values may be beneficial. outer_v : list of tuples, optional List containing tuples ``(v, Av)`` of vectors and corresponding matrix-vector products, used to augment the Krylov subspace, and carried between inner GMRES iterations. The element ``Av`` can be `None` if the matrix-vector product should be re-evaluated. This parameter is modified in-place by `lgmres`, and can be used to pass "guess" vectors in and out of the algorithm when solving similar problems. store_outer_Av : bool, optional Whether LGMRES should store also A*v in addition to vectors `v` in the `outer_v` list. Default is True. Returns ------- x : array or matrix The converged solution. info : integer Provides convergence information: 0 : successful exit >0 : convergence to tolerance not achieved, number of iterations <0 : illegal input or breakdown Notes ----- The LGMRES algorithm [BJM]_ [BPh]_ is designed to avoid the slowing of convergence in restarted GMRES, due to alternating residual vectors. Typically, it often outperforms GMRES(m) of comparable memory requirements by some measure, or at least is not much worse. Another advantage in this algorithm is that you can supply it with 'guess' vectors in the `outer_v` argument that augment the Krylov subspace. If the solution lies close to the span of these vectors, the algorithm converges faster. This can be useful if several very similar matrices need to be inverted one after another, such as in Newton-Krylov iteration where the Jacobian matrix often changes little in the nonlinear steps. References ---------- .. [BJM] A.H. Baker and E.R. Jessup and T. Manteuffel, SIAM J. Matrix Anal. Appl. 26, 962 (2005). .. [BPh] A.H. Baker, PhD thesis, University of Colorado (2003). http://amath.colorado.edu/activities/thesis/allisonb/Thesis.ps """ from scipy.linalg.basic import lstsq A,M,x,b,postprocess = make_system(A,M,x0,b) if not np.isfinite(b).all(): raise ValueError("RHS must contain only finite numbers") matvec = A.matvec psolve = M.matvec if outer_v is None: outer_v = [] axpy, dot, scal = None, None, None b_norm = norm2(b) if b_norm == 0: b_norm = 1 for k_outer in xrange(maxiter): r_outer = matvec(x) - b # -- callback if callback is not None: callback(x) # -- determine input type routines if axpy is None: if np.iscomplexobj(r_outer) and not np.iscomplexobj(x): x = x.astype(r_outer.dtype) axpy, dot, scal = get_blas_funcs(['axpy', 'dot', 'scal'], (x, r_outer)) # -- check stopping condition r_norm = norm2(r_outer) if r_norm < tol * b_norm or r_norm < tol: break # -- inner LGMRES iteration vs0 = -psolve(r_outer) inner_res_0 = norm2(vs0) if inner_res_0 == 0: rnorm = norm2(r_outer) raise RuntimeError("Preconditioner returned a zero vector; " "|v| ~ %.1g, |M v| = 0" % rnorm) vs0 = scal(1.0/inner_res_0, vs0) hs = [] vs = [vs0] ws = [] y = None for j in xrange(1, 1 + inner_m + len(outer_v)): # -- Arnoldi process: # # Build an orthonormal basis V and matrices W and H such that # A W = V H # Columns of W, V, and H are stored in `ws`, `vs` and `hs`. # # The first column of V is always the residual vector, `vs0`; # V has *one more column* than the other of the three matrices. # # The other columns in V are built by feeding in, one # by one, some vectors `z` and orthonormalizing them # against the basis so far. The trick here is to # feed in first some augmentation vectors, before # starting to construct the Krylov basis on `v0`. # # It was shown in [BJM]_ that a good choice (the LGMRES choice) # for these augmentation vectors are the `dx` vectors obtained # from a couple of the previous restart cycles. # # Note especially that while `vs0` is always the first # column in V, there is no reason why it should also be # the first column in W. (In fact, below `vs0` comes in # W only after the augmentation vectors.) # # The rest of the algorithm then goes as in GMRES, one # solves a minimization problem in the smaller subspace # spanned by W (range) and V (image). # # XXX: Below, I'm lazy and use `lstsq` to solve the # small least squares problem. Performance-wise, this # is in practice acceptable, but it could be nice to do # it on the fly with Givens etc. # # ++ evaluate v_new = None if j < len(outer_v) + 1: z, v_new = outer_v[j-1] elif j == len(outer_v) + 1: z = vs0 else: z = vs[-1] if v_new is None: v_new = psolve(matvec(z)) else: # Note: v_new is modified in-place below. Must make a # copy to ensure that the outer_v vectors are not # clobbered. v_new = v_new.copy() # ++ orthogonalize hcur = [] for v in vs: alpha = dot(v, v_new) hcur.append(alpha) v_new = axpy(v, v_new, v.shape[0], -alpha) # v_new -= alpha*v hcur.append(norm2(v_new)) if hcur[-1] == 0: # Exact solution found; bail out. # Zero basis vector (v_new) in the least-squares problem # does no harm, so we can just use the same code as usually; # it will give zero (inner) residual as a result. bailout = True else: bailout = False v_new = scal(1.0/hcur[-1], v_new) vs.append(v_new) hs.append(hcur) ws.append(z) # XXX: Ugly: should implement the GMRES iteration properly, # with Givens rotations and not using lstsq. Instead, we # spare some work by solving the LSQ problem only every 5 # iterations. if not bailout and j % 5 != 1 and j < inner_m + len(outer_v) - 1: continue # -- GMRES optimization problem hess = np.zeros((j+1, j), x.dtype) e1 = np.zeros((j+1,), x.dtype) e1[0] = inner_res_0 for q in xrange(j): hess[:(q+2),q] = hs[q] y, resids, rank, s = lstsq(hess, e1) inner_res = norm2(np.dot(hess, y) - e1) # -- check for termination if inner_res < tol * inner_res_0: break # -- GMRES terminated: eval solution dx = ws[0]*y[0] for w, yc in zip(ws[1:], y[1:]): dx = axpy(w, dx, dx.shape[0], yc) # dx += w*yc # -- Store LGMRES augmentation vectors nx = norm2(dx) if store_outer_Av: q = np.dot(hess, y) ax = vs[0]*q[0] for v, qc in zip(vs[1:], q[1:]): ax = axpy(v, ax, ax.shape[0], qc) outer_v.append((dx/nx, ax/nx)) else: outer_v.append((dx/nx, None)) # -- Retain only a finite number of augmentation vectors while len(outer_v) > outer_k: del outer_v[0] # -- Apply step x += dx else: # didn't converge ... return postprocess(x), maxiter return postprocess(x), 0
def lgmres(A, b, x0=None, tol=1e-5, maxiter=1000, M=None, callback=None, inner_m=30, outer_k=3, outer_v=None, store_outer_Av=True): """ Solve a matrix equation using the LGMRES algorithm. The LGMRES algorithm [BJM]_ [BPh]_ is designed to avoid some problems in the convergence in restarted GMRES, and often converges in fewer iterations. Parameters ---------- A : {sparse matrix, dense matrix, LinearOperator} The N-by-N matrix of the linear system. b : {array, matrix} Right hand side of the linear system. Has shape (N,) or (N,1). x0 : {array, matrix} Starting guess for the solution. tol : float Tolerance to achieve. The algorithm terminates when either the relative or the absolute residual is below `tol`. maxiter : integer Maximum number of iterations. Iteration will stop after maxiter steps even if the specified tolerance has not been achieved. M : {sparse matrix, dense matrix, LinearOperator} Preconditioner for A. The preconditioner should approximate the inverse of A. Effective preconditioning dramatically improves the rate of convergence, which implies that fewer iterations are needed to reach a given error tolerance. callback : function User-supplied function to call after each iteration. It is called as callback(xk), where xk is the current solution vector. Additional parameters --------------------- inner_m : int, optional Number of inner GMRES iterations per each outer iteration. outer_k : int, optional Number of vectors to carry between inner GMRES iterations. According to [BJM]_, good values are in the range of 1...3. However, note that if you want to use the additional vectors to accelerate solving multiple similar problems, larger values may be beneficial. outer_v : list of tuples, optional List containing tuples ``(v, Av)`` of vectors and corresponding matrix-vector products, used to augment the Krylov subspace, and carried between inner GMRES iterations. The element ``Av`` can be `None` if the matrix-vector product should be re-evaluated. This parameter is modified in-place by `lgmres`, and can be used to pass "guess" vectors in and out of the algorithm when solving similar problems. store_outer_Av : bool, optional Whether LGMRES should store also A*v in addition to vectors `v` in the `outer_v` list. Default is True. Returns ------- x : array or matrix The converged solution. info : integer Provides convergence information: 0 : successful exit >0 : convergence to tolerance not achieved, number of iterations <0 : illegal input or breakdown Notes ----- The LGMRES algorithm [BJM]_ [BPh]_ is designed to avoid the slowing of convergence in restarted GMRES, due to alternating residual vectors. Typically, it often outperforms GMRES(m) of comparable memory requirements by some measure, or at least is not much worse. Another advantage in this algorithm is that you can supply it with 'guess' vectors in the `outer_v` argument that augment the Krylov subspace. If the solution lies close to the span of these vectors, the algorithm converges faster. This can be useful if several very similar matrices need to be inverted one after another, such as in Newton-Krylov iteration where the Jacobian matrix often changes little in the nonlinear steps. References ---------- .. [BJM] A.H. Baker and E.R. Jessup and T. Manteuffel, SIAM J. Matrix Anal. Appl. 26, 962 (2005). .. [BPh] A.H. Baker, PhD thesis, University of Colorado (2003). http://amath.colorado.edu/activities/thesis/allisonb/Thesis.ps """ from scipy.linalg.basic import lstsq A, M, x, b, postprocess = make_system(A, M, x0, b) if not np.isfinite(b).all(): raise ValueError("RHS must contain only finite numbers") matvec = A.matvec psolve = M.matvec if outer_v is None: outer_v = [] axpy, dotc, scal = None, None, None b_norm = norm2(b) if b_norm == 0: b_norm = 1 for k_outer in xrange(maxiter): r_outer = matvec(x) - b # -- callback if callback is not None: callback(x) # -- determine input type routines if axpy is None: if np.iscomplexobj(r_outer) and not np.iscomplexobj(x): x = x.astype(r_outer.dtype) axpy, dotc, scal = blas.get_blas_funcs(['axpy', 'dotc', 'scal'], (x, r_outer)) # -- check stopping condition r_norm = norm2(r_outer) if r_norm < tol * b_norm or r_norm < tol: break # -- inner LGMRES iteration vs0 = -psolve(r_outer) inner_res_0 = norm2(vs0) if inner_res_0 == 0: rnorm = norm2(r_outer) raise RuntimeError("Preconditioner returned a zero vector; " "|v| ~ %.1g, |M v| = 0" % rnorm) vs0 = scal(1.0 / inner_res_0, vs0) hs = [] vs = [vs0] ws = [] y = None for j in xrange(1, 1 + inner_m + len(outer_v)): # -- Arnoldi process: # # Build an orthonormal basis V and matrices W and H such that # A W = V H # Columns of W, V, and H are stored in `ws`, `vs` and `hs`. # # The first column of V is always the residual vector, `vs0`; # V has *one more column* than the other of the three matrices. # # The other columns in V are built by feeding in, one # by one, some vectors `z` and orthonormalizing them # against the basis so far. The trick here is to # feed in first some augmentation vectors, before # starting to construct the Krylov basis on `v0`. # # It was shown in [BJM]_ that a good choice (the LGMRES choice) # for these augmentation vectors are the `dx` vectors obtained # from a couple of the previous restart cycles. # # Note especially that while `vs0` is always the first # column in V, there is no reason why it should also be # the first column in W. (In fact, below `vs0` comes in # W only after the augmentation vectors.) # # The rest of the algorithm then goes as in GMRES, one # solves a minimization problem in the smaller subspace # spanned by W (range) and V (image). # # XXX: Below, I'm lazy and use `lstsq` to solve the # small least squares problem. Performance-wise, this # is in practice acceptable, but it could be nice to do # it on the fly with Givens etc. # # ++ evaluate v_new = None if j < len(outer_v) + 1: z, v_new = outer_v[j - 1] elif j == len(outer_v) + 1: z = vs0 else: z = vs[-1] if v_new is None: v_new = psolve(matvec(z)) else: # Note: v_new is modified in-place below. Must make a # copy to ensure that the outer_v vectors are not # clobbered. v_new = v_new.copy() # ++ orthogonalize hcur = [] for v in vs: alpha = dotc(v, v_new) hcur.append(alpha) v_new = axpy(v, v_new, v.shape[0], -alpha) # v_new -= alpha*v hcur.append(norm2(v_new)) if hcur[-1] == 0: # Exact solution found; bail out. # Zero basis vector (v_new) in the least-squares problem # does no harm, so we can just use the same code as usually; # it will give zero (inner) residual as a result. bailout = True else: bailout = False v_new = scal(1.0 / hcur[-1], v_new) vs.append(v_new) hs.append(hcur) ws.append(z) # XXX: Ugly: should implement the GMRES iteration properly, # with Givens rotations and not using lstsq. Instead, we # spare some work by solving the LSQ problem only every 5 # iterations. if not bailout and j % 5 != 1 and j < inner_m + len(outer_v) - 1: continue # -- GMRES optimization problem hess = np.zeros((j + 1, j), x.dtype) e1 = np.zeros((j + 1, ), x.dtype) e1[0] = inner_res_0 for q in xrange(j): hess[:(q + 2), q] = hs[q] y, resids, rank, s = lstsq(hess, e1) inner_res = norm2(np.dot(hess, y) - e1) # -- check for termination if inner_res < tol * inner_res_0: break # -- GMRES terminated: eval solution dx = ws[0] * y[0] for w, yc in zip(ws[1:], y[1:]): dx = axpy(w, dx, dx.shape[0], yc) # dx += w*yc # -- Store LGMRES augmentation vectors nx = norm2(dx) if store_outer_Av: q = np.dot(hess, y) ax = vs[0] * q[0] for v, qc in zip(vs[1:], q[1:]): ax = axpy(v, ax, ax.shape[0], qc) outer_v.append((dx / nx, ax / nx)) else: outer_v.append((dx / nx, None)) # -- Retain only a finite number of augmentation vectors while len(outer_v) > outer_k: del outer_v[0] # -- Apply step x += dx else: # didn't converge ... return postprocess(x), maxiter return postprocess(x), 0
def qmr(A, b, x0=None, tol=1e-5, maxiter=None, xtype=None, M1=None, M2=None, callback=None): """Use Quasi-Minimal Residual iteration to solve A x = b Parameters ---------- A : {sparse matrix, dense matrix, LinearOperator} The N-by-N matrix of the linear system. b : {array, matrix} Right hand side of the linear system. Has shape (N,) or (N,1). Returns ------- x : {array, matrix} The converged solution. info : integer Provides convergence information: 0 : successful exit >0 : convergence to tolerance not achieved, number of iterations <0 : illegal input or breakdown Other Parameters ---------------- x0 : {array, matrix} Starting guess for the solution. tol : float Tolerance to achieve. The algorithm terminates when either the relative or the absolute residual is below `tol`. maxiter : integer Maximum number of iterations. Iteration will stop after maxiter steps even if the specified tolerance has not been achieved. M1 : {sparse matrix, dense matrix, LinearOperator} Left preconditioner for A. M2 : {sparse matrix, dense matrix, LinearOperator} Right preconditioner for A. Used together with the left preconditioner M1. The matrix M1*A*M2 should have better conditioned than A alone. callback : function User-supplied function to call after each iteration. It is called as callback(xk), where xk is the current solution vector. xtype : {'f','d','F','D'} This parameter is DEPRECATED -- avoid using it. The type of the result. If None, then it will be determined from A.dtype.char and b. If A does not have a typecode method then it will compute A.matvec(x0) to get a typecode. To save the extra computation when A does not have a typecode attribute use xtype=0 for the same type as b or use xtype='f','d','F',or 'D'. This parameter has been superceeded by LinearOperator. See Also -------- LinearOperator """ A_ = A A, M, x, b, postprocess = make_system(A, None, x0, b, xtype) if M1 is None and M2 is None: if hasattr(A_, 'psolve'): def left_psolve(b): return A_.psolve(b, 'left') def right_psolve(b): return A_.psolve(b, 'right') def left_rpsolve(b): return A_.rpsolve(b, 'left') def right_rpsolve(b): return A_.rpsolve(b, 'right') M1 = LinearOperator(A.shape, matvec=left_psolve, rmatvec=left_rpsolve) M2 = LinearOperator(A.shape, matvec=right_psolve, rmatvec=right_rpsolve) else: def id(b): return b M1 = LinearOperator(A.shape, matvec=id, rmatvec=id) M2 = LinearOperator(A.shape, matvec=id, rmatvec=id) n = len(b) if maxiter is None: maxiter = n * 10 ltr = _type_conv[x.dtype.char] revcom = getattr(_iterative, ltr + 'qmrrevcom') stoptest = getattr(_iterative, ltr + 'stoptest2') resid = tol ndx1 = 1 ndx2 = -1 work = np.zeros(11 * n, x.dtype) ijob = 1 info = 0 ftflag = True bnrm2 = -1.0 iter_ = maxiter while True: olditer = iter_ x, iter_, resid, info, ndx1, ndx2, sclr1, sclr2, ijob = \ revcom(b, x, work, iter_, resid, info, ndx1, ndx2, ijob) if callback is not None and iter_ > olditer: callback(x) slice1 = slice(ndx1 - 1, ndx1 - 1 + n) slice2 = slice(ndx2 - 1, ndx2 - 1 + n) if (ijob == -1): if callback is not None: callback(x) break elif (ijob == 1): work[slice2] *= sclr2 work[slice2] += sclr1 * A.matvec(work[slice1]) elif (ijob == 2): work[slice2] *= sclr2 work[slice2] += sclr1 * A.rmatvec(work[slice1]) elif (ijob == 3): work[slice1] = M1.matvec(work[slice2]) elif (ijob == 4): work[slice1] = M2.matvec(work[slice2]) elif (ijob == 5): work[slice1] = M1.rmatvec(work[slice2]) elif (ijob == 6): work[slice1] = M2.rmatvec(work[slice2]) elif (ijob == 7): work[slice2] *= sclr2 work[slice2] += sclr1 * A.matvec(x) elif (ijob == 8): if ftflag: info = -1 ftflag = False bnrm2, resid, info = stoptest(work[slice1], b, bnrm2, tol, info) ijob = 2 if info > 0 and iter_ == maxiter and resid > tol: #info isn't set appropriately otherwise info = iter_ return postprocess(x), info
def gmres(A, b, x0=None, tol=1e-5, restart=None, maxiter=None, xtype=None, M=None, callback=None, restrt=None): """ Use Generalized Minimal RESidual iteration to solve A x = b. Parameters ---------- A : {sparse matrix, dense matrix, LinearOperator} The N-by-N matrix of the linear system. b : {array, matrix} Right hand side of the linear system. Has shape (N,) or (N,1). Returns ------- x : {array, matrix} The converged solution. info : int Provides convergence information: * 0 : successful exit * >0 : convergence to tolerance not achieved, number of iterations * <0 : illegal input or breakdown Other parameters ---------------- x0 : {array, matrix} Starting guess for the solution (a vector of zeros by default). tol : float Tolerance to achieve. The algorithm terminates when either the relative or the absolute residual is below `tol`. restart : int, optional Number of iterations between restarts. Larger values increase iteration cost, but may be necessary for convergence. Default is 20. maxiter : int, optional Maximum number of iterations. Iteration will stop after maxiter steps even if the specified tolerance has not been achieved. M : {sparse matrix, dense matrix, LinearOperator} Inverse of the preconditioner of A. M should approximate the inverse of A and be easy to solve for (see Notes). Effective preconditioning dramatically improves the rate of convergence, which implies that fewer iterations are needed to reach a given error tolerance. By default, no preconditioner is used. callback : function User-supplied function to call after each iteration. It is called as callback(rk), where rk is the current residual vector. See Also -------- LinearOperator Notes ----- A preconditioner, P, is chosen such that P is close to A but easy to solve for. The preconditioner parameter required by this routine is ``M = P^-1``. The inverse should preferably not be calculated explicitly. Rather, use the following template to produce M:: # Construct a linear operator that computes P^-1 * x. import scipy.sparse.linalg as spla M_x = lambda x: spla.spsolve(P, x) M = spla.LinearOperator((n, n), M_x) Deprecated Parameters --------------------- xtype : {'f','d','F','D'} This parameter is DEPRECATED --- avoid using it. The type of the result. If None, then it will be determined from A.dtype.char and b. If A does not have a typecode method then it will compute A.matvec(x0) to get a typecode. To save the extra computation when A does not have a typecode attribute use xtype=0 for the same type as b or use xtype='f','d','F',or 'D'. This parameter has been superceeded by LinearOperator. See Also -------- LinearOperator """ # Change 'restrt' keyword to 'restart' if restrt is None: restrt = restart elif restart is not None: raise ValueError("Cannot specify both restart and restrt keywords. " "Preferably use 'restart' only.") A, M, x, b, postprocess = make_system(A, M, x0, b, xtype) n = len(b) if maxiter is None: maxiter = n * 10 if restrt is None: restrt = 20 restrt = min(restrt, n) matvec = A.matvec psolve = M.matvec ltr = _type_conv[x.dtype.char] revcom = getattr(_iterative, ltr + 'gmresrevcom') stoptest = getattr(_iterative, ltr + 'stoptest2') resid = tol ndx1 = 1 ndx2 = -1 work = np.zeros((6 + restrt) * n, dtype=x.dtype) work2 = np.zeros((restrt + 1) * (2 * restrt + 2), dtype=x.dtype) ijob = 1 info = 0 ftflag = True bnrm2 = -1.0 iter_ = maxiter old_ijob = ijob first_pass = True resid_ready = False iter_num = 1 while True: olditer = iter_ x, iter_, resid, info, ndx1, ndx2, sclr1, sclr2, ijob = \ revcom(b, x, restrt, work, work2, iter_, resid, info, ndx1, ndx2, ijob) #if callback is not None and iter_ > olditer: # callback(x) slice1 = slice(ndx1 - 1, ndx1 - 1 + n) slice2 = slice(ndx2 - 1, ndx2 - 1 + n) if (ijob == -1): # gmres success, update last residual if resid_ready and callback is not None: callback(resid) resid_ready = False break elif (ijob == 1): work[slice2] *= sclr2 work[slice2] += sclr1 * matvec(x) elif (ijob == 2): work[slice1] = psolve(work[slice2]) if not first_pass and old_ijob == 3: resid_ready = True first_pass = False elif (ijob == 3): work[slice2] *= sclr2 work[slice2] += sclr1 * matvec(work[slice1]) if resid_ready and callback is not None: callback(resid) resid_ready = False iter_num = iter_num + 1 elif (ijob == 4): if ftflag: info = -1 ftflag = False bnrm2, resid, info = stoptest(work[slice1], b, bnrm2, tol, info) old_ijob = ijob ijob = 2 if iter_num > maxiter: break if info >= 0 and resid > tol: #info isn't set appropriately otherwise info = maxiter return postprocess(x), info
def qmr(A, b, x0=None, tol=1e-5, maxiter=None, xtype=None, M1=None, M2=None, callback=None): """Use Quasi-Minimal Residual iteration to solve A x = b Parameters ---------- A : {sparse matrix, dense matrix, LinearOperator} The N-by-N matrix of the linear system. b : {array, matrix} Right hand side of the linear system. Has shape (N,) or (N,1). Returns ------- x : {array, matrix} The converged solution. info : integer Provides convergence information: 0 : successful exit >0 : convergence to tolerance not achieved, number of iterations <0 : illegal input or breakdown Other Parameters ---------------- x0 : {array, matrix} Starting guess for the solution. tol : float Tolerance to achieve. The algorithm terminates when either the relative or the absolute residual is below `tol`. maxiter : integer Maximum number of iterations. Iteration will stop after maxiter steps even if the specified tolerance has not been achieved. M1 : {sparse matrix, dense matrix, LinearOperator} Left preconditioner for A. M2 : {sparse matrix, dense matrix, LinearOperator} Right preconditioner for A. Used together with the left preconditioner M1. The matrix M1*A*M2 should have better conditioned than A alone. callback : function User-supplied function to call after each iteration. It is called as callback(xk), where xk is the current solution vector. xtype : {'f','d','F','D'} This parameter is DEPRECATED -- avoid using it. The type of the result. If None, then it will be determined from A.dtype.char and b. If A does not have a typecode method then it will compute A.matvec(x0) to get a typecode. To save the extra computation when A does not have a typecode attribute use xtype=0 for the same type as b or use xtype='f','d','F',or 'D'. This parameter has been superceeded by LinearOperator. See Also -------- LinearOperator """ A_ = A A,M,x,b,postprocess = make_system(A,None,x0,b,xtype) if M1 is None and M2 is None: if hasattr(A_,'psolve'): def left_psolve(b): return A_.psolve(b,'left') def right_psolve(b): return A_.psolve(b,'right') def left_rpsolve(b): return A_.rpsolve(b,'left') def right_rpsolve(b): return A_.rpsolve(b,'right') M1 = LinearOperator(A.shape, matvec=left_psolve, rmatvec=left_rpsolve) M2 = LinearOperator(A.shape, matvec=right_psolve, rmatvec=right_rpsolve) else: def id(b): return b M1 = LinearOperator(A.shape, matvec=id, rmatvec=id) M2 = LinearOperator(A.shape, matvec=id, rmatvec=id) n = len(b) if maxiter is None: maxiter = n*10 ltr = _type_conv[x.dtype.char] revcom = getattr(_iterative, ltr + 'qmrrevcom') stoptest = getattr(_iterative, ltr + 'stoptest2') resid = tol ndx1 = 1 ndx2 = -1 work = np.zeros(11*n,x.dtype) ijob = 1 info = 0 ftflag = True bnrm2 = -1.0 iter_ = maxiter while True: olditer = iter_ x, iter_, resid, info, ndx1, ndx2, sclr1, sclr2, ijob = \ revcom(b, x, work, iter_, resid, info, ndx1, ndx2, ijob) if callback is not None and iter_ > olditer: callback(x) slice1 = slice(ndx1-1, ndx1-1+n) slice2 = slice(ndx2-1, ndx2-1+n) if (ijob == -1): if callback is not None: callback(x) break elif (ijob == 1): work[slice2] *= sclr2 work[slice2] += sclr1*A.matvec(work[slice1]) elif (ijob == 2): work[slice2] *= sclr2 work[slice2] += sclr1*A.rmatvec(work[slice1]) elif (ijob == 3): work[slice1] = M1.matvec(work[slice2]) elif (ijob == 4): work[slice1] = M2.matvec(work[slice2]) elif (ijob == 5): work[slice1] = M1.rmatvec(work[slice2]) elif (ijob == 6): work[slice1] = M2.rmatvec(work[slice2]) elif (ijob == 7): work[slice2] *= sclr2 work[slice2] += sclr1*A.matvec(x) elif (ijob == 8): if ftflag: info = -1 ftflag = False bnrm2, resid, info = stoptest(work[slice1], b, bnrm2, tol, info) ijob = 2 if info > 0 and iter_ == maxiter and resid > tol: #info isn't set appropriately otherwise info = iter_ return postprocess(x), info
def gmres(A, b, x0=None, tol=1e-5, restart=None, maxiter=None, xtype=None, M=None, callback=None, restrt=None): """Use Generalized Minimal RESidual iteration to solve A x = b Parameters ---------- A : {sparse matrix, dense matrix, LinearOperator} The N-by-N matrix of the linear system. b : {array, matrix} Right hand side of the linear system. Has shape (N,) or (N,1). Returns ------- x : {array, matrix} The converged solution. info : integer Provides convergence information: 0 : successful exit >0 : convergence to tolerance not achieved, number of iterations <0 : illegal input or breakdown Other Parameters ---------------- x0 : {array, matrix} Starting guess for the solution. tol : float Tolerance to achieve. The algorithm terminates when either the relative or the absolute residual is below `tol`. restart : integer Number of iterations between restarts. Larger values increase iteration cost, but may be necessary for convergence. (Default: 20) maxiter : integer, optional Maximum number of iterations. Iteration will stop after maxiter steps even if the specified tolerance has not been achieved. M : {sparse matrix, dense matrix, LinearOperator} Preconditioner for A. The preconditioner should approximate the inverse of A. Effective preconditioning dramatically improves the rate of convergence, which implies that fewer iterations are needed to reach a given error tolerance. callback : function User-supplied function to call after each iteration. It is called as callback(rk), where rk is the current residual vector. xtype : {'f','d','F','D'} This parameter is DEPRECATED --- avoid using it. The type of the result. If None, then it will be determined from A.dtype.char and b. If A does not have a typecode method then it will compute A.matvec(x0) to get a typecode. To save the extra computation when A does not have a typecode attribute use xtype=0 for the same type as b or use xtype='f','d','F',or 'D'. This parameter has been superceeded by LinearOperator. See Also -------- LinearOperator """ # Change 'restrt' keyword to 'restart' if restrt is None: restrt = restart elif restart is not None: raise ValueError("Cannot specify both restart and restrt keywords. " "Preferably use 'restart' only.") A,M,x,b,postprocess = make_system(A,M,x0,b,xtype) n = len(b) if maxiter is None: maxiter = n*10 if restrt is None: restrt = 20 restrt = min(restrt, n) matvec = A.matvec psolve = M.matvec ltr = _type_conv[x.dtype.char] revcom = getattr(_iterative, ltr + 'gmresrevcom') stoptest = getattr(_iterative, ltr + 'stoptest2') resid = tol ndx1 = 1 ndx2 = -1 work = np.zeros((6+restrt)*n,dtype=x.dtype) work2 = np.zeros((restrt+1)*(2*restrt+2),dtype=x.dtype) ijob = 1 info = 0 ftflag = True bnrm2 = -1.0 iter_ = maxiter old_ijob = ijob first_pass = True resid_ready = False iter_num = 1 while True: olditer = iter_ x, iter_, resid, info, ndx1, ndx2, sclr1, sclr2, ijob = \ revcom(b, x, restrt, work, work2, iter_, resid, info, ndx1, ndx2, ijob) #if callback is not None and iter_ > olditer: # callback(x) slice1 = slice(ndx1-1, ndx1-1+n) slice2 = slice(ndx2-1, ndx2-1+n) if (ijob == -1): # gmres success, update last residual if resid_ready and callback is not None: callback(resid) resid_ready = False break elif (ijob == 1): work[slice2] *= sclr2 work[slice2] += sclr1*matvec(x) elif (ijob == 2): work[slice1] = psolve(work[slice2]) if not first_pass and old_ijob==3: resid_ready = True first_pass = False elif (ijob == 3): work[slice2] *= sclr2 work[slice2] += sclr1*matvec(work[slice1]) if resid_ready and callback is not None: callback(resid) resid_ready = False iter_num = iter_num+1 elif (ijob == 4): if ftflag: info = -1 ftflag = False bnrm2, resid, info = stoptest(work[slice1], b, bnrm2, tol, info) old_ijob = ijob ijob = 2 if iter_num > maxiter: break if info >= 0 and resid > tol: #info isn't set appropriately otherwise info = maxiter return postprocess(x), info
def minres(A, b, x0=None, shift=0.0, tol=1e-5, maxiter=None, xtype=None, M=None, callback=None, show=False, check=False): A, M, x, b, postprocess = make_system(A, M, x0, b, xtype) matvec = A.matvec psolve = M.matvec first = 'Enter minres. ' last = 'Exit minres. ' n = A.shape[0] if maxiter is None: maxiter = 5 * n msg = [ ' beta2 = 0. If M = I, b and x are eigenvectors ', # -1 ' beta1 = 0. The exact solution is x = 0 ', # 0 ' A solution to Ax = b was found, given rtol ', # 1 ' A least-squares solution was found, given rtol ', # 2 ' Reasonable accuracy achieved, given eps ', # 3 ' x has converged to an eigenvector ', # 4 ' acond has exceeded 0.1/eps ', # 5 ' The iteration limit was reached ', # 6 ' A does not define a symmetric matrix ', # 7 ' M does not define a symmetric matrix ', # 8 ' M does not define a pos-def preconditioner ' ] # 9 if show: print first + 'Solution of symmetric Ax = b' print first + 'n = %3g shift = %23.14e' % (n, shift) print first + 'itnlim = %3g rtol = %11.2e' % (maxiter, tol) print istop = 0 itn = 0 Anorm = 0 Acond = 0 rnorm = 0 ynorm = 0 xtype = x.dtype eps = finfo(xtype).eps x = zeros(n, dtype=xtype) # Set up y and v for the first Lanczos vector v1. # y = beta1 P' v1, where P = C**(-1). # v is really P' v1. y = b r1 = b y = psolve(b) beta1 = inner(b, y) if beta1 < 0: raise ValueError('indefinite preconditioner') elif beta1 == 0: return (postprocess(x), 0) beta1 = sqrt(beta1) if check: # are these too strict? # see if A is symmetric w = matvec(y) r2 = matvec(w) s = inner(w, w) t = inner(y, r2) z = abs(s - t) epsa = (s + eps) * eps**(1.0 / 3.0) if z > epsa: raise ValueError('non-symmetric matrix') # see if M is symmetric r2 = psolve(y) s = inner(y, y) t = inner(r1, r2) z = abs(s - t) epsa = (s + eps) * eps**(1.0 / 3.0) if z > epsa: raise ValueError('non-symmetric preconditioner') # Initialize other quantities oldb = 0 beta = beta1 dbar = 0 epsln = 0 qrnorm = beta1 phibar = beta1 rhs1 = beta1 rhs2 = 0 tnorm2 = 0 ynorm2 = 0 cs = -1 sn = 0 w = zeros(n, dtype=xtype) w2 = zeros(n, dtype=xtype) r2 = r1 if show: print print print ' Itn x(1) Compatible LS norm(A) cond(A) gbar/|A|' while itn < maxiter: itn += 1 s = 1.0 / beta v = s * y y = matvec(v) y = y - shift * v if itn >= 2: y = y - (beta / oldb) * r1 alfa = inner(v, y) y = y - (alfa / beta) * r2 r1 = r2 r2 = y y = psolve(r2) oldb = beta beta = inner(r2, y) if beta < 0: raise ValueError('non-symmetric matrix') beta = sqrt(beta) tnorm2 += alfa**2 + oldb**2 + beta**2 if itn == 1: if beta / beta1 <= 10 * eps: istop = -1 # Terminate later #tnorm2 = alfa**2 ?? gmax = abs(alfa) gmin = gmax # Apply previous rotation Qk-1 to get # [deltak epslnk+1] = [cs sn][dbark 0 ] # [gbar k dbar k+1] [sn -cs][alfak betak+1]. oldeps = epsln delta = cs * dbar + sn * alfa # delta1 = 0 deltak gbar = sn * dbar - cs * alfa # gbar 1 = alfa1 gbar k epsln = sn * beta # epsln2 = 0 epslnk+1 dbar = -cs * beta # dbar 2 = beta2 dbar k+1 root = norm([gbar, dbar]) Arnorm = phibar * root # Compute the next plane rotation Qk gamma = norm([gbar, beta]) # gammak gamma = max(gamma, eps) cs = gbar / gamma # ck sn = beta / gamma # sk phi = cs * phibar # phik phibar = sn * phibar # phibark+1 # Update x. denom = 1.0 / gamma w1 = w2 w2 = w w = (v - oldeps * w1 - delta * w2) * denom x = x + phi * w # Go round again. gmax = max(gmax, gamma) gmin = min(gmin, gamma) z = rhs1 / gamma ynorm2 = z**2 + ynorm2 rhs1 = rhs2 - delta * z rhs2 = -epsln * z # Estimate various norms and test for convergence. Anorm = sqrt(tnorm2) ynorm = sqrt(ynorm2) epsa = Anorm * eps epsx = Anorm * ynorm * eps epsr = Anorm * ynorm * tol diag = gbar if diag == 0: diag = epsa qrnorm = phibar rnorm = qrnorm test1 = rnorm / (Anorm * ynorm) # ||r|| / (||A|| ||x||) test2 = root / Anorm # ||Ar|| / (||A|| ||r||) # Estimate cond(A). # In this version we look at the diagonals of R in the # factorization of the lower Hessenberg matrix, Q * H = R, # where H is the tridiagonal matrix from Lanczos with one # extra row, beta(k+1) e_k^T. Acond = gmax / gmin # See if any of the stopping criteria are satisfied. # In rare cases, istop is already -1 from above (Abar = const*I). if istop == 0: t1 = 1 + test1 # These tests work if tol < eps t2 = 1 + test2 if t2 <= 1: istop = 2 if t1 <= 1: istop = 1 if itn >= maxiter: istop = 6 if Acond >= 0.1 / eps: istop = 4 if epsx >= beta1: istop = 3 #if rnorm <= epsx : istop = 2 #if rnorm <= epsr : istop = 1 if test2 <= tol: istop = 2 if test1 <= tol: istop = 1 # See if it is time to print something. prnt = False if n <= 40: prnt = True if itn <= 10: prnt = True if itn >= maxiter - 10: prnt = True if itn % 10 == 0: prnt = True if qrnorm <= 10 * epsx: prnt = True if qrnorm <= 10 * epsr: prnt = True if Acond <= 1e-2 / eps: prnt = True if istop != 0: prnt = True if show and prnt: str1 = '%6g %12.5e %10.3e' % (itn, x[0], test1) str2 = ' %10.3e' % (test2, ) str3 = ' %8.1e %8.1e %8.1e' % (Anorm, Acond, gbar / Anorm) print str1 + str2 + str3 if itn % 10 == 0: print if callback is not None: callback(x) if istop != 0: break #TODO check this if show: print print last + ' istop = %3g itn =%5g' % (istop, itn) print last + ' Anorm = %12.4e Acond = %12.4e' % (Anorm, Acond) print last + ' rnorm = %12.4e ynorm = %12.4e' % (rnorm, ynorm) print last + ' Arnorm = %12.4e' % (Arnorm, ) print last + msg[istop + 1] if istop == 6: info = maxiter else: info = 0 return (postprocess(x), info)
def gmres(A, b, x0=None, tol=1e-5, restrt=20, maxiter=None, xtype=None, M=None, callback=None): """Use Generalized Minimal RESidual iteration to solve A x = b Parameters ---------- A : {sparse matrix, dense matrix, LinearOperator} The N-by-N matrix of the linear system. b : {array, matrix} Right hand side of the linear system. Has shape (N,) or (N,1). Optional Parameters ------------------- x0 : {array, matrix} Starting guess for the solution. tol : float Relative tolerance to achieve before terminating. restrt : integer Number of iterations between restarts. Larger values increase iteration cost, but may be necessary for convergence. maxiter : integer Maximum number of iterations. Iteration will stop after maxiter steps even if the specified tolerance has not been achieved. M : {sparse matrix, dense matrix, LinearOperator} Preconditioner for A. The preconditioner should approximate the inverse of A. Effective preconditioning dramatically improves the rate of convergence, which implies that fewer iterations are needed to reach a given error tolerance. callback : function User-supplied function to call after each iteration. It is called as callback(rk), where rk is the current residual vector. Outputs ------- x : {array, matrix} The converged solution. info : integer Provides convergence information: 0 : successful exit >0 : convergence to tolerance not achieved, number of iterations <0 : illegal input or breakdown See Also -------- LinearOperator Deprecated Parameters --------------------- xtype : {'f','d','F','D'} The type of the result. If None, then it will be determined from A.dtype.char and b. If A does not have a typecode method then it will compute A.matvec(x0) to get a typecode. To save the extra computation when A does not have a typecode attribute use xtype=0 for the same type as b or use xtype='f','d','F',or 'D'. This parameter has been superceeded by LinearOperator. """ A,M,x,b,postprocess = make_system(A,M,x0,b,xtype) n = len(b) if maxiter is None: maxiter = n*10 restrt = min(restrt, n) matvec = A.matvec psolve = M.matvec ltr = _type_conv[x.dtype.char] revcom = getattr(_iterative, ltr + 'gmresrevcom') stoptest = getattr(_iterative, ltr + 'stoptest2') resid = tol ndx1 = 1 ndx2 = -1 work = np.zeros((6+restrt)*n,dtype=x.dtype) work2 = np.zeros((restrt+1)*(2*restrt+2),dtype=x.dtype) ijob = 1 info = 0 ftflag = True bnrm2 = -1.0 iter_ = maxiter old_ijob = ijob first_pass = True resid_ready = False iter_num = 1 while True: olditer = iter_ x, iter_, resid, info, ndx1, ndx2, sclr1, sclr2, ijob = \ revcom(b, x, restrt, work, work2, iter_, resid, info, ndx1, ndx2, ijob) #if callback is not None and iter_ > olditer: # callback(x) slice1 = slice(ndx1-1, ndx1-1+n) slice2 = slice(ndx2-1, ndx2-1+n) if (ijob == -1): # gmres success, update last residual if resid_ready and callback is not None: callback(resid) resid_ready = False break elif (ijob == 1): work[slice2] *= sclr2 work[slice2] += sclr1*matvec(x) elif (ijob == 2): work[slice1] = psolve(work[slice2]) if not first_pass and old_ijob==3: resid_ready = True first_pass = False elif (ijob == 3): work[slice2] *= sclr2 work[slice2] += sclr1*matvec(work[slice1]) if resid_ready and callback is not None: callback(resid) resid_ready = False iter_num = iter_num+1 elif (ijob == 4): if ftflag: info = -1 ftflag = False bnrm2, resid, info = stoptest(work[slice1], b, bnrm2, tol, info) old_ijob = ijob ijob = 2 if iter_num > maxiter: break if info >= 0 and resid > tol: #info isn't set appropriately otherwise info = maxiter return postprocess(x), info
def minres(A, b, x0=None, shift=0.0, tol=1e-5, maxiter=None, xtype=None, M=None, callback=None, show=False, check=False): A,M,x,b,postprocess = make_system(A,M,x0,b,xtype) matvec = A.matvec psolve = M.matvec first = 'Enter minres. ' last = 'Exit minres. ' n = A.shape[0] if maxiter is None: maxiter = 5 * n msg =[' beta2 = 0. If M = I, b and x are eigenvectors ', # -1 ' beta1 = 0. The exact solution is x = 0 ', # 0 ' A solution to Ax = b was found, given rtol ', # 1 ' A least-squares solution was found, given rtol ', # 2 ' Reasonable accuracy achieved, given eps ', # 3 ' x has converged to an eigenvector ', # 4 ' acond has exceeded 0.1/eps ', # 5 ' The iteration limit was reached ', # 6 ' A does not define a symmetric matrix ', # 7 ' M does not define a symmetric matrix ', # 8 ' M does not define a pos-def preconditioner '] # 9 if show: print first + 'Solution of symmetric Ax = b' print first + 'n = %3g shift = %23.14e' % (n,shift) print first + 'itnlim = %3g rtol = %11.2e' % (maxiter,tol) print istop = 0; itn = 0; Anorm = 0; Acond = 0; rnorm = 0; ynorm = 0; xtype = x.dtype eps = finfo(xtype).eps x = zeros( n, dtype=xtype ) # Set up y and v for the first Lanczos vector v1. # y = beta1 P' v1, where P = C**(-1). # v is really P' v1. y = b r1 = b y = psolve(b) beta1 = inner(b,y) if beta1 < 0: raise ValueError('indefinite preconditioner') elif beta1 == 0: return (postprocess(x), 0) beta1 = sqrt( beta1 ) if check: # are these too strict? # see if A is symmetric w = matvec(y) r2 = matvec(w) s = inner(w,w) t = inner(y,r2) z = abs( s - t ) epsa = (s + eps) * eps**(1.0/3.0) if z > epsa: raise ValueError('non-symmetric matrix') # see if M is symmetric r2 = psolve(y) s = inner(y,y) t = inner(r1,r2) z = abs( s - t ) epsa = (s + eps) * eps**(1.0/3.0) if z > epsa: raise ValueError('non-symmetric preconditioner') # Initialize other quantities oldb = 0; beta = beta1; dbar = 0; epsln = 0; qrnorm = beta1; phibar = beta1; rhs1 = beta1; rhs2 = 0; tnorm2 = 0; ynorm2 = 0; cs = -1; sn = 0; w = zeros(n, dtype=xtype) w2 = zeros(n, dtype=xtype) r2 = r1 if show: print print print ' Itn x(1) Compatible LS norm(A) cond(A) gbar/|A|' while itn < maxiter: itn += 1 s = 1.0/beta v = s*y y = matvec(v) y = y - shift * v if itn >= 2: y = y - (beta/oldb)*r1 alfa = inner(v,y) y = y - (alfa/beta)*r2 r1 = r2 r2 = y y = psolve(r2) oldb = beta beta = inner(r2,y) if beta < 0: raise ValueError('non-symmetric matrix') beta = sqrt(beta) tnorm2 += alfa**2 + oldb**2 + beta**2 if itn == 1: if beta/beta1 <= 10*eps: istop = -1 # Terminate later #tnorm2 = alfa**2 ?? gmax = abs(alfa) gmin = gmax # Apply previous rotation Qk-1 to get # [deltak epslnk+1] = [cs sn][dbark 0 ] # [gbar k dbar k+1] [sn -cs][alfak betak+1]. oldeps = epsln delta = cs * dbar + sn * alfa # delta1 = 0 deltak gbar = sn * dbar - cs * alfa # gbar 1 = alfa1 gbar k epsln = sn * beta # epsln2 = 0 epslnk+1 dbar = - cs * beta # dbar 2 = beta2 dbar k+1 root = norm([gbar, dbar]) Arnorm = phibar * root # Compute the next plane rotation Qk gamma = norm([gbar, beta]) # gammak gamma = max(gamma, eps) cs = gbar / gamma # ck sn = beta / gamma # sk phi = cs * phibar # phik phibar = sn * phibar # phibark+1 # Update x. denom = 1.0/gamma w1 = w2 w2 = w w = (v - oldeps*w1 - delta*w2) * denom x = x + phi*w # Go round again. gmax = max(gmax, gamma) gmin = min(gmin, gamma) z = rhs1 / gamma ynorm2 = z**2 + ynorm2 rhs1 = rhs2 - delta*z rhs2 = - epsln*z # Estimate various norms and test for convergence. Anorm = sqrt( tnorm2 ) ynorm = sqrt( ynorm2 ) epsa = Anorm * eps epsx = Anorm * ynorm * eps epsr = Anorm * ynorm * tol diag = gbar if diag == 0: diag = epsa qrnorm = phibar rnorm = qrnorm test1 = rnorm / (Anorm*ynorm) # ||r|| / (||A|| ||x||) test2 = root / Anorm # ||Ar|| / (||A|| ||r||) # Estimate cond(A). # In this version we look at the diagonals of R in the # factorization of the lower Hessenberg matrix, Q * H = R, # where H is the tridiagonal matrix from Lanczos with one # extra row, beta(k+1) e_k^T. Acond = gmax/gmin # See if any of the stopping criteria are satisfied. # In rare cases, istop is already -1 from above (Abar = const*I). if istop == 0: t1 = 1 + test1 # These tests work if tol < eps t2 = 1 + test2 if t2 <= 1 : istop = 2 if t1 <= 1 : istop = 1 if itn >= maxiter : istop = 6 if Acond >= 0.1/eps : istop = 4 if epsx >= beta1 : istop = 3 #if rnorm <= epsx : istop = 2 #if rnorm <= epsr : istop = 1 if test2 <= tol : istop = 2 if test1 <= tol : istop = 1 # See if it is time to print something. prnt = False if n <= 40 : prnt = True if itn <= 10 : prnt = True if itn >= maxiter-10 : prnt = True if itn % 10 == 0 : prnt = True if qrnorm <= 10*epsx : prnt = True if qrnorm <= 10*epsr : prnt = True if Acond <= 1e-2/eps : prnt = True if istop != 0 : prnt = True if show and prnt: str1 = '%6g %12.5e %10.3e' % (itn, x[0], test1) str2 = ' %10.3e' % (test2,) str3 = ' %8.1e %8.1e %8.1e' % (Anorm, Acond, gbar/Anorm) print str1 + str2 + str3 if itn % 10 == 0: print if callback is not None: callback(x) if istop != 0: break #TODO check this if show: print print last + ' istop = %3g itn =%5g' % (istop,itn) print last + ' Anorm = %12.4e Acond = %12.4e' % (Anorm,Acond) print last + ' rnorm = %12.4e ynorm = %12.4e' % (rnorm,ynorm) print last + ' Arnorm = %12.4e' % (Arnorm,) print last + msg[istop+1] if istop == 6: info = maxiter else: info = 0 return (postprocess(x),info)