
    _-Ph                     L    d Z ddlZddlmZ ddlZddlmZ ddlmZ 	 	 	 d	dZ	dS )
z#Minimum Residual projection method.    N)warn   )norm)make_systemh㈵>c                    t          | |||          \  } }}}}	t          j        dd           |#t          dt	          |          z            dz   }n|dk     rt          d          || |z  z
  }
||
z  }t          |          }||g|dd<   t          |          }|d	k    rd
}nt          ||z            }|||z  k     r |	|          dfS d}d}	 || |z  z  }t          j        |	                                |          }|d	k     rt          d            |	|          dfS |t          j        |	                                |          z  }|||z  z   }|dz  }t          j        ||          r|dk    r|| |z  z
  }
||
z  }n|||z  z
  }t          |          }||                    |           | ||           |||z  k     r |	|          dfS ||k    r |	|          |fS )a
  Minimal residual (MR) algorithm. 1D projection method.

    Solves the linear system Ax = b. Left preconditioning is supported.

    Parameters
    ----------
    A : array, matrix, sparse matrix, LinearOperator
        n x n, linear system to solve
    b : array, matrix
        right hand side, shape is (n,) or (n,1)
    x0 : array, matrix
        initial guess, default is a vector of zeros
    tol : float
        Tolerance for stopping criteria, let r=r_k
        ||M r|| < tol ||M b||
        if ||b||=0, then set ||M b||=1 for these tests.
    maxiter : int
        maximum number of iterations allowed
    M : array, matrix, sparse matrix, LinearOperator
        n x n, inverted preconditioner, i.e. solve M A x = M b.
    callback : function
        User-supplied function is called after each iteration as
        callback(xk), where xk is the current solution vector
    residuals : list
        preconditioned residual history in the 2-norm,
        including the initial preconditioned residual

    Returns
    -------
    (xk, info)
    xk : an updated guess after k iterations to the solution of Ax = b
    info : halting status

            ==  =======================================
            0   successful exit
            >0  convergence to tolerance not achieved,
                return iteration count instead.
            <0  numerical breakdown, or illegal input
            ==  =======================================

    Notes
    -----
    The LinearOperator class is in scipy.sparse.linalg.
    Use this class if you prefer to define A or M as a mat-vec routine
    as opposed to explicitly constructing the matrix.

    ..
        minimal residual algorithm:      Preconditioned version:
        r = b - A x                      r = b - A x, z = M r
        while not converged:             while not converged:
            p = A r                          p = M A z
            alpha = (p,r) / (p,p)            alpha = (p, z) / (p, p)
            x = x + alpha r                  x = x + alpha z
            r = r - alpha p                  z = z - alpha p

    See Also
    --------
    _steepest_descent

    Examples
    --------
    >>> from pyamg.krylov import minimal_residual
    >>> from pyamg.util.linalg import norm
    >>> import numpy as np
    >>> from pyamg.gallery import poisson
    >>> A = poisson((10,10))
    >>> b = np.ones((A.shape[0],))
    >>> (x,flag) = minimal_residual(A,b, maxiter=2, tol=1e-8)
    >>> print(f'{norm(b - A*x):.6}')
    7.26369

    References
    ----------
    .. [1] Yousef Saad, "Iterative Methods for Sparse Linear Systems,
       Second Edition", SIAM, pp. 137--142, 2003
       http://www-users.cs.umn.edu/~saad/books.html

    alwayszpyamg.krylov._minimal_residual)moduleNg?r      z%Number of iterations must be positiveg        g      ?r   2   Tz;
Indefinite matrix detected in minimal residual, stopping.
)r   warningsfilterwarningsintlen
ValueErrorr   npinner	conjugater   modappend)Abx0tolmaxiterMcallback	residualsxpostprocessrznormrnormbnormMbrecompute_ritppzalphas                       ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/krylov/_minimal_residual.pyminimal_residualr-   
   sX   b *!QA66Aq!Q H-MNNNN c#a&&j//A%	1@AAA 	
AE	A	AAGGE w	!!! GGE||a!e sV|A"" K	
B (QK XakkmmQ''88PQQQKNNB''RXakkmmQ///	M
a6"k"" 	rAvvAE	AAAAEAIAQ U###HQKKK 3<KNNA&&==KNNB''A (    )Nr   NNNN)
__doc__r   r   numpyr   util.linalgr   utilr   r-    r.   r,   <module>r4      s    ) )                        )-%).2U( U( U( U( U( U(r.   