o
    Ö­j‡*  ã                	   @   sb   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	dddd
d
dœdd„ZdS )é    )ÚinnerÚzerosÚinfÚfinfo)Únorm)Úsqrté   )Úmake_systemÚminresNgñhãˆµøä>g        F)ÚrtolÚshiftÚmaxiterÚMÚcallbackÚshowÚcheckc          I      C   sò  t | |||ƒ\} }}
}}| j}|j}d}d}| jd }|du r#d| }g d¢}|rNt|d ƒ t|d|d	›d
|d›� ƒ t|d|d	›d|d›� ƒ tƒ  d}d}d}d}d}d}|
j}t|ƒj}|du rk| ¡ }n|| |
  }||ƒ}t||ƒ}|dk r‚t	dƒ‚|dkrŒ||
ƒdfS t
|ƒ}|dkrœ|}
||
ƒdfS t|ƒ}|	rî||ƒ}||ƒ}t||ƒ} t||ƒ}!t| |! ƒ}"| | |d  }#|"|#krÊt	dƒ‚||ƒ}t||ƒ} t||ƒ}!t| |! ƒ}"| | |d  }#|"|#krît	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�rN||%|$ |  }t|2|ƒ}3||3|% |  }|}|}||ƒ}|%}$t||ƒ}%|%dk �rst	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�r)|#}?|)}(|(}|dk�s7|dk�r:t}@n|||  }@|dk�rHt}An|7| }A|-|. }|dk�r�d|@ }Bd|A }C|Cdk�rdd}|Bdk�rkd}||k�rrd}|d| k�r{d}|=|k�r‚d}|A|k�r‰d}|@|k�r�d}d}D|dk�r™d }D|dk�r d }D||d k�r©d }D|d dk�r²d }D|(d|= k�r»d }D|(d|> k�rÄd }D|d!| k�rÍd }D|dk�rÔd }D|�r|D�r|d"›d#|
d d$›d#|@d%›�}Ed#|Ad%›�}Fd#|d&›d#|d&›d#|6| d&›�}Gt|E|F |G ƒ |d dk�rtƒ  |du�r||
ƒ |dk�r n||k �s+|�ritƒ  t|d'|d	›d(|d)›� ƒ t|d*|d+›d,|d+›� ƒ t|d-|d+›d.|d+›� ƒ t|d/|8d+›� ƒ t|||d   ƒ |dk�rq|}Hnd}H||
ƒ|HfS )0aÌ  
    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 array, 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.
    rtol : float
        Tolerance to achieve. The algorithm terminates when the relative
        residual is below ``rtol``.
    maxiter : integer
        Maximum number of iterations.  Iteration will stop after maxiter
        steps even if the specified tolerance has not been achieved.
    M : {sparse array, 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_array
    >>> from scipy.sparse.linalg import minres
    >>> A = csc_array([[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      =  Ú3gz     shift  =  z23.14ez
itnlim =  z     rtol   =  z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      ð?é   é
   é   gš™™™™™¹?é   é   Fé(   Tg{®Gáz„?Ú6gú z12.5ez10.3ez8.1ez istop   =  z               itn   =Ú5gz Anorm   =  z12.4ez      Acond =  z rnorm   =  z      ynorm =  z Arnorm  =  )r	   ÚmatvecÚshapeÚprintr   r   ÚepsÚcopyr   Ú
ValueErrorr   r   ÚabsÚmaxr   Úminr   )IÚAÚbÚx0r   r   r   r   r   r   r   ÚxÚpostprocessr   ÚpsolveÚfirstÚlastÚnÚmsgÚistopÚitnÚAnormÚAcondÚrnormÚynormÚxtyper"   Úr1ÚyÚbeta1ÚbnormÚwÚr2ÚsÚtÚzÚepsaÚoldbÚbetaÚdbarÚepslnÚqrnormÚphibarÚrhs1Úrhs2Útnorm2ÚgmaxÚgminÚcsÚsnÚw2ÚvÚalfaÚoldepsÚdeltaÚgbarÚrootÚArnormÚgammaÚphiÚdenomÚw1ÚepsxÚepsrÚdiagÚtest1Útest2Út1Út2ÚprntÚstr1Ústr2Ústr3Úinfo© rh   ú_/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/sparse/linalg/_isolve/minres.pyr
   
   s‚  R































 

 �
å 
)N)Únumpyr   r   r   r   Únumpy.linalgr   Úmathr   Úutilsr	   Ú__all__r
   rh   rh   rh   ri   Ú<module>   s    ÿ