
    _-Ph                     L    d Z ddl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bicgstab Krylov solver.    N)sparse   )norm)make_systemh㈵>rrc	                 `   t          | |||          \  } }}	}}
t          j        dd           |t          |	          dz   }n|dk     rt	          d          || |	z  z
  }t          |          }||g|dd<   t          |          }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  }nt	          d          ||k     r |
|	          dfS | j        d         dk    rAt          j        | t          j        d	g|	j                  z            } |
||z            dfS |                                }|                                }t          j        |                                |          }d}	 ||z  }| |z  }|t          j        |                                |          z  }|||z  z
  }||z  }| |z  }t          j        |                                |          t          j        |                                |          z  }|	||z  z   ||z  z   }	|||z  z
  }t          j        |                                |          }||z  ||z  z  }|}|||||z  z
  z  z   }|dz  }t          |          }||                    |           | ||	           |d
k    r||z  }n.|dk    r(||t          j                            |	          z  |z   z  }||k     r |
|	          dfS ||k    r |
|	          |fS )a  Biconjugate Gradient Algorithm with Stabilization.

    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||)
        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 bicgstab
    >>> 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) = bicgstab(A,b, maxiter=2, tol=1e-8)
    >>> print(f'{norm(b - A*x):.6}')
    4.68163

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

    alwayszpyamg.krylov._bicgstab)moduleN      z%Number of iterations must be positiveg        g      ?r   zrr+z5Unable to use ||A||_F with the current matrix format.zInvalid stopping criteria.r   )dtype)r   warningsfilterwarningslen
ValueErrorr   r   issparseAdata
isinstancenpndarrayravellinalgshapearrayr   copyinner	conjugateappend)r   bx0tolcriteriamaxiterMcallback	residualsxpostprocessrnormrnormbrtolnormAentryrstarp	rrstarOlditMpAMpalphasMsAMsomega	rrstarNewbetas                                 V/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyamg/krylov/_bicgstab.pybicgstabr?   
   s   N *!QA66Aq!Q H-EFFFF a&&1*	1@AAA 	
AE	AGGEw	!!! GGEGGE|| 4U{	U		?13 	VNNEERZ(( 	V!#''EETUUUebinnQ///%785666t||A"" 	wqzQRXse17;;;;<<AeG$$a((FFHHE	A**A..I	
B1(U"f "(5??#4#4c::: OU"f !,,RXcmmoos-K-KK 
NURZ' O HU__..22	I%%%-8	 ECK((
aQ U###HQKKK t;DD%").."3"33e;<D4<<KNNA&&==KNNB''c1(    )Nr   r   NNNN)__doc__r   numpyr   scipyr   util.linalgr   utilr   r?    r@   r>   <module>rG      s                             04!&*o( o( o( o( o( o(r@   