def lsqr(A, b, damp=<NUM_LIT:0.0>, atol=<NUM_LIT>, btol=<NUM_LIT>, conlim=<NUM_LIT>,<EOL>iter_lim=None, show=False, calc_var=False):
A = aslinearoperator(A)<EOL>if len(b.shape) > <NUM_LIT:1>:<EOL><INDENT>b = b.squeeze()<EOL><DEDENT>m, n = A.shape<EOL>if iter_lim is None:<EOL><INDENT>iter_lim = <NUM_LIT:2> * n<EOL><DEDENT>var = np.zeros(n)<EOL>msg = ('<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>')<EOL>if show:<EOL><INDENT>print('<STR_LIT:U+0020>')<EOL>print('<STR_LIT>')<EOL>str1 = '<STR_LIT>' % (m, n)<EOL>str2 = '<STR_LIT>' % (damp, calc_var)<EOL>str3 = '<STR_LIT>' % (atol, conlim)<EOL>str4 = '<STR_LIT>' % (btol, iter_lim)<EOL>print(str1)<EOL>print(str2)<EOL>print(str3)<EOL>print(str4)<EOL><DEDENT>itn = <NUM_LIT:0><EOL>istop = <NUM_LIT:0><EOL>nstop = <NUM_LIT:0><EOL>ctol = <NUM_LIT:0><EOL>if conlim > <NUM_LIT:0>:<EOL><INDENT>ctol = <NUM_LIT:1>/conlim<EOL><DEDENT>anorm = <NUM_LIT:0><EOL>acond = <NUM_LIT:0><EOL>dampsq = damp**<NUM_LIT:2><EOL>ddnorm = <NUM_LIT:0><EOL>res2 = <NUM_LIT:0><EOL>xnorm = <NUM_LIT:0><EOL>xxnorm = <NUM_LIT:0><EOL>z = <NUM_LIT:0><EOL>cs2 = -<NUM_LIT:1><EOL>sn2 = <NUM_LIT:0><EOL>"""<STR_LIT>"""<EOL>__xm = np.zeros(m)  <EOL>__xn = np.zeros(n)  <EOL>v = np.zeros(n)<EOL>u = b<EOL>x = np.zeros(n)<EOL>alfa = <NUM_LIT:0><EOL>beta = np.linalg.norm(u)<EOL>w = np.zeros(n)<EOL>if beta > <NUM_LIT:0>:<EOL><INDENT>u = (<NUM_LIT:1>/beta) * u<EOL>v = A.rmatvec(u)<EOL>alfa = np.linalg.norm(v)<EOL><DEDENT>if alfa > <NUM_LIT:0>:<EOL><INDENT>v = (<NUM_LIT:1>/alfa) * v<EOL>w = v.copy()<EOL><DEDENT>rhobar = alfa<EOL>phibar = beta<EOL>bnorm = beta<EOL>rnorm = beta<EOL>r1norm = rnorm<EOL>r2norm = rnorm<EOL>arnorm = alfa * beta<EOL>if arnorm == <NUM_LIT:0>:<EOL><INDENT>print(msg[<NUM_LIT:0>])<EOL>return x, istop, itn, r1norm, r2norm, anorm, acond, arnorm, xnorm, var<EOL><DEDENT>head1 = '<STR_LIT>'<EOL>head2 = '<STR_LIT>'<EOL>if show:<EOL><INDENT>print('<STR_LIT:U+0020>')<EOL>print(head1, head2)<EOL>test1 = <NUM_LIT:1><EOL>test2 = alfa / beta<EOL>str1 = '<STR_LIT>' % (itn, x[<NUM_LIT:0>])<EOL>str2 = '<STR_LIT>' % (r1norm, r2norm)<EOL>str3 = '<STR_LIT>' % (test1, test2)<EOL>print(str1, str2, str3)<EOL><DEDENT>while itn < iter_lim:<EOL><INDENT>itn = itn + <NUM_LIT:1><EOL>"""<STR_LIT>"""<EOL>u = A.matvec(v) - alfa * u<EOL>beta = np.linalg.norm(u)<EOL>if beta > <NUM_LIT:0>:<EOL><INDENT>u = (<NUM_LIT:1>/beta) * u<EOL>anorm = sqrt(anorm**<NUM_LIT:2> + alfa**<NUM_LIT:2> + beta**<NUM_LIT:2> + damp**<NUM_LIT:2>)<EOL>v = A.rmatvec(u) - beta * v<EOL>alfa = np.linalg.norm(v)<EOL>if alfa > <NUM_LIT:0>:<EOL><INDENT>v = (<NUM_LIT:1> / alfa) * v<EOL><DEDENT><DEDENT>rhobar1 = sqrt(rhobar**<NUM_LIT:2> + damp**<NUM_LIT:2>)<EOL>cs1 = rhobar / rhobar1<EOL>sn1 = damp / rhobar1<EOL>psi = sn1 * phibar<EOL>phibar = cs1 * phibar<EOL>cs, sn, rho = _sym_ortho(rhobar1, beta)<EOL>theta = sn * alfa<EOL>rhobar = -cs * alfa<EOL>phi = cs * phibar<EOL>phibar = sn * phibar<EOL>tau = sn * phi<EOL>t1 = phi / rho<EOL>t2 = -theta / rho<EOL>dk = (<NUM_LIT:1> / rho) * w<EOL>x = x + t1 * w<EOL>w = v + t2 * w<EOL>ddnorm = ddnorm + np.linalg.norm(dk)**<NUM_LIT:2><EOL>if calc_var:<EOL><INDENT>var = var + dk**<NUM_LIT:2><EOL><DEDENT>delta = sn2 * rho<EOL>gambar = -cs2 * rho<EOL>rhs = phi - delta * z<EOL>zbar = rhs / gambar<EOL>xnorm = sqrt(xxnorm + zbar**<NUM_LIT:2>)<EOL>gamma = sqrt(gambar**<NUM_LIT:2> + theta**<NUM_LIT:2>)<EOL>cs2 = gambar / gamma<EOL>sn2 = theta / gamma<EOL>z = rhs / gamma<EOL>xxnorm = xxnorm + z**<NUM_LIT:2><EOL>acond = anorm * sqrt(ddnorm)<EOL>res1 = phibar**<NUM_LIT:2><EOL>res2 = res2 + psi**<NUM_LIT:2><EOL>rnorm = sqrt(res1 + res2)<EOL>arnorm = alfa * abs(tau)<EOL>r1sq = rnorm**<NUM_LIT:2> - dampsq * xxnorm<EOL>r1norm = sqrt(abs(r1sq))<EOL>if r1sq < <NUM_LIT:0>:<EOL><INDENT>r1norm = -r1norm<EOL><DEDENT>r2norm = rnorm<EOL>test1 = rnorm / bnorm<EOL>test2 = arnorm / (anorm * rnorm + eps)<EOL>test3 = <NUM_LIT:1> / (acond + eps)<EOL>t1 = test1 / (<NUM_LIT:1> + anorm * xnorm / bnorm)<EOL>rtol = btol + atol * anorm * xnorm / bnorm<EOL>if itn >= iter_lim:<EOL><INDENT>istop = <NUM_LIT:7><EOL><DEDENT>if <NUM_LIT:1> + test3 <= <NUM_LIT:1>:<EOL><INDENT>istop = <NUM_LIT:6><EOL><DEDENT>if <NUM_LIT:1> + test2 <= <NUM_LIT:1>:<EOL><INDENT>istop = <NUM_LIT:5><EOL><DEDENT>if <NUM_LIT:1> + t1 <= <NUM_LIT:1>:<EOL><INDENT>istop = <NUM_LIT:4><EOL><DEDENT>if test3 <= ctol:<EOL><INDENT>istop = <NUM_LIT:3><EOL><DEDENT>if test2 <= atol:<EOL><INDENT>istop = <NUM_LIT:2><EOL><DEDENT>if test1 <= rtol:<EOL><INDENT>istop = <NUM_LIT:1><EOL><DEDENT>prnt = False<EOL>if n <= <NUM_LIT>:<EOL><INDENT>prnt = True<EOL><DEDENT>if itn <= <NUM_LIT:10>:<EOL><INDENT>prnt = True<EOL><DEDENT>if itn >= iter_lim-<NUM_LIT:10>:<EOL><INDENT>prnt = True<EOL><DEDENT>if test3 <= <NUM_LIT:2>*ctol:<EOL><INDENT>prnt = True<EOL><DEDENT>if test2 <= <NUM_LIT:10>*atol:<EOL><INDENT>prnt = True<EOL><DEDENT>if test1 <= <NUM_LIT:10>*rtol:<EOL><INDENT>prnt = True<EOL><DEDENT>if istop != <NUM_LIT:0>:<EOL><INDENT>prnt = True<EOL><DEDENT>if prnt:<EOL><INDENT>if show:<EOL><INDENT>str1 = '<STR_LIT>' % (itn, x[<NUM_LIT:0>])<EOL>str2 = '<STR_LIT>' % (r1norm, r2norm)<EOL>str3 = '<STR_LIT>' % (test1, test2)<EOL>str4 = '<STR_LIT>' % (anorm, acond)<EOL>print(str1, str2, str3, str4)<EOL><DEDENT><DEDENT>if istop != <NUM_LIT:0>:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if show:<EOL><INDENT>print('<STR_LIT:U+0020>')<EOL>print('<STR_LIT>')<EOL>print(msg[istop])<EOL>print('<STR_LIT:U+0020>')<EOL>str1 = '<STR_LIT>' % (istop, r1norm)<EOL>str2 = '<STR_LIT>' % (anorm, arnorm)<EOL>str3 = '<STR_LIT>' % (itn, r2norm)<EOL>str4 = '<STR_LIT>' % (acond, xnorm)<EOL>print(str1 + '<STR_LIT:U+0020>' + str2)<EOL>print(str3 + '<STR_LIT:U+0020>' + str4)<EOL>print('<STR_LIT:U+0020>')<EOL><DEDENT>return x, istop, itn, r1norm, r2norm, anorm, acond, arnorm, xnorm, var<EOL>
Find the least-squares solution to a large, sparse, linear system
    of equations.

    The function solves ``Ax = b``  or  ``min ||b - Ax||^2`` or
    ``min ||Ax - b||^2 + d^2 ||x||^2``.

    The matrix A may be square or rectangular (over-determined or
    under-determined), and may have any rank.

    ::

      1. Unsymmetric equations --    solve  A*x = b

      2. Linear least squares  --    solve  A*x = b
                                     in the least-squares sense

      3. Damped least squares  --    solve  (   A    )*x = ( b )
                                            ( damp*I )     ( 0 )
                                     in the least-squares sense

    Parameters
    ----------
    A : {sparse matrix, ndarray, LinearOperator}
        Representation of an m-by-n matrix.  It is required that
        the linear operator can produce ``Ax`` and ``A^T x``.
    b : (m,) ndarray
        Right-hand side vector ``b``.
    damp : float
        Damping coefficient.
    atol, btol : float
        Stopping tolerances. If both are 1.0e-9 (say), the final
        residual norm should be accurate to about 9 digits.  (The
        final x will usually have fewer correct digits, depending on
        cond(A) and the size of damp.)
    conlim : float
        Another stopping tolerance.  lsqr terminates if an estimate of
        ``cond(A)`` exceeds `conlim`.  For compatible systems ``Ax =
        b``, `conlim` could be as large as 1.0e+12 (say).  For
        least-squares problems, conlim should be less than 1.0e+8.
        Maximum precision can be obtained by setting ``atol = btol =
        conlim = zero``, but the number of iterations may then be
        excessive.
    iter_lim : int
        Explicit limitation on number of iterations (for safety).
    show : bool
        Display an iteration log.
    calc_var : bool
        Whether to estimate diagonals of ``(A'A + damp^2*I)^{-1}``.

    Returns
    -------
    x : ndarray of float
        The final solution.
    istop : int
        Gives the reason for termination.
        1 means x is an approximate solution to Ax = b.
        2 means x approximately solves the least-squares problem.
    itn : int
        Iteration number upon termination.
    r1norm : float
        ``norm(r)``, where ``r = b - Ax``.
    r2norm : float
        ``sqrt( norm(r)^2  +  damp^2 * norm(x)^2 )``.  Equal to `r1norm` if
        ``damp == 0``.
    anorm : float
        Estimate of Frobenius norm of ``Abar = [[A]; [damp*I]]``.
    acond : float
        Estimate of ``cond(Abar)``.
    arnorm : float
        Estimate of ``norm(A'*r - damp^2*x)``.
    xnorm : float
        ``norm(x)``
    var : ndarray of float
        If ``calc_var`` is True, estimates all diagonals of
        ``(A'A)^{-1}`` (if ``damp == 0``) or more generally ``(A'A +
        damp^2*I)^{-1}``.  This is well defined if A has full column
        rank or ``damp > 0``.  (Not sure what var means if ``rank(A)
        < n`` and ``damp = 0.``)

    Notes
    -----
    LSQR uses an iterative method to approximate the solution.  The
    number of iterations required to reach a certain accuracy depends
    strongly on the scaling of the problem.  Poor scaling of the rows
    or columns of A should therefore be avoided where possible.

    For example, in problem 1 the solution is unaltered by
    row-scaling.  If a row of A is very small or large compared to
    the other rows of A, the corresponding row of ( A  b ) should be
    scaled up or down.

    In problems 1 and 2, the solution x is easily recovered
    following column-scaling.  Unless better information is known,
    the nonzero columns of A should be scaled so that they all have
    the same Euclidean norm (e.g., 1.0).

    In problem 3, there is no freedom to re-scale if damp is
    nonzero.  However, the value of damp should be assigned only
    after attention has been paid to the scaling of A.

    The parameter damp is intended to help regularize
    ill-conditioned systems, by preventing the true solution from
    being very large.  Another aid to regularization is provided by
    the parameter acond, which may be used to terminate iterations
    before the computed solution becomes very large.

    If some initial estimate ``x0`` is known and if ``damp == 0``,
    one could proceed as follows:

      1. Compute a residual vector ``r0 = b - A*x0``.
      2. Use LSQR to solve the system  ``A*dx = r0``.
      3. Add the correction dx to obtain a final solution ``x = x0 + dx``.

    This requires that ``x0`` be available before and after the call
    to LSQR.  To judge the benefits, suppose LSQR takes k1 iterations
    to solve A*x = b and k2 iterations to solve A*dx = r0.
    If x0 is "good", norm(r0) will be smaller than norm(b).
    If the same stopping tolerances atol and btol are used for each
    system, k1 and k2 will be similar, but the final solution x0 + dx
    should be more accurate.  The only way to reduce the total work
    is to use a larger stopping tolerance for the second system.
    If some value btol is suitable for A*x = b, the larger value
    btol*norm(b)/norm(r0)  should be suitable for A*dx = r0.

    Preconditioning is another way to reduce the number of iterations.
    If it is possible to solve a related system ``M*x = b``
    efficiently, where M approximates A in some helpful way (e.g. M -
    A has low rank or its elements are small relative to those of A),
    LSQR may converge more rapidly on the system ``A*M(inverse)*z =
    b``, after which x can be recovered by solving M*x = z.

    If A is symmetric, LSQR should not be used!

    Alternatives are the symmetric conjugate-gradient method (cg)
    and/or SYMMLQ.  SYMMLQ is an implementation of symmetric cg that
    applies to any symmetric A and will converge more rapidly than
    LSQR.  If A is positive definite, there are other implementations
    of symmetric cg that require slightly less work per iteration than
    SYMMLQ (but will take the same number of iterations).

    References
    ----------
    .. [1] C. C. Paige and M. A. Saunders (1982a).
           "LSQR: An algorithm for sparse linear equations and
           sparse least squares", ACM TOMS 8(1), 43-71.
    .. [2] C. C. Paige and M. A. Saunders (1982b).
           "Algorithm 583.  LSQR: Sparse linear equations and least
           squares problems", ACM TOMS 8(2), 195-209.
    .. [3] M. A. Saunders (1995).  "Solution of sparse rectangular
           systems using LSQR and CRAIG", BIT 35, 588-604.