ó
Ê½÷Xc           @` s¦   d  Z  d d l m Z m Z m Z d g Z d d l 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 d d d e d „ Z d S(   s¢  
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.

i    (   t   divisiont   print_functiont   absolute_importt   lsmr(   t   zerost   inftyt
   atleast_1d(   t   norm(   t   sqrt(   t   aslinearoperatori   (   t
   _sym_orthog        gíµ ÷Æ°>g    „×—Ac   L      C` s¬  t  |  ƒ }  t | ƒ } | j d k r6 | j ƒ  } n  d( } d
 }	 d }
 d } d } |  j \ } } t | | g ƒ } | d) k rŠ | } n  | rñ t d ƒ t d ƒ t d | | f ƒ t d | ƒ t d | | f ƒ t d | | f ƒ n  | } t | ƒ } t	 | ƒ } d } | d k rMd | | } |  j
 | ƒ } t | ƒ } n  | d k rjd | | } n  d } | | } | } d } d } d } d } | j ƒ  } t	 | ƒ } t	 | ƒ } | } d } d }  d }! d }" d }# d }$ | | }% d }& d }' t |% ƒ }( d }) d }* | }+ d }, d }- | d k r?d | }- n  | }. | | }/ |/ d k rŽ| rrt | d ƒ n  | |, | |. |/ |( |) |* f S| rt d ƒ t |	 |
 ƒ d }0 | | }1 d | | d f }2 d |. |/ f }3 d |0 |1 f }4 t d j |2 |3 |4 g ƒ ƒ n  xð| | k  r | d } |  j | ƒ | | } t | ƒ } | d k r§d | | } |  j
 | ƒ | | } t | ƒ } | d k r§d | | } q§n  t | | ƒ \ }5 }6 }7 | }8 t |7 | ƒ \ }9 }: } |: | }; |9 | } | }< |# }= | | }> | | }? t | | |; ƒ \ } } } | | }# | | } | |> | |8 |< | } | |# | | | } | |; | | } |5 | }@ |6 | }A |9 |@ }B |: |@ } |" }C t |  |> ƒ \ }D }E }F |E | }" |D | }  |E | |D |B } |= |C |! |F }! |# |" |! |  }G |$ |A |A }$ t |$ | |G d | | ƒ }. |% | | }% t |% ƒ }( |% | | }% t |& |< ƒ }& | d k ršt |' |< ƒ }' n  t |& |? ƒ t |' |? ƒ }) t | ƒ }/ t | ƒ }* |. |+ }0 |( |. d k rù|/ |( |. }1 n t }1 d |) }H |0 d |( |* |+ }I | | |( |* |+ }J | | k rJd }, n  d |H d k rcd }, n  d |1 d k r|d }, n  d |I d k r•d }, n  |H |- k rªd }, n  |1 | k r¿d }, n  |0 |J k rÔd }, n  | rí| d k sN| d  k sN| | d  k sN| d  d k sN|H d! |- k sN|1 d! | k sN|0 d! |J k sN|, d k rí| | k rzd } t d ƒ t |	 |
 ƒ n  | d } d | | d f }2 d |. |/ f }3 d |0 |1 f }4 d" |( |) f }K t d j |2 |3 |4 |K g ƒ ƒ qín  |, d k rPqqW| rt d ƒ t d# ƒ t | |, ƒ t d$ |, |. f ƒ t d% |( |/ f ƒ t d& | |) f ƒ t d' |* ƒ t |2 |3 ƒ t |4 |K ƒ n  | |, | |. |/ |( |) |* f S(*   sì  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 : 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.
    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^{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, 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.
    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.
    show : bool, optional
        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://web.stanford.edu/group/SOL/software/lsmr/

    i   s:   The exact solution is  x = 0                              s:   Ax - b is small enough, given atol, btol                  s:   The least-squares solution is good enough, given atol     s:   The estimate of cond(Abar) has exceeded conlim            s:   Ax - b is small enough for this machine                   s:   The least-squares solution is good enough for this machines:   Cond(Abar) seems to be too large for this machine         s:   The iteration limit has been reached                      s(      itn      x(1)       norm r    norm Ars%    compatible   LS      norm A   cond Ai   i    t    s2   LSMR            Least-squares solution of  Ax = b
s'   The matrix A has %8g rows  and %8g colss   damp = %20.14e
s,   atol = %8.2e                 conlim = %8.2e
s'   btol = %8.2e             maxiter = %8g
g}Ã”%­I²Ts
   %6g %12.5es    %10.3e %10.3es     %8.1e %8.1et    i   i   i   i   i   i   i(   i
   gš™™™™™ñ?s    %8.1e %8.1es   LSMR finisheds   istop =%8g    normr =%8.1es!       normA =%8.1e    normAr =%8.1es   itn   =%8g    condA =%8.1es       normx =%8.1e(   s:   The exact solution is  x = 0                              s:   Ax - b is small enough, given atol, btol                  s:   The least-squares solution is good enough, given atol     s:   The estimate of cond(Abar) has exceeded conlim            s:   Ax - b is small enough for this machine                   s:   The least-squares solution is good enough for this machines:   Cond(Abar) seems to be too large for this machine         s:   The iteration limit has been reached                      N(   R	   R   t   ndimt   squeezet   shapet   mint   Nonet   printR   R   t   rmatvect   copyR   t   joint   matvecR
   t   maxt   absR   (L   t   At   bt   dampt   atolt   btolt   conlimt   maxitert   showt   msgt   hdg1t   hdg2t   pfreqt   pcountt   mt   nt   minDimt   ut   betat   vt   alphat   itnt   zetabart   alphabart   rhot   rhobart   cbart   sbart   ht   hbart   xt   betaddt   betadt   rhodoldt   tautildeoldt
   thetatildet   zetat   dt   normA2t   maxrbart   minrbart   normAt   condAt   normxt   normbt   istopt   ctolt   normrt   normart   test1t   test2t   str1t   str2t   str3t   chatt   shatt   alphahatt   rhooldt   ct   st   thetanewt	   rhobaroldt   zetaoldt   thetabart   rhotempt	   betaacutet	   betacheckt   betahatt   thetatildeoldt	   ctildeoldt	   stildeoldt   rhotildeoldt   taudt   test3t   t1t   rtolt   str4(    (    s>   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/linalg/isolve/lsmr.pyR      s`   k       	
















 

							(  

%

N(   t   __doc__t
   __future__R    R   R   t   __all__t   numpyR   R   R   t   numpy.linalgR   t   mathR   t   scipy.sparse.linalg.interfaceR	   t   lsqrR
   R   t   FalseR   (    (    (    s>   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/linalg/isolve/lsmr.pyt   <module>   s   	