
    _-PhB                     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#Steepest Descent projection method.    N)warn)sparse   )norm)make_systemh㈵>rrc	                    t          | |||          \  } }}	}}
t          j        dd           |t          t	          |                    }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  }nY|dk    r't          |          }t          ||z            }||z  }n,|dk    rt          j        |          }|}nt          d          d}d}	 | |z  }t          j        |                                |          }|dk     rt'          d            |
|	          dfS ||z  }|	||z  z   }	|dz  }t          j        ||          r|dk    r	|| |	z  z
  }n|||z  z
  }||z  }t          j        |                                |          }|dk     rt'          d            |
|	          dfS t          |          }||                    |           | ||	           |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 |dk    rt'          d            |
|	          dfS ||k    r |
|	          |fS )a|	  Steepest descent algorithm.

    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
    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 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 of cg

            ==  =======================================
            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.

    See Also
    --------
    _minimal_residual

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

    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._steepest_descent)moduleN   z%Number of iterations must be positiveg        g      ?r	   zrr+z5Unable to use ||A||_F with the current matrix format.MrMrrMrzInvalid stopping criteria.2   r   Tz:
Indefinite matrix detected in steepest descent, aborting
zC
Indefinite preconditioner detected in steepest descent, stopping.
zA
Singular preconditioner detected in steepest descent, stopping.
)r   warningsfilterwarningsintlen
ValueErrornpinner	conjugatelinalgr   r   issparseAdata
isinstancendarrayravelsqrtr   modappend)r   bx0tolcriteriamaxiterMcallback	residualsxpostprocessrzrznormrnormbrtolnormAnormMbrecompute_ritqzAzalphas                           ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/krylov/_steepest_descent.pysteepest_descentr<      s   X *!QA66Aq!Q H-MNNNN c!ff++	1@AAA 	
AE	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		Qa!eV|	U		5666 K	
B5(Ehq{{}}a((99OPPPKNNB''S	M
a6"k"" 	rAvvAE	AAEAIAEXakkmmQ''88XYYYKNNB''Q U###HQKKK t;DD%").."3"33e;<DDGGE<DDGBKKED4<<KNNA&&99 VWWWKNNB''==KNNB''k5(    )Nr   r	   NNNN)__doc__r   r   numpyr   scipyr   util.linalgr   utilr   r<    r=   r;   <module>rD      s    ) )                              8<%).2u( u( u( u( u( u(r=   