
    _-Ph                     X    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l	m
Z
 	 	 	 dd
ZdS )z!Conjugate Residual Krylov solver.    N)warn)sparse   )norm)make_systemh㈵>rrc	                    t          | |||          \  } }}	}}
t          j        dd           |#t          dt	          |          z            dz   }n|dk     rt          d          || |	z  z
  }||z  }|                                }t          j        |	                                |          }t          j
                            |          }||g|dd<   t          |          }|d	k    rd
}|dk    r||z  }n|dk    rt          j        | j                  rt          | j        j                  }nUt!          | j        t          j                  r't          t          j        | j                            }nt          d          ||t          j
                            |	          z  |z   z  }nA|dk    r,t          j        |          }t          ||z            }||z  }nt          d          ||k     r |
|	          dfS d}| |z  }t          j        |	                                |          }| |z  }d}	 |}|t          j        |	                                |          z  }|	||z  z  }	t          j        ||          r|dk    r	|||z  z  }n|| |	z  z
  }||z  }| |z  }t          j        |	                                |          }||z  }||z  }||z  }||z  }||z  }|dz  }t          j        |	                                |          }t          j
                            |          }||                    |           | ||	           |dk    r||z  }nI|dk    r)||t          j
                            |	          z  |z   z  }n|dk    rt          |          }||z  }||k     r |
|	          dfS |d	k    rt-          d            |
|	          dfS ||k    r |
|	          |fS )aO	  Conjugate Residual algorithm.

    Solves the linear system Ax = b. Left preconditioning is supported.
    The matrix A must be Hermitian symmetric (but not necessarily definite).

    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
    criteria : string
        Stopping criteria, let r=r_k, x=x_k
        'rr':        ||r||       < tol ||b||
        'rr+':       ||r||       < tol (||b|| + ||A||_F ||x||)
        'MrMr':      ||M r||     < tol ||M b||
        if ||b||=0, then set ||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
        residual history in the 2-norm, including the initial 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.

    Examples
    --------
    >>> from pyamg.krylov import cr
    >>> 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) = cr(A,b, maxiter=2, tol=1e-8)
    >>> print(f'{norm(b - A*x):.6}')
    6.54282

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

    alwayszpyamg.krylov._cr)moduleNg?r      z%Number of iterations must be positiveg        g      ?r	   zrr+z5Unable to use ||A||_F with the current matrix format.MrMrzInvalid stopping criteria.r      Tz<
Singular preconditioner detected in CR, ceasing iterations
)r   warningsfilterwarningsintlen
ValueErrorcopynpinner	conjugatelinalgr   r   issparseAdata
isinstancendarrayravelsqrtmodappendr   )r   bx0tolcriteriamaxiterMcallback	residualsxpostprocessrzpzznormrnormbrtolnormAnormMbrecompute_rAzrAzApitrAz_oldalphabetas                               P/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/krylov/_cr.pycrr@      s.   P *!QA66Aq!Q H-?@@@@ c#a&&j//A%	1@AAA 	
AE	A	AA	A	!++--	#	#BINN1Ew	!!! GGE|| 4U{	U		?13 	VNNEERZ(( 	V!#''EETUUUebinnQ///%78	V		a!eV|5666t||A"" K	
QB
(1;;=="
%
%C	
QB	
B7(bhr||~~r222	UQY6"k"" 	rAvvOAAAE	AEUhq{{}}b))7{	T		Q
d

b
aXakkmmQ''	q!! U###HQKKK t;DD%").."3"33e;<DDGGE<D4<<KNNA&&99QRRRKNNB''==KNNB''o7(    )Nr   r	   NNNN)__doc__r   r   numpyr   scipyr   util.linalgr   utilr   r@    rA   r?   <module>rH      s    ' '                              *. $x( x( x( x( x( x(rA   