
    (ph`                     F    S SK rSSKJr  SSKJr  S/rSSSSSSSS	.S
 jjrg)    N   )_get_atol_rtol)make_systemtfqmrgh㈵>        F)rtolatolmaxiterMcallbackshowc                v   U R                   n	[        R                  " U	[        R                  5      (       a  [        n	U R                  U	5      n [        R                  " UR                   [        R                  5      (       a  UR                  U	5      n[        XX!5      u  ppn[        R                  R                  U5      S:X  a  UR                  5       n
U" U
5      S4$ U R                  S   nUc  [        SUS-  5      nUc  UR                  5       nOXR                  U
5      -
  nUnUR                  5       nUnUR                  U R                  U5      5      nUnS=n=nn[        R                  " UR                  5       U5      R                  nUn[        R                   " U5      nUnUS:X  a
  U" U
5      S4$ [#        SUXC5      u  nn[%        U5       GH  nUS-  S:H  nU(       aD  [        R                  " UR                  5       U5      nUS:X  a  U" U
5      S4s  $ UU-  nUUU-  -
  nUWU-  -  nUUS-  U-  U-  U-  -   n[        R                  R                  U5      U-  n[        R                   " SS	US-  -   -  5      n UUU -  -  nU S-  U-  nUR                  U5      n!U
UU!-  -  n
Ub  U" U
5        U[        R                   " US	-   5      -  U:  a$  U(       a  ['        S
US	-    35        U" U
5      S4s  $ U(       dh  [        R                  " UR                  5       U5      nUU-  n"UU"U-  -   nU"U-  U"S-  U-  -   nUR                  U R                  U5      5      nUU-  nGM  UR                  U R                  W5      5      nUnUnGM     U(       a  ['        SWS	-    35        U" U
5      U4$ )a*  
Use Transpose-Free Quasi-Minimal Residual iteration to solve ``Ax = b``.

Parameters
----------
A : {sparse array, ndarray, LinearOperator}
    The real or complex N-by-N matrix of the linear system.
    Alternatively, `A` can be a linear operator which can
    produce ``Ax`` using, e.g.,
    `scipy.sparse.linalg.LinearOperator`.
b : {ndarray}
    Right hand side of the linear system. Has shape (N,) or (N,1).
x0 : {ndarray}
    Starting guess for the solution.
rtol, atol : float, optional
    Parameters for the convergence test. For convergence,
    ``norm(b - A @ x) <= max(rtol*norm(b), atol)`` should be satisfied.
    The default is ``rtol=1e-5``, the default for ``atol`` is ``0.0``.
maxiter : int, optional
    Maximum number of iterations.  Iteration will stop after maxiter
    steps even if the specified tolerance has not been achieved.
    Default is ``min(10000, ndofs * 10)``, where ``ndofs = A.shape[0]``.
M : {sparse array, ndarray, LinearOperator}
    Inverse of the preconditioner of A.  M should approximate the
    inverse of A and be easy to solve for (see Notes).  Effective
    preconditioning dramatically improves the rate of convergence,
    which implies that fewer iterations are needed to reach a given
    error tolerance.  By default, no preconditioner is used.
callback : function, optional
    User-supplied function to call after each iteration.  It is called
    as ``callback(xk)``, where ``xk`` is the current solution vector.
show : bool, optional
    Specify ``show = True`` to show the convergence, ``show = False`` is
    to close the output of the convergence.
    Default is `False`.

Returns
-------
x : ndarray
    The converged solution.
info : int
    Provides convergence information:

        - 0  : successful exit
        - >0 : convergence to tolerance not achieved, number of iterations
        - <0 : illegal input or breakdown

Notes
-----
The Transpose-Free QMR algorithm is derived from the CGS algorithm.
However, unlike CGS, the convergence curves for the TFQMR method is
smoothed by computing a quasi minimization of the residual norm. The
implementation supports left preconditioner, and the "residual norm"
to compute in convergence criterion is actually an upper bound on the
actual residual norm ``||b - Axk||``.

References
----------
.. [1] R. W. Freund, A Transpose-Free Quasi-Minimal Residual Algorithm for
       Non-Hermitian Linear Systems, SIAM J. Sci. Comput., 14(2), 470-482,
       1993.
.. [2] Y. Saad, Iterative Methods for Sparse Linear Systems, 2nd edition,
       SIAM, Philadelphia, 2003.
.. [3] C. T. Kelley, Iterative Methods for Linear and Nonlinear Equations,
       number 16 in Frontiers in Applied Mathematics, SIAM, Philadelphia,
       1995.

Examples
--------
>>> import numpy as np
>>> from scipy.sparse import csc_array
>>> from scipy.sparse.linalg import tfqmr
>>> A = csc_array([[3, 2, 0], [1, -1, 0], [0, 5, 1]], dtype=float)
>>> b = np.array([2, 4, -1], dtype=float)
>>> x, exitCode = tfqmr(A, b, atol=0.0)
>>> print(exitCode)            # 0 indicates successful convergence
0
>>> np.allclose(A.dot(x), b)
True
r   r   i'  
   r      g      ?r   z:TFQMR: Linear solve converged due to reach TOL iterations z@TFQMR: Linear solve not converged due to reach MAXIT iterations )dtypenp
issubdtypeint64floatastyper   linalgnormcopyshapeminmatvecinner	conjugaterealsqrtr   rangeprint)#Abx0r   r	   r
   r   r   r   r   xpostprocessndofsruwrstarvuhatdthetaetarhorhoLastr0normtau_iterevenvtrstaralphauNextczbetas#                                      T/var/www/html/venv/lib/python3.13/site-packages/scipy/sparse/linalg/_isolve/tfqmr.pyr   r   	   s{   h GGE	}}UBHH%%HHUO	}}QWWbhh''HHUO)!6A! 
yy~~aBFFHA""GGAJEeURZ(	zFFHO	A	AE	!ADA
((5??$a
(
-
-CGWWS\F
C{A"" Wfd9GD!gax1}hhu0!4G"}#A++'ME	ME	UT\E!S(1,,		q!C'GGB!eQh,'(uqy!tunHHQK	S1WQK a 4' $$(F8- .NA&&((5??,a0C=DD1HAttQw!m+A88AHHQK(DIA88AHHUO,DAGS V 	  F8% 	&NG$$    )N)numpyr   	iterativer   utilsr   __all__r    rA   r@   <module>rG      s0     %  )j%Bej%rA   