ó
î&]\c           @` sá   d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l m Z d  d l	 m
 Z
 d  d l m Z m Z m Z m Z m Z m Z d  d l m Z d g Z d d d d e d „ Z d d	 d
 d d d d d e d d d „ Z d S(   i    (   t   divisiont   print_functiont   absolute_importN(   t   LinAlgError(   t   xrange(   t   get_blas_funcst   qrt   solvet   svdt	   qr_insertt   lstsq(   t   make_systemt   gcrotmkc	   "      C` s  | d k r d „  } n  | d k r0 d „  } n  t d d d d g | f ƒ \ }	 }
 } } | g } g  } d } t j } | t | ƒ } t j t | ƒ | f d | j ƒ} t j d d | j ƒ} t j d d | j ƒ} t j | j ƒ j	 } t
 } x]t | ƒ D]O} | r5| t | ƒ k  r5| | \ } } n | rb| t | ƒ k rb| | ƒ } d } nT | r | | t | ƒ k r | | | t | ƒ \ } } n | | d
 ƒ } d } | d k r×| |  | ƒ ƒ } n | j ƒ  } | | ƒ } xV t | ƒ D]H \ } } |
 | | ƒ } | | | | f <|	 | | | j d	 | ƒ } qüWt j | d d | j ƒ} xP t | ƒ D]B \ } } |
 | | ƒ } | | | <|	 | | | j d	 | ƒ } qqW| | ƒ | | d <t j d d d d ƒ  d | d
 } Wd QXt j | ƒ r| | | ƒ } n  | d
 | | k s6t } n  | j | ƒ | j | ƒ t j | d | d f d | j d d ƒ} | | d | d … d | d … f <d | | d | d f <t j | d | f d | j d d ƒ}  | |  d | d … d d … f <t | |  | | d d d t d t
 ƒ\ } } t | d ƒ } | | k  sO| rPqqWt j | | | f ƒ s|t ƒ  ‚ n  t | d | d … d | d … f | d	 d | d … f j ƒ  ƒ \ } }! }! }! | d d … d | d … f } | | | | | | | f S(   s„  
    FGMRES Arnoldi process, with optional projection or augmentation

    Parameters
    ----------
    matvec : callable
        Operation A*x
    v0 : ndarray
        Initial vector, normalized to nrm2(v0) == 1
    m : int
        Number of GMRES rounds
    atol : float
        Absolute tolerance for early exit
    lpsolve : callable
        Left preconditioner L
    rpsolve : callable
        Right preconditioner R
    CU : list of (ndarray, ndarray)
        Columns of matrices C and U in GCROT
    outer_v : list of ndarrays
        Augmentation vectors in LGMRES
    prepend_outer_v : bool, optional
        Whether augmentation vectors come before or after 
        Krylov iterates

    Raises
    ------
    LinAlgError
        If nans encountered

    Returns
    -------
    Q, R : ndarray
        QR decomposition of the upper Hessenberg H=QR
    B : ndarray
        Projections corresponding to matrix C
    vs : list of ndarray
        Columns of matrix V
    zs : list of ndarray
        Columns of matrix Z
    y : ndarray
        Solution to ||H y - e_1||_2 = min!
    res : float
        The final (preconditioned) residual norm

    c         S` s   |  S(   N(    (   t   x(    (    sB   lib/python2.7/site-packages/scipy/sparse/linalg/isolve/_gcrotmk.pyt   <lambda>C   s    c         S` s   |  S(   N(    (   R   (    (    sB   lib/python2.7/site-packages/scipy/sparse/linalg/isolve/_gcrotmk.pyR   E   s    t   axpyt   dott   scalt   nrm2t   dtypei   i    iÿÿÿÿi   t   overt   ignoret   divideNt   ordert   Ft   whicht   colt   overwrite_qrut   check_finite(   i   i   (   i   i    (   i    iÿÿÿÿ(   t   NoneR   t   npt   nant   lent   zerosR   t   onest   finfot   epst   FalseR   t   copyt	   enumeratet   shapet   errstatet   isfinitet   Truet   appendR	   t   absR   R
   t   conj("   t   matvect   v0t   mt   atolt   lpsolvet   rpsolvet   cst   outer_vt   prepend_outer_vR   R   R   R   t   vst   zst   yt   rest   Bt   Qt   RR$   t	   breakdownt   jt   zt   wt   w_normt   it   ct   alphat   hcurt   vt   Q2t   R2t   _(    (    sB   lib/python2.7/site-packages/scipy/sparse/linalg/isolve/_gcrotmk.pyt   _fgmres   s|    1*		$	!!
!	,$( S gñhãˆµøä>iè  i   t   oldestc   C      C` s	  t  |  | | | ƒ \ }  } } } } t j | ƒ j ƒ  sH t d ƒ ‚ n  | d k rj t d | f ƒ ‚ n  | d k r˜ t j d d t d d ƒ| } n  |  j	 } | j	 } |	 d k r¿ g  }	 n  | d k rÔ | } n  d  \ } } } | | | ƒ } t
 d	 d
 d d g | | f ƒ \ } } } } | | ƒ } |
 r[g  |	 D] \ } } d | f ^ q9|	 (n  |	 rI|	 j d d „  ƒ t j |  j d t |	 ƒ f d | j d d ƒ} g  } d } xg |	 r|	 j d ƒ \ } } | d k rê| | ƒ } n  | | d d … | f <| d 7} | j | ƒ q´Wt | d t d d d t ƒ\ } } } ~ t | j ƒ } g  }  xÆ t t | ƒ ƒ D]² } | | | } xC t | ƒ D]5 }! | | | |! | | j d | |! | f ƒ } qŽWt | | | f ƒ d t | d! ƒ k  rõPn  | d | | | f | ƒ } |  j | ƒ qmWt t | |  ƒ ƒ d d d … |	 (n  |	 rÏt
 d	 d
 g | f ƒ \ } } x_ |	 D]T \ } } | | | ƒ }" | | | | j d |" ƒ } | | | | j d |" ƒ } qtWn  xSt | ƒ D]E}# | d k	 rû| | ƒ n  | | ƒ }$ t | | | ƒ }% |$ |% k rW|# d k s8|	 rW| | | ƒ } | | ƒ }$ n  |$ |% k rmd }# Pn  | t | t |	 ƒ d ƒ }& g  |	 D] \ } } | ^ q‘} y\ t | | |$ |& d | d t | | | ƒ |$ d | ƒ\ } } }' }( }) }* }+ |* |$ 9}* Wn t k
 rPn X|) d |* d }, xA t |) d |* d ƒ D]( \ }- }" | |- |, |, j d |" ƒ }, qDW|' j |* ƒ }. xF t |	 |. ƒ D]5 \ }/ }0 |/ \ } } | | |, |, j d |0 ƒ }, qW| j | j |* ƒ ƒ }1 |( d |1 d }2 xA t |( d |1 d ƒ D]( \ }3 }4 | |3 |2 |2 j d |4 ƒ }2 q
Wy/ d | |2 ƒ }5 t j |5 ƒ sdt ƒ  ‚ n  Wn t t f k
 rqÜn X| |5 |2 ƒ }2 | |5 |, ƒ }, | |2 | ƒ }6 | |2 | | j d |6 ƒ } | |, | | j d |6 ƒ } | d k rxt |	 ƒ | k r|	 r|	 d =q÷Wnñ| d k r	t |	 ƒ | k r	|	 r	t | d d … d d … f j |' j ƒ j }7 t  |7 ƒ \ }8 }9 }: g  }; xtt! |8 d d … d | d … f j ƒ D]I\ } }< |	 d \ } } | |< d } | |< d } xi t |	 d |< d ƒ D]P \ }= }> |= \ }? }@ | |? | | j d |> ƒ } | |@ | | j d |> ƒ } qWx] |; D]U \ }? }@ | |? | ƒ }5 | |? | | j d |5 ƒ } | |@ | | j d |5 ƒ } q_W| | ƒ }5 | d |5 | ƒ } | d |5 | ƒ } |; j | | f ƒ q´W|; |	 (q	n  |	 j |2 |, f ƒ qÜW|	 j d | j" ƒ  f ƒ |
 rm	g  |	 D] \ }A }B d |B f ^ qK	|	 (n  | | ƒ |# d f S("   s`  
    Solve a matrix equation using flexible GCROT(m,k) algorithm.

    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, atol : float, optional
        Tolerances for convergence, ``norm(residual) <= max(tol*norm(b), atol)``.
        The default for ``atol`` is `tol`.

        .. warning::

           The default value for `atol` will be changed in a future release.
           For future compatibility, specify `atol` explicitly.
    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}, optional
        Preconditioner for A.  The preconditioner should approximate the
        inverse of A. gcrotmk is a 'flexible' algorithm and the preconditioner
        can vary from iteration to iteration. Effective preconditioning
        dramatically improves the rate of convergence, which implies that
        fewer iterations are needed to reach a given error tolerance.
    callback : function, optional
        User-supplied function to call after each iteration.  It is called
        as callback(xk), where xk is the current solution vector.
    m : int, optional
        Number of inner FGMRES iterations per each outer iteration.
        Default: 20
    k : int, optional
        Number of vectors to carry between inner FGMRES iterations.
        According to [2]_, good values are around m.
        Default: m
    CU : list of tuples, optional
        List of tuples ``(c, u)`` which contain the columns of the matrices
        C and U in the GCROT(m,k) algorithm. For details, see [2]_.
        The list given and vectors contained in it are modified in-place.
        If not given, start from empty matrices. The ``c`` elements in the
        tuples can be ``None``, in which case the vectors are recomputed
        via ``c = A u`` on start and orthogonalized as described in [3]_.
    discard_C : bool, optional
        Discard the C-vectors at the end. Useful if recycling Krylov subspaces
        for different linear systems.
    truncate : {'oldest', 'smallest'}, optional
        Truncation scheme to use. Drop: oldest vectors, or vectors with
        smallest singular values using the scheme discussed in [1,2].
        See [2]_ for detailed comparison.
        Default: 'oldest'

    Returns
    -------
    x : array or matrix
        The solution found.
    info : int
        Provides convergence information:

        * 0  : successful exit
        * >0 : convergence to tolerance not achieved, number of iterations

    References
    ----------
    .. [1] E. de Sturler, ''Truncation strategies for optimal Krylov subspace
           methods'', SIAM J. Numer. Anal. 36, 864 (1999).
    .. [2] J.E. Hicken and D.W. Zingg, ''A simplified and flexible variant
           of GCROT for solving nonsymmetric linear systems'',
           SIAM J. Sci. Comput. 32, 172 (2010).
    .. [3] M.L. Parks, E. de Sturler, G. Mackey, D.D. Johnson, S. Maiti,
           ''Recycling Krylov subspaces for sequences of linear systems'',
           SIAM J. Sci. Comput. 28, 1651 (2006).

    s$   RHS must contain only finite numbersRM   t   smallests    Invalid value for 'truncate': %rsš   scipy.sparse.linalg.gcrotmk called without specifying `atol`. The default value will change in the future. To preserve current behavior, set ``atol=tol``.t   categoryt
   stackleveli   R   R   R   R   t   keyc         S` s   |  d d  k	 S(   Ni    (   R   (   t   cu(    (    sB   lib/python2.7/site-packages/scipy/sparse/linalg/isolve/_gcrotmk.pyR   .  s    i    R   R   R   Ni   t   overwrite_at   modet   economict   pivotinggê-™—q=g      ð?iÿÿÿÿR4   R2   R5   (   RM   RN   (   NNN(   i    i    (#   R   R   R*   t   allt
   ValueErrorR   t   warningst   warnt   DeprecationWarningR/   R   t   sortt   emptyR(   R    R   t   popR,   R   R+   t   listt   TR   R-   t   zipt   maxRL   R   R   t   FloatingPointErrort   ZeroDivisionErrorR   R   R'   R&   (C   t   At   bt   x0t   tolt   maxitert   Mt   callbackR1   t   kt   CUt	   discard_Ct   truncateR2   R   t   postprocessR/   t   psolveR   R   R   t   rR   t   b_normRE   t   ut   Ct   usR@   R=   R>   t   PR5   t   new_usRD   t   yct   j_outert   betat   beta_tolt   mlR<   R8   R9   R:   t   prest   uxRA   t   byRR   t   byct   hyt   cxRH   t   hycRF   t   gammat   Dt   Wt   sigmat   Vt   new_CURB   t   cupt   wpt   cpt   upt   czt   uz(    (    sB   lib/python2.7/site-packages/scipy/sparse/linalg/isolve/_gcrotmk.pyR   ¹   sþ    O$					-)1	
'3*&
$$ !$ .6$ !)(    (    (   t
   __future__R    R   R   RY   t   numpyR   t   numpy.linalgR   t   scipy._lib.sixR   t   scipy.linalgR   R   R   R   R	   R
   t    scipy.sparse.linalg.isolve.utilsR   t   __all__R   R%   RL   R   (    (    (    sB   lib/python2.7/site-packages/scipy/sparse/linalg/isolve/_gcrotmk.pyt   <module>   s   .	§