a
    ™¥»aœ*  ã                   @   sì   d dl mZmZmZmZmZ d dlmZ ddlm	Z	 dgZ
dd
d„Zedkrèd 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 )é    )ÚsqrtÚinnerÚzerosÚinfÚfinfo)Únormé   )Úmake_systemÚminresNç        çñhãˆµøä>Fc
           H      C   s~  t | |||ƒ\} }}
}}| j}|j}d}d}| jd }|du rFd| }g d¢}|rŒt|d ƒ t|d||f  ƒ t|d	||f  ƒ tƒ  d}d}d}d}d}d}|
j}t|ƒj}|| |
  }||ƒ}t||ƒ}|dk rätd
ƒ‚n|dkrø||
ƒdfS t	|ƒ}|	�r¢||ƒ}||ƒ}t||ƒ}t||ƒ} t
||  ƒ}!|| |d  }"|!|"k�rXtdƒ‚||ƒ}t||ƒ}t||ƒ} t
||  ƒ}!|| |d  }"|!|"k�r¢tdƒ‚d}#|}$d}%d}&|}'|}(|})d}*d}+d},t|ƒj}-d}.d}/t||d�}t||d�}0|}|�rtƒ  tƒ  tdƒ ||k �rð|d7 }d|$ }|| }1||1ƒ}|||1  }|dk�rb||$|# |  }t|1|ƒ}2||2|$ |  }|}|}||ƒ}|$}#t||ƒ}$|$dk �r¬tdƒ‚t	|$ƒ}$|+|2d |#d  |$d  7 }+|dk�rð|$| d| k�rðd}|&}3|.|% |/|2  }4|/|% |.|2  }5|/|$ }&|. |$ }%t|5|%gƒ}6|(|6 }7t|5|$gƒ}8t|8|ƒ}8|5|8 }.|$|8 }/|.|( }9|/|( }(d|8 }:|0};|}0|1|3|;  |4|0  |: }|
|9|  }
t|,|8ƒ},t|-|8ƒ}-|)|8 }!|*|4|!  })|& |! }*t	|+ƒ}t|
ƒ}|| }"|| | }<|| | }=|5}>|>dk�r|"}>|(}'|'}|dk�s4|dk�r:t}?n|||  }?|dk�rVt}@n|6| }@|,|- }|dk�ræd|? }Ad|@ }B|Bdk�rŽd}|Adk�rœd}||k�rªd}|d| k�r¼d}|<|k�rÊd}|@|k�rØd}|?|k�ræd}d}C|dk�rød}C|dk�rd}C||d k�rd}C|d dk�r*d}C|'d|< k�r<d}C|'d|= k�rNd}C|d| k�r`d}C|dk�rnd}C|�rÌ|C�rÌd||
d |?f }Dd|@f }Ed|||5| f }Ft|D|E |F ƒ |d dk�rÌtƒ  |du�rÞ||
ƒ |dk�r�qð�q|�r^tƒ  t|d ||f  ƒ t|d!||f  ƒ t|d"||f  ƒ t|d#|7f  ƒ t|||d   ƒ |dk�rn|}Gnd}G||
ƒ|GfS )$ag
  
    Use MINimum RESidual iteration to solve Ax=b

    MINRES minimizes norm(A*x - 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, dense matrix, 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 : {array, matrix}
        Right hand side of the linear system. Has shape (N,) or (N,1).

    Returns
    -------
    x : {array, matrix}
        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  : {array, matrix}
        Starting guess for the solution.
    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, dense matrix, 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.

    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{®Gáz„?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   Úepsr   Ú
ValueErrorr   ÚabsÚmaxr   r   Úminr   )HÚ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beta1Ú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© rA   ú[/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/scipy/sparse/linalg/isolve/minres.pyr
   	   sp   J





































Ú__main__)Úarange)Úspdiagsr   c                 C   s   t  ttt|   ƒ¡ d S )N)Ú	residualsÚappendr   r!   r    )r)   rA   rA   rB   Úcbn  s    rH   r   Zcsr)Úformatr   gê-�™—q=)r#   r$   r&   )Nr   r   NNNFF)Únumpyr   r   r   r   r   Znumpy.linalgr   Úutilsr	   Ú__all__r
   Ú__name__rD   Zscipy.sparserE   r.   rF   rH   Úfloatr    r%   r   r+   r   r!   r)   rA   rA   rA   rB   Ú<module>   s&     ÿ
  _$(