U
    Gvf,                     @   s   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gZdddZedk rd dl mZ d dlmZ dZg Zdd Zeeded edgd geeddZededed ed gd geeddZeje_eejd  ZeeeddedZdS )    )innerzerosinffinfo)norm)sqrt   )make_systemminresN        h㈵>Fc
           I      C   s  t | |||\} }}
}}| j}|j}d}d}| jd }|dkrFd| }dddd	d
ddddddg}|rt|d  t|d||f   t|d||f   t  d}d}d}d}d}d}|
j}t|j}|dkr| }n|| |
  }||}t||}|dk r
t	dn|dkr ||
dfS t
|}|dkrB|}
||
dfS t|}|	r||}||}t||} t||}!t| |! }"| | |d  }#|"|#krt	d||}t||} t||}!t| |! }"| | |d  }#|"|#krt	dd}$|}%d}&d}'|}(|})|}*d}+d},d}-t|j}.d}/d}0t||d}t||d}1|}|r\t  t  td ||k r:|d7 }d|% } | | }2||2}|||2  }|dkr||%|$ |  }t|2|}3||3|% |  }|}|}||}|%}$t||}%|%dk rt	dt|%}%|,|3d |$d  |%d  7 },|dkr:|%| d| kr:d}|'}4|/|& |0|3  }5|0|& |/|3  }6|0|% }'|/ |% }&t
|6|&g}7|)|7 }8t
|6|%g}9t|9|}9|6|9 }/|%|9 }0|/|) }:|0|) })d|9 };|1}<|}1|2|4|<  |5|1  |; }|
|:|  }
t|-|9}-t|.|9}.|*|9 }"|+|5|"  }*|' |" }+t|,}t
|
}|| }#|| | }=|| | }>|6}?|?dkrb|#}?|)}(|(}|dks~|dkrt}@n|||  }@|dkrt}An|7| }A|-|. }|dkr0d|@ }Bd|A }C|Cdkrd}|Bdkrd}||krd}|d | krd!}|=|krd"}|A|kr"d}|@|kr0d}d#}D|d$krBd%}D|dkrPd%}D||d krbd%}D|d dkrtd%}D|(d|= krd%}D|(d|> krd%}D|d&| krd%}D|dkrd%}D|r|Drd'||
d |@f }Ed(|Af }Fd)|||6| f }Gt|E|F |G  |d dkrt  |dk	r(||
 |dkr\q:q\|rt  t|d*||f   t|d+||f   t|d,||f   t|d-|8f   t|||d    |dkr|}Hnd}H||
