Ë
    0Ò^jü=  ã                   ó¦   — d dl Zd dlmZ d dlmZmZmZmZm	Z	m
Z
 ddlmZ d dlmZ d dlmZ dgZ	 	 dd
„Z ed¬«      dddddddddd	ddœ
d„«       Zy)é    N)ÚLinAlgError)Úget_blas_funcsÚqrÚsolveÚsvdÚ	qr_insertÚlstsqé   )Ú_get_atol_rtol)Úmake_system)Úxp_capabilitiesÚgcrotmkFc	           
      ó”  — |€d„ }|€d„ }t        g d¢|f«      \  }	}
}}|g}g }d}t        j                  }|t        |«      z   }t        j                  t        |«      |f|j
                  ¬«      }t        j                  d|j
                  ¬«      }t        j                  d|j
                  ¬«      }t        j                  |j
                  «      j                  }d}t        |«      D �]^  }|r|t        |«      k  r	||   \  }}nS|r|t        |«      k(  r ||«      }d}n8|s)||t        |«      z
  k\  r|||t        |«      z
  z
     \  }}n ||d	   «      }d}|€ | | |«      «      }n|j                  «       } ||«      }t        |«      D ].  \  }} |
||«      }||||f<    |	|||j                  d
   | «      }Œ0 t        j                  |dz   |j
                  ¬«      }t        |«      D ],  \  }} |
||«      }|||<    |	|||j                  d
   | «      }Œ.  ||«      |dz   <   t        j                  dd¬«      5  d|d	   z  }ddd«       t        j                  «      r	 |||«      }|d	   ||z  kD  sd}|j                  |«       |j                  |«       t        j                  |dz   |dz   f|j
                  d¬«      }||d|dz   …d|dz   …f<   d||dz   |dz   f<   t        j                  |dz   |f|j
                  d¬«      } || d|dz   …dd…f<   t!        || ||ddd¬«      \  }}t#        |d   «      }||k  s|s�Œ_ n t        j                  ||f   «      s
t%        «       ‚t'        |d|dz   …d|dz   …f   |d
d|dz   …f   j)                  «       «      \  }}!}!}!|dd…d|dz   …f   }|||||||fS # 1 sw Y   �ŒvxY w)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                 ó   — | S ©N© ©Úxs    úo/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/scipy/sparse/linalg/_isolve/_gcrotmk.pyÚlpsolvez_fgmres.<locals>.lpsolveB   ó   € ØˆHó    c                 ó   — | S r   r   r   s    r   Úrpsolvez_fgmres.<locals>.rpsolveE   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ÚRr3   Ú	breakdownÚjÚzÚwÚw_normÚiÚcÚalphaÚhcurÚvÚQ2ÚR2Ú_s"                                     r   Ú_fgmresrX      s  € ðb €ò	à€ò	ô +Ò+JÈRÈEÓRÑ€Dˆ#ˆt�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ó Kˆñ ˜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÷	ô �;‰;�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Kô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	ñ 	ús   É 	N=Î=O	T)Únp_onlygñhãˆµøä>g        iè  é   Úoldest)
Úrtolr@   ÚmaxiterÚMÚcallbackr?   ÚkÚCUÚ	discard_CÚtruncatec       
         óF  — t        | |||«      \  } }}}t        j                  |«      j                  «       st	        d«      ‚|dvrt	        d|›�«      ‚| j
                  }|j
                  }|
€g }
|	€|}	d\  }}}|€|j                  «       }n| ||«      z
  }t        g d¢||f«      \  }}}} ||«      }t        d|||«      \  }}|dk(  r|}|dfS |r|
D ��cg c]	  \  }}d|f‘Œ c}}|
dd |
�rw|
j                  d	„ ¬
«       t        j                  | j                  d   t        |
«      f|j                  d¬«      }g }d}|
r@|
j                  d«      \  }}|€ ||«      }||dd…|f<   |dz  }|j                  |«       |
rŒ@t!        |ddd¬«      \  }}}~t#        |j$                  «      }g }t'        t        |«      «      D ]„  }|||      }t'        |«      D ]&  }  ||||       ||j                  d   || |f    «      }Œ( t)        |||f   «      dt)        |d   «      z  k  r n$ |d|||f   z  |«      }|j                  |«       Œ† t#        t+        ||«      «      ddd…   |
dd |
rVt        ddg|f«      \  }}|
D ]?  \  }} |||«      }! ||||j                  d   |!«      } ||||j                  d   |! «      }ŒA t'        |«      D �]Ì  }"|� ||«        ||«      }#t-        |||z  «      }$|#|$k  r|"dkD  s|
r| ||«      z
  } ||«      }#|#|$k  rd}" �n‚|t-        |	t        |
«      z
  d«      z   }%|
D ��cg c]  \  }}|‘Œ	 }}}	 t/        |||#z  |%|t-        |||z  «      |#z  |¬«      \  }}}&}'}(})}*|)|#z  })|(d   |)d   z  }+t+        |(dd |)dd «      D ]  \  },}! ||,|+|+j                  d   |!«      }+Œ |&j3                  |)«      }-t+        |
|-«      D ]#  \  }.}/|.\  }} |||+|+j                  d   |/ «      }+Œ% t        j4                  d¬«      5  |j3                  |j3                  |)«      «      }0ddd«       |'d   0d   z  }1t+        |'dd |0dd «      D ]  \  }2}3 ||2|1|1j                  d   |3«      }1Œ 	 d ||1«      z  }4t        j                  |4«      s
t7        «       ‚	  ||4|1«      }1 ||4|+«      }+ ||1|«      }5 ||1||j                  d   |5 «      } ||+||j                  d   |5«      }|dk(  r)t        |
«      |	k\  �rƒ|
�r€|
d= t        |
«      |	k\  �rn|
rŒ�ni|dk(  �rct        |
«      |	k\  �rT|
�rQt;        |dd…dd…f   j$                  |&j$                  «      j$                  }6t=        |6«      \  }7}8}9g }:t?        |7dd…d|	dz
  …f   j$                  «      D ]ä  \  }};|
