
    -i=                         S SK rS SKJr  S SKJrJrJrJrJ	r	J
r
  SSKJr  S SKJr  S/r  SS	 jrSS
SSSSSSSSSS.
S jjrg)    N)LinAlgError)get_blas_funcsqrsolvesvd	qr_insertlstsq   )_get_atol_rtol)make_systemgcrotmkFc	                    Uc  S nUc  S n[        / SQU45      u  ppU/n/ nSn[        R                  nU[        U5      -   n[        R                  " [        U5      U4UR
                  S9n[        R                  " SUR
                  S9n[        R                  " SUR
                  S9n[        R                  " UR
                  5      R                  nSn[        U5       GHx  nU(       a  U[        U5      :  a	  UU   u  nnO_U(       a  U[        U5      :X  a  U" U5      nSnO>U(       d*  UU[        U5      -
  :  a  UUU[        U5      -
  -
     u  nnOU" US	   5      nSnUc  U" U " U5      5      nOUR                  5       nU" U5      n[        U5       H/  u  nnU
" UU5      nUUUU4'   U	" UUUR                  S
   U* 5      nM1     [        R                  " US-   UR
                  S9n[        U5       H-  u  nnU
" UU5      nUUU'   U	" UUUR                  S
   U* 5      nM/     U" U5      UWS-   '   [        R                  " SSS9   SUS	   -  nSSS5        [        R                  " W5      (       a	  U" UU5      nUS	   UU-  :  d  SnUR                  U5        UR                  U5        [        R                  " US-   US-   4UR
                  SS9nUUSUS-   2SUS-   24'   SUUS-   US-   4'   [        R                  " US-   U4UR
                  SS9n UU SUS-   2SS24'   [!        UU UUSSSS9u  nn[#        US   5      nUU:  d
  U(       d  GMy    O   [        R                  " UWU4   5      (       d
  [%        5       e['        USUS-   2SUS-   24   US
SUS-   24   R)                  5       5      u  n    n!USS2SUS-   24   nUUUXUU4$ ! , (       d  f       GN= f)a  
FGMRES Arnoldi process, with optional projection or augmentation

Parameters
----------
matvec : callable
    Operation A*x
v0 : ndarray
    Initial vector, normalized to nrm2(v0) == 1
m : int
    Number of GMRES rounds
atol : float
    Absolute tolerance for early exit
lpsolve : callable
    Left preconditioner L
rpsolve : callable
    Right preconditioner R
cs : list of (ndarray, ndarray)
    Columns of matrices C and U in GCROT
outer_v : list of ndarrays
    Augmentation vectors in LGMRES
prepend_outer_v : bool, optional
    Whether augmentation vectors come before or after
    Krylov iterates

Raises
------
LinAlgError
    If nans encountered

Returns
-------
Q, R : ndarray
    QR decomposition of the upper Hessenberg H=QR
B : ndarray
    Projections corresponding to matrix C
vs : list of ndarray
    Columns of matrix V
zs : list of ndarray
    Columns of matrix Z
y : ndarray
    Solution to ||H y - e_1||_2 = min!
res : float
    The final (preconditioned) residual norm

Nc                     U $ N xs    W/var/www/html/venv/lib/python3.13/site-packages/scipy/sparse/linalg/_isolve/_gcrotmk.pylpsolve_fgmres.<locals>.lpsolve@       H    c                     U $ r   r   r   s    r   rpsolve_fgmres.<locals>.rpsolveC   r   r   axpydotscalnrm2)dtype)r
   r
   )r
   r   Fr      r
   ignore)overdivideTFr!   ordercol)whichoverwrite_qrucheck_finite)r   r"   )r   npnanlenzerosr!   onesfinfoepsrangecopy	enumerateshapeerrstateisfiniteappendr   absr   r	   conj)"matvecv0matolr   r   csouter_vprepend_outer_vr   r   r   r    vszsyresBQRr4   	breakdownjzww_normicalphahcurvQ2R2_s"                                     r   _fgmresrY      s   b 		 ++JRERDt
B	BA
&&C	CLA 	#b'1RXX.A 	bhh'A
rxx(A
((288

 
 CI 1X q3w</1:DAqc'l!2AA Q!c'l*:%:1CL 012DAq2AA9q	"A Aa bMDAq1IEAacFQ1771:v.A " xx!177+bMDAq1IEDGQ1771:v.A " GQqS	[[hx8d2hJE 9 ;;uUAAR3<' I
		!
		!
 XXqsAaCjs;4AaC4191Q3qs7XXqsAhaggS94AaC46
Rq'+%A1 !D'l :W Z ;;q1vm $1Q3$t!t)a$1Q3$inn&67KAq!Q	!DQqSD&	AaBAs""k 98s   	O
O'	gh㈵>g        i     oldest)
rtolrA   maxiterMcallbackr@   kCU	discard_Ctruncatec       
            [        XX!5      u  pp[        R                  " U5      R                  5       (       d  [	        S5      eUS;  a  [	        SU< 35      eU R
                  nUR
                  nU
c  / n
U	c  Un	Su  nnnUc  UR                  5       nO
X" U5      -
  n[        / SQUU45      u  nnnnU" U5      n[        SUXC5      u  pCUS:X  a  UnUS4$ U(       a  U
 VVs/ s H
  u  nnSU4PM     snnU
SS& U
(       Ga  U
R                  S	 S
9  [        R                  " U R                  S   [        U
5      4UR                  SS9n/ nSnU
(       aG  U
R                  S5      u  nnUc  U" U5      nUUSS2U4'   US-  nUR                  U5        U
(       a  MG  [!        USSSS9u  nnnA[#        UR$                  5      n/ n['        [        U5      5       H  nUUU      n['        U5       H'  n U" UUU       UUR                  S   UU U4   * 5      nM)     [)        UUU4   5      S[)        US   5      -  :  a    O&U" SUUU4   -  U5      nUR                  U5        M     [#        [+        UU5      5      SSS2   U
SS& U
(       aW  [        SS/U45      u  nnU
 H?  u  nnU" UU5      n!U" UXR                  S   U!5      nU" UUUR                  S   U!* 5      nMA     ['        U5       GH  n"Ub  U" U5        U" U5      n#[-        XCU-  5      n$U#U$::  a  U"S:  d  U
(       a  X" U5      -
  nU" U5      n#U#U$::  a  Sn"  GOU[-        U	[        U
5      -
  S5      -   n%U
 VVs/ s H  u  nnUPM
     nnn [/        UUU#-  U%U[-        XCU-  5      U#-  US9u  nnn&n'n(n)n*U)U#-  n)U(S   U)S   -  n+[+        U(SS U)SS 5       H  u  n,n!U" U,U+U+R                  S   U!5      n+M      U&R3                  U)5      n-[+        U
U-5       H$  u  n.n/U.u  nnU" UU+U+R                  S   U/* 5      n+M&     [        R4                  " SS9   UR3                  UR3                  U)5      5      n0SSS5        U'S   W0S   -  n1[+        U'SS U0SS 5       H  u  n2n3U" U2U1U1R                  S   U35      n1M       SU" U15      -  n4[        R                  " U45      (       d
  [7        5       e U" U4U15      n1U" U4U+5      n+U" U1U5      n5U" U1UUR                  S   U5* 5      nU" U+XR                  S   U55      nUS:X  a3  [        U
5      U	:  a"  U
(       a  U
S	 [        U
5      U	:  a	  U
(       a  M  GOvUS:X  Gao  [        U
5      U	:  Ga_  U
(       GaW  [;        USS2SS24   R$                  U&R$                  5      R$                  n6[=        U65      u  n7n8n9/ n:[?        U7SS2SU	S-
  24   R$                  5       H  u  nn;U
S   u  nnUU;S   -  nUU;S   -  n[+        U
SS U;SS 5       H;  u  n<n=U<u  n>n?U" U>UUR                  S   U=5      nU" U?UUR                  S   U=5      nM=     U: HA  u  n>n?U" U>U5      n4U" U>UUR                  S   U4* 5      nU" U?UUR                  S   U4* 5      nMC     U" U5      n4U" SU4-  U5      nU" SU4-  U5      nU:R                  UU45        M     U:U
SS& U
R                  U1U+45        GM     U
R                  SUR                  5       45        U(       a  U
 V@VAs/ s H
  u  n@nASUA4PM     snAn@U
SS& UW"S-   4$ s  snnf s  snnf ! [0         a       Mf  f = f! , (       d  f       GN= f! [6        [8        4 a     GM  f = fs  snAn@f )a  
Solve ``Ax = b`` with the flexible GCROT(m,k) algorithm.

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.,
    `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`` and ``atol=0.0``.
maxiter : int, optional
    Maximum number of iterations.  Iteration will stop after maxiter
    steps even if the specified tolerance has not been achieved. The
    default is ``1000``.
M : {sparse array, ndarray, LinearOperator}, optional
    Preconditioner for `A`.  The preconditioner should approximate the
    inverse of `A`. gcrotmk is a 'flexible' algorithm and the preconditioner
    can vary from iteration to iteration. Effective preconditioning
    dramatically improves the rate of convergence, which implies that
    fewer iterations are needed to reach a given error tolerance.
callback : function, optional
    User-supplied function to call after each iteration.  It is called
    as ``callback(xk)``, where ``xk`` is the current solution vector.
m : int, optional
    Number of inner FGMRES iterations per each outer iteration.
    Default: 20
k : int, optional
    Number of vectors to carry between inner FGMRES iterations.
    According to [2]_, good values are around `m`.
    Default: `m`
CU : list of tuples, optional
    List of tuples ``(c, u)`` which contain the columns of the matrices
    C and U in the GCROT(m,k) algorithm. For details, see [2]_.
    The list given and vectors contained in it are modified in-place.
    If not given, start from empty matrices. The ``c`` elements in the
    tuples can be ``None``, in which case the vectors are recomputed
    via ``c = A u`` on start and orthogonalized as described in [3]_.
discard_C : bool, optional
    Discard the C-vectors at the end. Useful if recycling Krylov subspaces
    for different linear systems.
truncate : {'oldest', 'smallest'}, optional
    Truncation scheme to use. Drop: oldest vectors, or vectors with
    smallest singular values using the scheme discussed in [1,2].
    See [2]_ for detailed comparison.
    Default: 'oldest'

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

    * 0  : successful exit
    * >0 : convergence to tolerance not achieved, number of iterations

References
----------
.. [1] E. de Sturler, ''Truncation strategies for optimal Krylov subspace
       methods'', SIAM J. Numer. Anal. 36, 864 (1999).
.. [2] J.E. Hicken and D.W. Zingg, ''A simplified and flexible variant
       of GCROT for solving nonsymmetric linear systems'',
       SIAM J. Sci. Comput. 32, 172 (2010).
.. [3] M.L. Parks, E. de Sturler, G. Mackey, D.D. Johnson, S. Maiti,
       ''Recycling Krylov subspaces for sequences of linear systems'',
       SIAM J. Sci. Comput. 28, 1651 (2006).

Examples
--------
>>> import numpy as np
>>> from scipy.sparse import csc_array
>>> from scipy.sparse.linalg import gcrotmk
>>> R = np.random.randn(5, 5)
>>> A = csc_array(R)
>>> b = np.random.randn(5)
>>> x, exit_code = gcrotmk(A, b, atol=1e-5)
>>> print(exit_code)
0
>>> np.allclose(A.dot(x), b)
True

z$RHS must contain only finite numbers)r[   smallestzInvalid value for 'truncate': N)NNNr   r   r   c                     U S   S L$ )Nr   r   )cus    r   <lambda>gcrotmk.<locals>.<lambda>=  s    r!uD0r   )keyr'   r(   r
   Teconomic)overwrite_amodepivotingg-q=)r   r   g      ?r"   r   r   )r   rA   rB   r$   )invalidr[   re   ) r   r.   r:   all
ValueErrorr>   r6   r   r   sortemptyr8   r0   r!   popr;   r   listTr5   r<   zipmaxrY   r   r   r9   FloatingPointErrorZeroDivisionErrorr   r   r7   )BAbx0r\   rA   r]   r^   r_   r@   r`   ra   rb   rc   r   r>   psolver   r   r   rr    b_normrR   uCusrM   rJ   rK   PrB   new_usrQ   ycj_outerbetabeta_tolmlrI   rE   rF   rG   presuxrN   byrg   bychycxrU   hycrS   gammaDWsigmaVnew_CUrO   cupwpcpupczuzsB                                                                     r   r   r      s   x !b#GA;;q>?@@--9(FGGXXFXXF	zy&OD#t	zFFHq	M*+JQPQFSD#tT!WF  	64>JD{1v')*rtq!$r*1 

01 HHaggaj#b'*!''E66!9DAqy1IAacFFAIIaL b QDzDI1a !##Y s2wA1Q4A1XAaD1aggaj1QqS6': 1QqS6{US3[00S1Q3Z#AMM!   SV_%dd+1	"FE?QD9	c DAqQBQ771:r*AQ1771:s+A  >QKAw tF]+81F1IA7D8GQR[!$$BDAqaB	'.v/0v/17=476k4J44O24(6$Aq!RQ IA4 U1Q4ZAB12'EAraRXXa["-B (UU1X2r{GBDAqaRXXa[3$/B #
 [[*quuQxB +URU]"QR&"QR&)FAsaRXXa[#.B *
	d2hJE;;u%%(** & %_%_ B
Q
UF+Q
E* xb'Q,2qE b'Q,22#2w!|!CRCE(**acc*,,!!f5! %a$1Q3$ikk2DAqa5DAqAaDAAaDA#&r!"vqu#5R!$B Q
B7 Q
B7 $6 #)B #B
 Q
UF; Q
UF; #) !GESY*ASY*AMM1a&)) 3* 1 			2r({ "@ IItQVVX*,-"B$"-1gk> +`    	 	D +* #$56 		j .sB   #\1!\74-\=9!]"0]!]9=
]]
]	!]65]6)NNr   r   Fr   )numpyr.   numpy.linalgr   scipy.linalgr   r   r   r   r   r	   	iterativer   !scipy.sparse.linalg._isolve.utilsr   __all__rY   r   r   r   r   <module>r      sU     $ K K % 9 + MO!g#T4b$$QUDTUXr   