§
    fŠtj"=  ã                   ód   — d Z dgZddlmZmZmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ 	 	 dd„ZdS )a¢  
Copyright (C) 2010 David Fong and Michael Saunders

LSMR uses an iterative method.

07 Jun 2010: Documentation updated
03 Jun 2010: First release version in Python

David Chin-lung Fong            clfong@stanford.edu
Institute for Computational and Mathematical Engineering
Stanford University

Michael Saunders                saunders@stanford.edu
Systems Optimization Laboratory
Dept of MS&E, Stanford University.

Úlsmré    )ÚzerosÚinfÚ
atleast_1dÚresult_type)Únorm)Úsqrt)Úaslinearoperator)Ú
_sym_orthoç        ç�íµ ÷Æ°>ç    „×—ANFc	                 óD  — t          | ¦  «        } t          |¦  «        }|j        dk    r|                     ¦   «         }d}	d}
d}d}d}| j        \  }}t          ||g¦  «        }|€|}|€t          | |t          ¦  «        }nt          | ||t          ¦  «        }|rxt          d¦  «         t          d	¦  «         t          d
|› d|› d�¦  «         t          d|d›d�¦  «         t          d|d›d|d›d�¦  «         t          d|d›d|d›d�¦  «         |}t          |¦  «        }|€%t          ||¦  «        }|                     ¦   «         }nHt          |                     ¦   «         ¦  «        }||                      |¦  «        z
  }t          |¦  «        }|dk    r-d|z  |z  }|                      |¦  «        }t          |¦  «        }nt          ||¦  «        }d}|dk    rd|z  |z  }d}||z  }|}d}d}d}d}|                     ¦   «         }t          ||¦  «        } |}!d}"d}#d}$d}%d}&d}'||z  }(d})d}*t          |(¦  «        }+d},d}-d}.d}/|dk    rd|z  }/|}0||z  }1|1dk    r!|rt          |	d         ¦  «         ||.||0|1|+|,|-fS |dk    rd|d<   ||.||0|1|+|,|-fS |rnt          d¦  «         t          |