d   \  }}||;d   z  }||;d   z  }t+        |
dd |;dd «      D ]:  \  }<}=|<\  }>}? ||>||j                  d   |=«      } ||?||j                  d   |=«      }Œ< |:D ]@  \  }>}? ||>|«      }4 ||>||j                  d   |4 «      } ||?||j                  d   |4 «      }ŒB  ||«      }4 |d|4z  |«      } |d|4z  |«      }|:j                  ||f«       Œæ |:|
dd |
j                  |1|+f«       �ŒÏ |
j                  d|j                  «       f«       |r|
D �@�Acg c]	  \  }@}Ad|Af‘Œ c}A}@|
dd |"dz   fS c c}}w c c}}w # t0        $ r Y  Œ\w xY w# 1 sw Y   �ŒÌxY w# t6        t8        f$ r Y �ŒOw xY wc c}A}@w )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                 ó   — | d   d uS )Nr   r   )Úcus    r   ú<lambda>zgcrotmk.<locals>.<lambda>@  s   € ˜r !™u¨DÐ0€ r   )Úkeyr&   r'   r
   TÚeconomic)Úoverwrite_aÚmodeÚpivotinggê-�™—q=)r   r   g      ð?r!   r   r   )r   r@   rA   r#   )Úinvalidr[   re   ) r   r-   r9   ÚallÚ
ValueErrorr=   r5   r   r   ÚsortÚemptyr7   r/   r    Úpopr:   r   ÚlistÚTr4   r;   ÚzipÚmaxrX   r   r   r8   ÚFloatingPointErrorÚZeroDivisionErrorr   r   r6   )BÚAÚbÚx0r\   r@   r]   r^   r_   r?   r`   ra   rb   rc   r   r=   Úpsolver   r   r   Úrr   Úb_normrQ   ÚuÚCÚusrL   rI   rJ   ÚPrA   Únew_usrP   ÚycÚj_outerÚbetaÚbeta_tolÚmlrH   rD   rE   rF   ÚpresÚuxrM   Úbyrg   ÚbycÚhyÚcxrT   ÚhycrR   ÚgammaÚDÚWÚsigmaÚVÚnew_CUrN   ÚcupÚwpÚcpÚupÚczÚuzsB                                                                     r   r   r   º   s  € ôz ˜!˜A˜b Ó#�G€A€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ˆáØ')×*™t˜q !�$˜’Ó*ˆ‰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ò ô �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˜˜1Ÿ7™7 1™: rÓ*ˆAÙ�Q˜˜1Ÿ7™7 1™:¨ sÓ+‰Að	,ô ˜“>ó }ˆàÐÙ�QŒKá�A‹wˆô �t˜T F™]Ó+ˆà�8Ò ¨1¢±à‘F˜1“I‘ˆAÙ˜“7ˆDà�8ÒØˆGÚà”�Qœ˜R›‘[ !Ó$Ñ$ˆà×‘D�A�qŠaÐˆÑð	Ü'.¨vØ/0°©vØ/1Ø7=Ü47¸¸dÀ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“{ò 	0‰GˆB�Ø‰DˆAˆqÙ�a˜˜RŸX™X a™[¨3¨$Ó/‰Bð	0ô
 �[‰[ Ô*ñ 	!Ø—‘�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‹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ò 8™˜˜RØ!$™˜˜BÙ   Q¨¯©°©
°BÓ7˜Ù   Q¨¯©°©
°BÓ7™ð8ð #)ò <™˜˜BÙ # B¨£
˜Ù   Q¨¯©°©
°U°FÓ;˜Ù   Q¨¯©°©
°U°FÓ;™ð<ñ ! ›G�EÙ˜S ™Y¨Ó*�AÙ˜S ™Y¨Ó*�Aà—M‘M 1 a &Õ)ð)*ð* �‘1�ð 	�	‰	�2�r�(Öð{}ð@ ‡I�Iˆt�Q—V‘V“XÐÔÙØ*,×-¡  B�$˜’Ó-ˆ‰1ˆàˆg˜‰kˆ>Ðùó +ùó`  øô ò 	ò ð	ú÷D	!ñ 	!ûô #Ô$5Ð6ò 	âð	üój .sB   Ã[Ì8[$Í1[*Ð![:Ñ6*\Û\Û*	[7Û6[7Û:\	Ü\Ü\)NNr   r   Fr   )Únumpyr-   Únumpy.linalgr   Úscipy.linalgr   r   r   r   r   r	   Ú	iterativer   Ú!scipy.sparse.linalg._isolve.utilsr   Úscipy._lib._array_apir   Ú__all__rX   r   r   r   r   ú<module>r¤      sm   ðó Ý $ç I× IÝ %Ý 9Ý 1ð ˆ+€ð MOØ!óg#ñT ˜Ôð 4¨b¸$À$ÐQUØ�D˜T¨U¸Xóó ñr   