
    _-Ph                     d    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lmZ 	 	 	 ddZdS )z/Conjugate Gradient, Normal Error Krylov solver.    N)warn)sparse)aslinearoperator   )norm)make_systemh㈵>rrc	                    t          j        |           r| j                                        }	n8t	          t          j        |                                           j                  }	t          | |||          \  } }}
}}| j	        d         }t          j        dd           d}|(t          t          j        d|z                      dz   }nT|d	k     rt          d
          |d|z  k    r6t          d           t          t          j        d|z                      dz   }|| |
z  z
  }t!          |          }||z  }|	|z  }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  }nY|dk    r't!          |          }t!          ||z            }||z  }n,|dk    rt          j        |          }|}nt          d          ||k     r ||
          dfS d}	 |t          j        |                                |          z  }|
||z  z  }
t          j        ||          r|dk    r||| |z  z  z  }n|| |
z  z
  }||z  }t          j        |                                |          }||z  }|}||z  }||	|z  z  }|d	z  }t
          j                            |          }||                    |           | ||
           |dk    r||z  }nf|dk    r)||t
          j                            |
          z  |z   z  }n7|dk    rt!          |          }||z  }n|dk    rt          j        |          }|}||k     r ||
          dfS ||k    r ||
          |fS )a	  Conjugate Gradient, Normal Error algorithm.

    Applies CG to the normal equations, A A.H x = b. Left preconditioning
    is supported.  Note that unless A is well-conditioned, the use of
    CGNE is inadvisable

    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||
        'rMr':       <r, Mr>^1/2 < tol
        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 A.H 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 cgne
    >>> 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) = cgne(A,b, maxiter=2, tol=1e-8)
    >>> print(f'{norm(b - A*x):.6}')
    46.1547

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

    r   alwayszpyamg.krylov._cgne)module   Ng?r      z%Number of iterations must be positivezOmaximum allowed inner iterations (maxiter) are the 130% timesthe number of dofsg        g      ?r
   zrr+z5Unable to use ||A||_F with the current matrix format.MrMrrMrzInvalid stopping criteria.)r   
isspmatrixT	conjugater   npasarrayconjr   shapewarningsfilterwarningsintceil
ValueErrorr   r   innerissparseAdata
isinstancendarrayravellinalgsqrtmodappend)r    bx0tolcriteriamaxiterMcallback	residualsAHxpostprocessnrecompute_rrnormrzpold_zrnormbrtolnormAnormMbitalphanew_zrbetas                              R/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/krylov/_cgne.pycgnerD      s   V  6S]]__ bjmm0022455 *!QA66Aq!Q	
A H-ABBBB K
 bgc!enn%%)	1@AAA	CE		 " 	# 	# 	#bgc!enn%%) 	
AE	AGGE 	
AA
QAXakkmmQ''Fw	!!! GGE|| 4U{	U		?13 	VNNEERZ(( 	V!#''EETUUUebinnQ///%78	V		Qa!eV|	U		5666t||A"" 
B3( !++--333 	
UQY 6"k"" 	rAvv!a% AAAE	A E !++--++ 	
T		R!V
a	q!! U###HQKKK t;DD%").."3"33e;<DDGGE<DDGFOOED4<<KNNA&&==KNNB''g3(    )Nr	   r
   NNNN)__doc__r   r   numpyr   scipyr   scipy.sparse.linalgr   util.linalgr   utilr   rD    rE   rC   <module>rM      s    5 5                  0 0 0 0 0 0             ,0"&G( G( G( G( G( G(rE   