|HfS ).a  
    Use MINimum RESidual iteration to solve Ax=b

    MINRES minimizes norm(Ax - b) for a real symmetric matrix A.  Unlike
    the Conjugate Gradient method, A can be indefinite or singular.

    If shift != 0 then the method solves (A - shift*I)x = b

    Parameters
    ----------
    A : {sparse matrix, ndarray, LinearOperator}
        The real symmetric N-by-N matrix of the linear system
        Alternatively, ``A`` can be a linear operator which can
        produce ``Ax`` using, e.g.,
        ``scipy.sparse.linalg.LinearOperator``.
    b : ndarray
        Right hand side of the linear system. Has shape (N,) or (N,1).

    Returns
    -------
    x : ndarray
        The converged solution.
    info : integer
        Provides convergence information:
            0  : successful exit
            >0 : convergence to tolerance not achieved, number of iterations
            <0 : illegal input or breakdown

    Other Parameters
    ----------------
    x0 : ndarray
        Starting guess for the solution.
    shift : float
        Value to apply to the system ``(A - shift * I)x = b``. Default is 0.
    tol : float
        Tolerance to achieve. The algorithm terminates when the relative
        residual is below `tol`.
    maxiter : integer
        Maximum number of iterations.  Iteration will stop after maxiter
        steps even if the specified tolerance has not been achieved.
    M : {sparse matrix, ndarray, LinearOperator}
        Preconditioner for A.  The preconditioner should approximate the
        inverse of A.  Effective preconditioning dramatically improves the
        rate of convergence, which implies that fewer iterations are needed
        to reach a given error tolerance.
    callback : function
        User-supplied function to call after each iteration.  It is called
        as callback(xk), where xk is the current solution vector.
    show : bool
        If ``True``, print out a summary and metrics related to the solution
        during iterations. Default is ``False``.
    check : bool
        If ``True``, run additional input validation to check that `A` and
        `M` (if specified) are symmetric. Default is ``False``.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csc_matrix
    >>> from scipy.sparse.linalg import minres
    >>> A = csc_matrix([[3, 2, 0], [1, -1, 0], [0, 5, 1]], dtype=float)
    >>> A = A + A.T
    >>> b = np.array([2, 4, -1], dtype=float)
    >>> x, exitCode = minres(A, b)
    >>> print(exitCode)            # 0 indicates successful convergence
    0
    >>> np.allclose(A.dot(x), b)
    True

    References
    ----------
    Solution of sparse indefinite systems of linear equations,
        C. C. Paige and M. A. Saunders (1975),
        SIAM J. Numer. Anal. 12(4), pp. 617-629.
        https://web.stanford.edu/group/SOL/software/minres/

    This file is a translation of the following MATLAB implementation:
        https://web.stanford.edu/group/SOL/software/minres/minres-matlab.zip

    zEnter minres.   zExit  minres.   r   N   z3 beta2 = 0.  If M = I, b and x are eigenvectors    z/ beta1 = 0.  The exact solution is x0          z3 A solution to Ax = b was found, given rtol        z3 A least-squares solution was found, given rtol    z3 Reasonable accuracy achieved, given eps           z3 x has converged to an eigenvector                 z3 acond has exceeded 0.1/eps                        z3 The iteration limit was reached                   z3 A  does not define a symmetric matrix             z3 M  does not define a symmetric matrix             z3 M  does not define a pos-def preconditioner       zSolution of symmetric Ax = bz#n      =  %3g     shift  =  %23.14ez"itnlim =  %3g     rtol   =  %11.2ezindefinite preconditionergUUUUUU?znon-symmetric matrixznon-symmetric preconditionerdtypezD   Itn     x(1)     Compatible    LS       norm(A)  cond(A) gbar/|A|r         ?   
      g?      F(   Tg{Gz?z%6g %12.5e %10.3ez %10.3ez %8.1e %8.1e %8.1ez( istop   =  %3g               itn   =%5gz' Anorm   =  %12.4e      Acond =  %12.4ez' rnorm   =  %12.4e      ynorm =  %12.4ez Arnorm  =  %12.4e)r	   matvecshapeprintr   r   epscopyr   
ValueErrorr   r   absmaxr   minr   )IAbZx0shifttolmaxiterMcallbackshowcheckxpostprocessr   psolvefirstlastnmsgistopitnZAnormZAcondZrnormZynormZxtyper   Zr1yZbeta1ZbnormwZr2stzZepsaZoldbbetaZdbarZepslnZqrnormZphibarZrhs1Zrhs2Ztnorm2ZgmaxZgmincsZsnZw2vZalfaZoldepsdeltaZgbarrootZArnormgammaphiZdenomZw1ZepsxZepsrZdiagZtest1Ztest2t1t2ZprntZstr1Zstr2Zstr3info rB   F/tmp/pip-unpacked-wheel-96ln3f52/scipy/sparse/linalg/_isolve/minres.pyr
   
   s   R









































__main__)arange)spdiagsr   c                 C   s   t ttt|    d S )N)	residualsappendr   r"   r!   )r*   rB   rB   rC   cb  s    rI   r   Zcsr)formatr   g-q=)r$   r%   r'   )Nr   r   NNNFF)Znumpyr   r   r   r   Znumpy.linalgr   mathr   utilsr	   __all__r
   __name__rE   Zscipy.sparserF   r/   rG   rI   floatr!   r&   r   r,   r   r"   r*   rB   rB   rB   rC   <module>   s.           
  o
$(