def lsmr(A, b, damp=<NUM_LIT:0.0>, atol=<NUM_LIT>, btol=<NUM_LIT>, conlim=<NUM_LIT>,<EOL>maxiter=None, show=False):
A = aslinearoperator(A)<EOL>b = b.squeeze()<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>hdg1 = '<STR_LIT>''<STR_LIT:r>'<EOL>hdg2 = '<STR_LIT>'<EOL>pfreq = <NUM_LIT:20>   <EOL>pcount = <NUM_LIT:0>   <EOL>m, n = A.shape<EOL>minDim = min([m, n])<EOL>if maxiter is None:<EOL><INDENT>maxiter = minDim<EOL><DEDENT>if show:<EOL><INDENT>print('<STR_LIT:U+0020>')<EOL>print('<STR_LIT>')<EOL>print('<STR_LIT>' % (m, n))<EOL>print('<STR_LIT>' % (damp))<EOL>print('<STR_LIT>' % (atol, conlim))<EOL>print('<STR_LIT>' % (btol, maxiter))<EOL><DEDENT>u = b<EOL>beta = norm(u)<EOL>v = zeros(n)<EOL>alpha = <NUM_LIT:0><EOL>if beta > <NUM_LIT:0>:<EOL><INDENT>u = (<NUM_LIT:1> / beta) * u<EOL>v = A.rmatvec(u)<EOL>alpha = norm(v)<EOL><DEDENT>if alpha > <NUM_LIT:0>:<EOL><INDENT>v = (<NUM_LIT:1> / alpha) * v<EOL><DEDENT>itn = <NUM_LIT:0><EOL>zetabar = alpha * beta<EOL>alphabar = alpha<EOL>rho = <NUM_LIT:1><EOL>rhobar = <NUM_LIT:1><EOL>cbar = <NUM_LIT:1><EOL>sbar = <NUM_LIT:0><EOL>h = v.copy()<EOL>hbar = zeros(n)<EOL>x = zeros(n)<EOL>betadd = beta<EOL>betad = <NUM_LIT:0><EOL>rhodold = <NUM_LIT:1><EOL>tautildeold = <NUM_LIT:0><EOL>thetatilde = <NUM_LIT:0><EOL>zeta = <NUM_LIT:0><EOL>d = <NUM_LIT:0><EOL>normA2 = alpha * alpha<EOL>maxrbar = <NUM_LIT:0><EOL>minrbar = <NUM_LIT><EOL>normA = sqrt(normA2)<EOL>condA = <NUM_LIT:1><EOL>normx = <NUM_LIT:0><EOL>normb = beta<EOL>istop = <NUM_LIT:0><EOL>ctol = <NUM_LIT:0><EOL>if conlim > <NUM_LIT:0>:<EOL><INDENT>ctol = <NUM_LIT:1> / conlim<EOL><DEDENT>normr = beta<EOL>normar = alpha * beta<EOL>if normar == <NUM_LIT:0>:<EOL><INDENT>if show:<EOL><INDENT>print(msg[<NUM_LIT:0>])<EOL><DEDENT>return x, istop, itn, normr, normar, normA, condA, normx<EOL><DEDENT>if show:<EOL><INDENT>print('<STR_LIT:U+0020>')<EOL>print(hdg1, hdg2)<EOL>test1 = <NUM_LIT:1><EOL>test2 = alpha / beta<EOL>str1 = '<STR_LIT>' % (itn, x[<NUM_LIT:0>])<EOL>str2 = '<STR_LIT>' % (normr, normar)<EOL>str3 = '<STR_LIT>' % (test1, test2)<EOL>print('<STR_LIT>'.join([str1, str2, str3]))<EOL><DEDENT>while itn < maxiter:<EOL><INDENT>itn = itn + <NUM_LIT:1><EOL>u = A.matvec(v) - alpha * u<EOL>beta = norm(u)<EOL>if beta > <NUM_LIT:0>:<EOL><INDENT>u = (<NUM_LIT:1> / beta) * u<EOL>v = A.rmatvec(u) - beta * v<EOL>alpha = norm(v)<EOL>if alpha > <NUM_LIT:0>:<EOL><INDENT>v = (<NUM_LIT:1> / alpha) * v<EOL><DEDENT><DEDENT>chat, shat, alphahat = _sym_ortho(alphabar, damp)<EOL>rhoold = rho<EOL>c, s, rho = _sym_ortho(alphahat, beta)<EOL>thetanew = s*alpha<EOL>alphabar = c*alpha<EOL>rhobarold = rhobar<EOL>zetaold = zeta<EOL>thetabar = sbar * rho<EOL>rhotemp = cbar * rho<EOL>cbar, sbar, rhobar = _sym_ortho(cbar * rho, thetanew)<EOL>zeta = cbar * zetabar<EOL>zetabar = - sbar * zetabar<EOL>hbar = h - (thetabar * rho / (rhoold * rhobarold)) * hbar<EOL>x = x + (zeta / (rho * rhobar)) * hbar<EOL>h = v - (thetanew / rho) * h<EOL>betaacute = chat * betadd<EOL>betacheck = -shat * betadd<EOL>betahat = c * betaacute<EOL>betadd = -s * betaacute<EOL>thetatildeold = thetatilde<EOL>ctildeold, stildeold, rhotildeold = _sym_ortho(rhodold, thetabar)<EOL>thetatilde = stildeold * rhobar<EOL>rhodold = ctildeold * rhobar<EOL>betad = - stildeold * betad + ctildeold * betahat<EOL>tautildeold = (zetaold - thetatildeold * tautildeold) / rhotildeold<EOL>taud = (zeta - thetatilde * tautildeold) / rhodold<EOL>d = d + betacheck * betacheck<EOL>normr = sqrt(d + (betad - taud)**<NUM_LIT:2> + betadd * betadd)<EOL>normA2 = normA2 + beta * beta<EOL>normA = sqrt(normA2)<EOL>normA2 = normA2 + alpha * alpha<EOL>maxrbar = max(maxrbar, rhobarold)<EOL>if itn > <NUM_LIT:1>:<EOL><INDENT>minrbar = min(minrbar, rhobarold)<EOL><DEDENT>condA = max(maxrbar, rhotemp) / min(minrbar, rhotemp)<EOL>normar = abs(zetabar)<EOL>normx = norm(x)<EOL>test1 = normr / normb<EOL>if (normA * normr) != <NUM_LIT:0>:<EOL><INDENT>test2 = normar / (normA * normr)<EOL><DEDENT>else:<EOL><INDENT>test2 = infty<EOL><DEDENT>test3 = <NUM_LIT:1> / condA<EOL>t1 = test1 / (<NUM_LIT:1> + normA * normx / normb)<EOL>rtol = btol + atol * normA * normx / normb<EOL>if itn >= maxiter:<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>if show:<EOL><INDENT>if (n <= <NUM_LIT>) or (itn <= <NUM_LIT:10>) or (itn >= maxiter - <NUM_LIT:10>) or(itn % <NUM_LIT:10> == <NUM_LIT:0>) or (test3 <= <NUM_LIT> * ctol) or(test2 <= <NUM_LIT> * atol) or (test1 <= <NUM_LIT> * rtol) or(istop != <NUM_LIT:0>):<EOL><INDENT>if pcount >= pfreq:<EOL><INDENT>pcount = <NUM_LIT:0><EOL>print('<STR_LIT:U+0020>')<EOL>print(hdg1, hdg2)<EOL><DEDENT>pcount = pcount + <NUM_LIT:1><EOL>str1 = '<STR_LIT>' % (itn, x[<NUM_LIT:0>])<EOL>str2 = '<STR_LIT>' % (normr, normar)<EOL>str3 = '<STR_LIT>' % (test1, test2)<EOL>str4 = '<STR_LIT>' % (normA, condA)<EOL>print('<STR_LIT>'.join([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>' % (istop, normr))<EOL>print('<STR_LIT>' % (normA, normar))<EOL>print('<STR_LIT>' % (itn, condA))<EOL>print('<STR_LIT>' % (normx))<EOL>print(str1, str2)<EOL>print(str3, str4)<EOL><DEDENT>return x, istop, itn, normr, normar, normA, condA, normx<EOL>
Iterative solver for least-squares problems.

    lsmr solves the system of linear equations ``Ax = b``. If the system
    is inconsistent, it solves the least-squares problem ``min ||b - Ax||_2``.
    A is a rectangular matrix of dimension m-by-n, where all cases are
    allowed: m = n, m > n, or m < n. B is a vector of length m.
    The matrix A may be dense or sparse (usually sparse).

    Parameters
    ----------
    A : {matrix, sparse matrix, ndarray, LinearOperator}
        Matrix A in the linear system.
    b : (m,) ndarray
        Vector b in the linear system.
    damp : float
        Damping factor for regularized least-squares. `lsmr` solves
        the regularized least-squares problem::

         min ||(b) - (  A   )x||
             ||(0)   (damp*I) ||_2

        where damp is a scalar.  If damp is None or 0, the system
        is solved without regularization.
    atol, btol : float
        Stopping tolerances. `lsmr` continues iterations until a
        certain backward error estimate is smaller than some quantity
        depending on atol and btol.  Let ``r = b - Ax`` be the
        residual vector for the current approximate solution ``x``.
        If ``Ax = b`` seems to be consistent, ``lsmr`` terminates
        when ``norm(r) <= atol * norm(A) * norm(x) + btol * norm(b)``.
        Otherwise, lsmr terminates when ``norm(A^{T} r) <=
        atol * norm(A) * norm(r)``.  If both tolerances are 1.0e-6 (say),
        the final ``norm(r)`` should be accurate to about 6
        digits. (The final x will usually have fewer correct digits,
        depending on ``cond(A)`` and the size of LAMBDA.)  If `atol`
        or `btol` is None, a default value of 1.0e-6 will be used.
        Ideally, they should be estimates of the relative error in the
        entries of A and B respectively.  For example, if the entries
        of `A` have 7 correct digits, set atol = 1e-7. This prevents
        the algorithm from doing unnecessary work beyond the
        uncertainty of the input data.
    conlim : float
        `lsmr` 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. If `conlim` is None, the
        default value is 1e+8.  Maximum precision can be obtained by
        setting ``atol = btol = conlim = 0``, but the number of
        iterations may then be excessive.
    maxiter : int
        `lsmr` terminates if the number of iterations reaches
        `maxiter`.  The default is ``maxiter = min(m, n)``.  For
        ill-conditioned systems, a larger value of `maxiter` may be
        needed.
    show : bool
        Print iterations logs if ``show=True``.

    Returns
    -------
    x : ndarray of float
        Least-square solution returned.
    istop : int
        istop gives the reason for stopping::

          istop   = 0 means x=0 is a solution.
                  = 1 means x is an approximate solution to A*x = B,
                      according to atol and btol.
                  = 2 means x approximately solves the least-squares problem
                      according to atol.
                  = 3 means COND(A) seems to be greater than CONLIM.
                  = 4 is the same as 1 with atol = btol = eps (machine
                      precision)
                  = 5 is the same as 2 with atol = eps.
                  = 6 is the same as 3 with CONLIM = 1/eps.
                  = 7 means ITN reached maxiter before the other stopping
                      conditions were satisfied.

    itn : int
        Number of iterations used.
    normr : float
        ``norm(b-Ax)``
    normar : float
        ``norm(A^T (b - Ax))``
    norma : float
        ``norm(A)``
    conda : float
        Condition number of A.
    normx : float
        ``norm(x)``

    Notes
    -----

    .. versionadded:: 0.11.0

    References
    ----------
    .. [1] D. C.-L. Fong and M. A. Saunders,
           "LSMR: An iterative algorithm for sparse least-squares problems",
           SIAM J. Sci. Comput., vol. 33, pp. 2950-2971, 2011.
           http://arxiv.org/abs/1006.0758
    .. [2] LSMR Software, http://www.stanford.edu/~clfong/lsmr.html