|¦  «         d}2||z  }3|d›d|d         d›�}4d|0d›d|1d›�}5d|2d›d|3d›�}6t          d                     |4|5|6g¦  «        ¦  «         ||k     �r_|dz   }|| z  }||                      |¦  «        z  }t          |¦  «        }|dk    rC|d|z  z  }|| z  }||                      |¦  «        z  }t          |¦  «        }|dk    r|d|z  z  }t!          ||¦  «        \  }7}8}9|}:t!          |9|¦  «        \  };}<}|<|z  }=|;|z  }|}>|&}?||z  }@||z  }At!          ||z  |=¦  «        \  }}}||z  }&| |z  }| |@|z  |:|>z  z   z  } | |z  } ||&||z  z  | z  z  }||=|z   z  }||z  }|7|!z  }B|8 |!z  }C|;|Bz  }D|< |Bz  }!|%}Et!          |#|@¦  «        \  }F}G}H|G|z  }%|F|z  }#|G |"z  |F|Dz  z   }"|?|E|$z  z
  |Hz  }$|&|%|$z  z
  |#z  }I|'|C|Cz  z   }'t          |'|"|Iz
  dz  z   |!|!z  z   ¦  «        }0|(||z  z   }(t          |(¦  «        }+|(||z  z   }(t#          |)|>¦  «        })|dk    rt          |*|>¦  «        }*t#          |)|A¦  «        t          |*|A¦  «        z  },t%          |¦  «        }1t          |¦  «        }-|0|z  }2|+|0z  dk    r	|1|+|0z  z  }3nt&          }3d|,z  }J|2d|+|-z  |z  z   z  }K|||+z  |-z  |z  z   }L||k    rd}.d|Jz   dk    rd }.d|3z   dk    rd!}.d|Kz   dk    rd"}.|J|/k    rd#}.|3|k    rd}.|2|Lk    rd}.|r¾|d$k    s9|d%k    s3||d%z
  k    s*|d%z  dk    s!|Jd&|/z  k    s|3d&|z  k    s|2d&|Lz  k    s|.dk    r||k    r!d}t          d¦  «         t          |
|¦  «         |dz   }|d›d|d         d›�}4d|0d›d|1d›�}5d|2d›d|3d›�}6d|+d›d|,d›�}Mt          d                     |4|5|6|Mg¦  «        ¦  «         |.dk    rn||k     �°_|r«t          d¦  «         t          d'¦  «         t          |	|.         ¦  «         t          d(|.d›d)|0d›�¦  «         t          d*|+d›d+|1d›�¦  «         t          d,|d›d-|,d›�¦  «         t          d.|-d›�¦  «         t          |4|5¦  «         t          |6|M¦  «         ||.||0|1|+|,|-fS )/a‹  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 : {sparse array, ndarray, LinearOperator}
        Matrix A in the linear system.
        Alternatively, ``A`` can be a linear operator which can
        produce ``Ax`` and ``A^H x`` using, e.g.,
        ``scipy.sparse.linalg.LinearOperator``.
    b : array_like, shape (m,)
        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. Default is 0.
    atol, btol : float, optional
        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^H r) <=
        atol * norm(A) * norm(r)``.  If both tolerances are 1.0e-6 (default),
        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, optional
        `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. Default is 1e8.
    maxiter : int, optional
        `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. Default is False.
    show : bool, optional
        Print iterations logs if ``show=True``. Default is False.
    x0 : array_like, shape (n,), optional
        Initial guess of ``x``, if None zeros are used. Default is None.

        .. versionadded:: 1.0.0

    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.  If x0 was given, then x=x0 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^H (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.
           :arxiv:`1006.0758`
    .. [2] LSMR Software, https://web.stanford.edu/group/SOL/software/lsmr/

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csc_array
    >>> from scipy.sparse.linalg import lsmr
    >>> A = csc_array([[1., 0.], [1., 1.], [0., 1.]], dtype=float)

    The first example has the trivial solution ``[0, 0]``

    >>> b = np.array([0., 0., 0.], dtype=float)
    >>> x, istop, itn, normr = lsmr(A, b)[:4]
    >>> istop
    0
    >>> x
    array([0., 0.])

    The stopping code ``istop=0`` returned indicates that a vector of zeros was
    found as a solution. The returned solution `x` indeed contains
    ``[0., 0.]``. The next example has a non-trivial solution:

    >>> b = np.array([1., 0., -1.], dtype=float)
    >>> x, istop, itn, normr = lsmr(A, b)[:4]
    >>> istop
    1
    >>> x
    array([ 1., -1.])
    >>> itn
    1
    >>> normr
    4.440892098500627e-16

    As indicated by ``istop=1``, `lsmr` found a solution obeying the tolerance
    limits. The given solution ``[1., -1.]`` obviously solves the equation. The
    remaining return values include information about the number of iterations
    (`itn=1`) and the remaining difference of left and right side of the solved
    equation.
    The final example demonstrates the behavior in the case where there is no
    solution for the equation:

    >>> b = np.array([1., 0.01, -1.], dtype=float)
    >>> x, istop, itn, normr = lsmr(A, b)[:4]
    >>> istop
    2
    >>> x
    array([ 1.00333333, -0.99666667])
    >>> A.dot(x)-b
    array([ 0.00333333, -0.00333333,  0.00333333])
    >>> normr
    0.005773502691896255

    `istop` indicates that the system is inconsistent and thus `x` is rather an
    approximate solution to the corresponding least-squares problem. `normr`
    contains the minimal distance that was found.
    é   )z9The exact solution is x = 0, or x = x0, if x0 was given  z:Ax - b is small enough, given atol, btol                  z:The least-squares solution is good enough, given atol     z:The estimate of cond(Abar) has exceeded conlim            z:Ax - b is small enough for this machine                   z:The least-squares solution is good enough for this machinez:Cond(Abar) seems to be too large for this machine         z:The iteration limit has been reached                      z(   itn      x(1)       norm r    norm Arz% compatible   LS      norm A   cond Aé   r   Nú z2LSMR            Least-squares solution of  Ax = b
zThe matrix A has z
 rows and z columnszdamp = z20.14eú
zatol = z8.2ez                 conlim = zbtol = z             maxiter = Ú8gg}Ã”%­I²T© Ú6gz12.5ez10.3ez  z8.1eÚ é   é   é   é   é   é   é(   é
   gš™™™™™ñ?zLSMR finishedzistop =z    normr =z    normA =z    normAr =zitn   =z    condA =z    normx =)r
   r   ÚndimÚsqueezeÚshapeÚminr   ÚfloatÚprintr   r   ÚcopyÚmatvecÚrmatvecr	   Újoinr   ÚmaxÚabsr   )NÚAÚbÚdampÚatolÚbtolÚconlimÚmaxiterÚshowÚx0ÚmsgÚhdg1Úhdg2ÚpfreqÚpcountÚmÚnÚminDimÚdtypeÚuÚnormbÚxÚbetaÚvÚalphaÚitnÚzetabarÚalphabarÚrhoÚrhobarÚcbarÚsbarÚhÚhbarÚbetaddÚbetadÚrhodoldÚtautildeoldÚ
