ó
    Eñ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  pšp¼U/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XÞU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    Úa/home/mande/repo/quber/.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   Féÿÿÿÿr   é   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 �ò	à�ò	ô +Ò+JÈRÈEÓRÑ€Dˆtà
ˆ€BØ	€BØ€AÜ
�&‰&€Cà	ŒC�‹LÑ€Aô 	�Š”#�b“'˜1� R§X¡XÑ.€Aô 	�Š�˜bŸh™hÑ'€AÜ
�Š�˜rŸx™xÑ(€Aä
�(Š(�2—8‘8Ó
×
 Ñ
 €Cà€Iô �1�Xˆö ˜q¤3 w£<Ó/Ø˜1‘:‰DˆA‰qÞ ¤c¨'£lÓ!2Ù˜“ˆAØ‰AÞ  Q¨!¬c°'«lÑ*:Ó%:Ø˜1 ¤C¨£LÑ 0Ñ1Ñ2‰DˆA‰qá˜˜2™“ˆAØˆAà‰9Ù™˜q›	Ó"‰Að —‘“ˆAá�a“ˆô ˜b–M‰DˆAˆqÙ˜˜1“IˆEØˆAˆa�ˆc‰FÙ�Q˜˜1Ÿ7™7 1™:¨ vÓ.ŠAñ "ô �xŠx˜˜!™ 1§7¡7Ñ+ˆÜ˜b–M‰DˆAˆqÙ˜˜1“IˆEØˆD�‰GÙ�Q˜˜1Ÿ7™7 1™:¨ vÓ.ŠAñ "ñ ˜“GˆˆQˆq‰S‰	ä�[Š[˜h¨xÓ8à�d˜2‘h‘JˆE÷ 9ô �;Š;�u×ÑÙ�U˜A“ˆAà�R‘˜3 ™<Ó'ð ˆIà
�	‰	�!ŒØ
�	‰	�!Œô
 �XŠX�q˜‘s˜A˜a™C�j¨¯©°sÑ;ˆØˆˆ4ˆAˆa‰Cˆ4���1‘�ˆ9‰Øˆˆ1ˆQ‰3ˆq�‰sˆ7‰ä�XŠX�q˜‘s˜A�h a§g¡g°SÑ9ˆØˆˆ4ˆAˆa‰Cˆ4’ˆ6‰
ä˜˜R  q°Ø'+¸%ñA‰ˆˆ1ô �!�D‘'‹lˆð �‹:Ÿ™ÙñW ôZ �;Š;�q˜˜1˜‘v×Ñä‹mÐô ˜˜$˜1˜Q™3˜$˜t  !¡˜t˜)™ a¨¨$¨1¨Q©3¨$¨¡i§n¡nÓ&6Ó7�K€A€qˆ!ˆQà	Š!ˆDˆQˆq‰SˆDˆ&‰	€Aàˆa��B˜A˜sÐ"Ð"÷k 9Ö8ús   É	OÏ
O'	gñhãˆµøä>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XÝR                  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O¤U[-        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+XÝR                  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 !™u¨DÑ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Ó#�G€Aˆä�;Š;�q‹>×Ñ×ÑÜÐ?Ó@Ð@àÐ-Ó-ÜÐ9¸(¹ÐFÓGÐGà�X‰X€FØ�X‰X€Fà	�zØˆà�yØˆà&�O€Dˆ#ˆtà	�zØ�F‰F‹H‰à��q“	‰Mˆä*Ò+JÈQÐPQÈFÓSÑ€Dˆ#ˆt�Tá�!‹W€Fô   	¨6°4Ó>�J€Dà�ƒ{ØˆØ�1ˆvˆæÙ')Ô*¢r™t˜q !�$˜“¡rÒ*ˆ‰1ˆ÷ 
à
�‰Ñ0ˆÑ1ô �HŠH�a—g‘g˜a‘j¤# b£'Ð*°!·'±'ÀÑEˆØˆØˆÞà—6‘6˜!“9‰DˆAˆqØ‰yÙ˜1“I�ØˆAŠa�ˆc‰FØ�‰FˆAØ�I‰I�aŒL÷ ˆbô �Q D¨zÀDÑI‰ˆˆ1ˆaØô �!—#‘#‹Yˆð ˆÜ”s˜2“w–ˆAØ�1�Q‘4‘ˆAÜ˜1–X�Ù˜˜A˜a™D™ 1 a§g¡g¨a¡j°1°Q°q°S±6°'Ó:’ñ ä�1�Q�q�S‘6‹{˜U¤S¨¨3©£[Ñ0Ó0áÙ�S˜˜1˜Q˜3™‘Z Ó#ˆAØ�M‰M˜!Öñ  ô ”S˜˜V“_Ó%¡d¨ dÑ+ˆ‰1ˆæ	Ü" F¨E ?°Q°DÓ9‰	ˆˆcó ‰DˆAˆqÙ�Q˜“ˆBÙ�Q˜Ÿ7™7 1™: rÓ*ˆAÙ�Q˜˜1Ÿ7™7 1™:¨ sÓ+ŠAñ ô ˜—>ˆàÑÙ�QŒKá�A‹wˆô �t F™]Ó+ˆà�8Ó ¨1£¶à�F˜1“I‘ˆAÙ˜“7ˆDà�8ÓØˆGÚà”�Qœ˜R›‘[ !Ó$Ñ$ˆáÔšB‘D�A�q‹a™BˆÑð	Ü'.¨vØ/0°©vØ/1Ø7=Ü47¸À6¹kÓ4JÈ4Ñ4OØ24ñ(6Ñ$ˆAˆq�!�R˜˜Q ð �‰IˆAð4 �‰U�1�Q‘4‰ZˆÜ˜˜A˜B˜  1 2 Ö'‰EˆAˆrÙ�a˜˜RŸX™X a™[¨"Ó-ŠBñ (à�U‰U�1‹XˆÜ˜2˜r–{‰GˆB�Ø‰DˆAˆqÙ�a˜˜RŸX™X a™[¨3¨$Ó/ŠBñ #ô
 �[Š[ Ó*Ø—‘�q—u‘u˜Q“x“ˆB÷ +à�‰U�R˜‘U‰]ˆÜ˜"˜Q˜R˜& " Q R &Ö)‰FˆAˆsÙ�a˜˜RŸX™X a™[¨#Ó.ŠBñ *ð
	Ø‘d˜2“h‘JˆEÜ—;’;˜u×%Ñ%Ü(Ó*Ð*ð &ñ �%˜‹_ˆÙ�%˜‹_ˆñ �B˜“
ˆÙ��Q˜Ÿ™ ™
 U FÓ+ˆÙ��QŸ™ ™
 EÓ*ˆð �xÓÜ�b“'˜Q“,¦2Ø�q�Eô �b“'˜Q“,§2 2ùà˜Ô#Ü�2‹w˜!Œ|§ä˜!˜C˜R˜C¢˜E™(Ÿ*™* a§c¡cÓ*×,Ñ,�Ü! !›f‘��5˜!ð �Ü% aª¨$¨1¨Q©3¨$¨¡i§k¡kÖ2‘D�A�qØ˜a™5‘D�A�qØ˜A˜a™D™�AØ˜A˜a™D™�AÜ#& r¨!¨" v¨q°°¨uÖ#5™˜˜RØ!$™˜˜BÙ   Q¨¯©°©
°BÓ7˜Ù   Q¨¯©°©
°BÓ7šñ $6ó #)™˜˜BÙ # B¨£
˜Ù   Q¨¯©°©
°U°FÓ;˜Ù   Q¨¯©°©
°U°FÓ;šñ #)ñ ! ›G�EÙ˜S ™Y¨Ó*�AÙ˜S ™Y¨Ó*�Aà—M‘M 1 a &Ö)ñ) 3ð* �‘1�ð 	�	‰	�2�r�(×ñ{ "ð@ ‡I�Iˆt�Q—V‘V“XÐÔÞÙ*,Ô-ª"¡  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 4¨b¸$À$ÐQUØ�D˜T¨U¸X÷r   