thetatildeÚzetaÚdÚnormA2ÚmaxrbarÚminrbarÚnormAÚcondAÚnormxÚistopÚctolÚnormrÚnormarÚtest1Útest2Ústr1Ústr2Ústr3ÚchatÚshatÚalphahatÚrhooldÚcÚsÚthetanewÚ	rhobaroldÚzetaoldÚthetabarÚrhotempÚ	betaacuteÚ	betacheckÚbetahatÚthetatildeoldÚ	ctildeoldÚ	stildeoldÚrhotildeoldÚtaudÚtest3Út1ÚrtolÚstr4sN                                                                                 ú^/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/scipy/sparse/linalg/_isolve/lsmr.pyr   r      sÈ	  € õP 	˜ÑÔ€AÝ�1‰Œ€AØ„v�‚z€zØ�IŠI‰KŒKˆðI€Cð 6€DØ2€DØ€EØ€FàŒ7�D€A€qõ �!�Q�‰[Œ[€Fà€Øˆà	€zÝ˜A˜q¥%Ñ(Ô(ˆˆå˜A˜q "¥eÑ,Ô,ˆàð JÝˆc‰
Œ
ˆ
ÝÐCÑDÔDÐDÝÐ: !Ð:Ð:¨qÐ:Ð:Ð:Ñ;Ô;Ð;ÝÐ'˜Ð'Ð'Ð'Ð'Ñ(Ô(Ð(ÝÐL˜ÐLÐLÐL¸VÐLÐLÐLÐLÑMÔMÐMÝÐH˜ÐHÐHÐH¸'ÐHÐHÐHÐHÑIÔIÐIà	€AÝ�‰GŒG€EØ	€zÝ�!�U‰OŒOˆØ�zŠz‰|Œ|ˆˆå�r—w’w‘y”yÑ!Ô!ˆØ�—’˜‘”‰OˆÝ�A‰wŒwˆàˆa‚x€xØ�‰X˜‰NˆØ�IŠI�a‰LŒLˆÝ�Q‘”ˆˆå�!�U‰OŒOˆØˆàˆq‚y€yØ�‰Y˜!‰Oˆð €CØ�d‰l€GØ€HØ
€CØ€FØ€DØ€Dà	�Š‰Œ€AÝ��E‰?Œ?€Dð €FØ€EØ€GØ€KØ€JØ€DØ	€Að �U‰]€FØ€GØ€GÝ�‰LŒL€EØ€EØ€Eð €EØ€DØ�‚z€zØ�6‰zˆØ€Eð �T‰\€FØ�‚{€{Øð 	Ý�#�a”&‰MŒMˆMØ�%˜˜e V¨U°E¸5Ð@Ð@à�‚z€zØˆˆ"‰Ø�%˜˜e V¨U°E¸5Ð@Ð@àð +Ýˆc‰
Œ
ˆ
Ýˆd�DÑÔÐØˆØ˜‘ˆØÐ'Ð'Ð'˜1˜Qœ4Ð'Ð'Ð'ˆØ/�5Ð/Ð/Ð/ Ð/Ð/Ð/ˆØ-�EÐ-Ð-Ð- Ð-Ð-Ð-ˆÝˆb�gŠg�t˜T 4Ð(Ñ)Ô)Ñ*Ô*Ð*ð �Š-‰-Ø�A‰gˆð 	
ˆeˆV‰ˆØ	ˆQ�XŠX�a‰[Œ[ÑˆÝ�A‰wŒwˆà�!Š8ˆ8Ø�!�d‘(‰OˆAØ�$�‰JˆAØ�—’˜1‘”ÑˆAÝ˜‘G”GˆEØ�qŠyˆyØ�a˜%‘iÑ �õ  *¨(°DÑ9Ô9Ñˆˆd�Hð ˆÝ˜x¨Ñ.Ô.‰	ˆˆ1ˆcØ�U‘7ˆØ�U‘7ˆð ˆ	ØˆØ˜#‘:ˆØ˜‘*ˆÝ'¨¨s©
°HÑ=Ô=Ñˆˆd�FØ�g‰~ˆØ�&˜7Ñ"ˆð 	�8˜c‘> V¨iÑ%7Ñ8Ð9Ñ9ˆØ�‰	ˆØ	ˆd�c˜F‘lÑ# tÑ+Ñ+ˆØ	�˜3‘ÐÑˆØ	ˆQ‰ˆð
 ˜6‘Mˆ	Ø�E˜F‘Nˆ	ð �i‘-ˆØ��i‘ˆð
 #ˆÝ,6°wÀÑ,IÔ,IÑ)ˆ	�9˜kØ Ñ'ˆ
Ø˜fÑ$ˆØ�˜eÑ# i°'Ñ&9Ñ9ˆð
  °Ñ!<Ñ<ÀÑKˆØ�z KÑ/Ñ/°7Ñ:ˆØ�	˜IÑ%Ñ%ˆÝ�Q˜% $™,¨Ñ*Ñ*¨V°f©_Ñ<Ñ=Ô=ˆð ˜$ ™+Ñ%ˆÝ�V‘”ˆØ˜% %™-Ñ'ˆõ �g˜yÑ)Ô)ˆØ�Š7ˆ7Ý˜' 9Ñ-Ô-ˆGÝ�G˜WÑ%Ô%­¨G°WÑ(=Ô(=Ñ=ˆõ
 �W‘”ˆÝ�Q‘”ˆð
 ˜‘ˆØ�E‰M˜aÒÐØ˜e e™mÑ,ˆEˆEåˆEØ�E‘	ˆØ�a˜% %™-¨%Ñ/Ñ/Ñ0ˆØ�d˜U‘l UÑ*¨UÑ2Ñ2ˆð �'Š>ˆ>ØˆEØˆu‰9˜Š>ˆ>ØˆEØˆu‰9˜Š>ˆ>ØˆEØˆr‰6�QŠ;ˆ;ØˆEð �DŠ=ˆ=ØˆEØ�DŠ=ˆ=ØˆEØ�DŠ=ˆ=ØˆEð ð 	9Ø�R’�˜S BšY˜Y¨C°7¸R±<Ò,?Ð,?Ø�b‘˜A’� 5¨C°$©JÒ#6Ð#6Ø˜˜t™Ò#Ð#¨°#¸±*Ò)<Ð)<Ø˜’
�
à˜U’?�?Ø�FÝ˜#‘J”J�JÝ˜$ Ñ%Ô%Ð%Ø !™�ØÐ/Ð/Ð/ 1 Q¤4Ð/Ð/Ð/�Ø7˜5Ð7Ð7Ð7¨Ð7Ð7Ð7�Ø5˜EÐ5Ð5Ð5¨Ð5Ð5Ð5�Ø4˜5Ð4Ð4Ð4¨Ð4Ð4Ð4�Ý�b—g’g˜t T¨4°Ð6Ñ7Ô7Ñ8Ô8Ð8à�1Š9ˆ9Øðo �Š-‰-ðv ð 	Ýˆc‰
Œ
ˆ
ÝˆoÑÔÐÝˆc�%ŒjÑÔÐÝÐ9˜Ð9Ð9Ð9¨UÐ9Ð9Ð9Ñ:Ô:Ð:ÝÐA˜EÐAÐAÐA°FÐAÐAÐAÑBÔBÐBÝÐ7˜Ð7Ð7Ð7¨5Ð7Ð7Ð7Ñ8Ô8Ð8ÝÐ(˜EÐ(Ð(Ð(Ñ)Ô)Ð)Ýˆd�DÑÔÐÝˆd�DÑÔÐàˆe�S˜% ¨°°uÐ<Ð<ó    )r   r   r   r   NFN)Ú__doc__Ú__all__Únumpyr   r   r   r   Únumpy.linalgr   Úmathr	   Úscipy.sparse.linalg._interfacer
   Ú scipy.sparse.linalg._isolve.lsqrr   r   r   r{   rz   ú<module>rƒ      s°   ððð ð$ ˆ(€à 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;à 7Ð 7Ð 7Ð 7Ð 7Ð 7ð 7:Ø&*ðI=ð I=ð I=ð I=ð I=ð I=r{   