
    
9j:I                    `    S SK Jr  S SKrS SKrS SKJr  S SK	J
r  S rS rS rS rS	 rSS
 jrg)    )annotationsN)cublas)devicec                   SSK Jn  U R                  S:w  a$  [        SR	                  U R                  5      5      eUR                  S;  a$  [        SR	                  UR                  5      5      eU R
                  S   U R
                  S   :w  a  [        S5      eU R
                  S   UR
                  S   :w  a/  [        S	R	                  U R
                  UR
                  5      5      eU R                  UR                  :w  a/  [        S
R	                  U R                  UR                  5      5      eU R                  nUS:X  a  SnO?US:X  a  SnO6US:X  a  SnO-US:X  a  SnO$[        SR	                  U R                  5      5      e[        X$S-   5      n[        X$S-   5      n[        X$S-   5      nUR
                  S   nUR                  S:X  a  UR
                  S   OSn	U R                  (       a  [        R                  n
O-U R                  (       a  [        R                  n
O[        S5      eUR                  (       d  [        S5      e[        R                  " 5       n[         R"                  " U[$        R&                  S9n[         R"                  " S[$        R&                  S9nU" XXR(                  R*                  U5      n[         R"                  " XR                  S9nU" XXR(                  R*                  XR(                  R*                  UR(                  R*                  UR(                  R*                  5        [         R,                  R.                  R1                  Xm5        U" XXU R(                  R*                  UUR(                  R*                  UR(                  R*                  XR(                  R*                  5
        [         R,                  R.                  R1                  X}5        g)a  Solve a linear matrix equation using cusolverDn<t>getr[fs]().

Computes the solution to a system of linear equation ``ax = b``.

Args:
    a (cupy.ndarray): The matrix with dimension ``(M, M)``.
    b (cupy.ndarray): The matrix with dimension ``(M)`` or ``(M, K)``.

Returns:
    cupy.ndarray:
        The matrix with dimension ``(M)`` or ``(M, K)``.

Note: ``a`` and ``b`` will be overwritten.
r   cusolver   a.ndim must be 2 (actual: {}))   r	   "b.ndim must be 1 or 2 (actual: {})r   za must be a square matrix.shape mismatch (a: {}, b: {}).zdtype mismatch (a: {}, b: {})fsdFcDzzunsupported dtype (actual:{})getrf_bufferSizegetrfgetrsz'a must be F-contiguous or C-contiguous.zb must be F-contiguous.dtypeN)cupy_backends.cuda.libsr   ndim
ValueErrorformatshaper   	TypeErrorgetattr_f_contiguous_cublasCUBLAS_OP_N_c_contiguousCUBLAS_OP_T_deviceget_cusolver_handle_cupyempty_numpyint32dataptrlinalg_util3_check_cusolver_dev_info_if_synchronization_allowed)ab	_cusolverr   thelperr   r   nnrhstranshandledipivdinfolworkdworks                   F/home/wildlama/miniconda3/lib/python3.13/site-packages/cupyx/lapack.pygesvr?   
   s    >vv{8??GHHvvV=DDQVVLMMwwqzQWWQZ566wwqzQWWQZ913 	3ww!''7qww02 	2GGE|	#	#	#7>>qwwGHHY$6 67FI7{+EI7{+E	
A11771:!D##	
##BCC??233((*FKK.EKK.E6aQ/EKKWW-E	&Q

Azz~~uzz~~
**..	LLJJ 
&!&&**a
**..!&&**a9	LLJJ    c                   SSK Jn  U R                  S:w  a$  [        SR	                  U R                  5      5      eUR                  S:X  a  SnODUR                  S:X  a  UR
                  S   nO$[        SR	                  UR                  5      5      eU R
                  S   UR
                  S   :w  a/  [        SR	                  U R
                  UR
                  5      5      eU R                  UR                  :w  a/  [        SR	                  U R                  UR                  5      5      eU R                  nUS	:X  a  S
nO5US:X  a  SnO,US:X  a  SnO#US:X  a  SnO[        SR	                  U5      5      e[        X%S-   5      n[        X%S-   5      n[        [        US-   5      nUS;   a  [        X%S-   5      n	[        X%S-   5      n
O[        X%S-   5      n	[        X%S-   5      n
[        R                  nUR                  S;   a  [        R                  nO[        R                  nU R
                  u  p[        X5      n[        R                  " S[         R"                  S9n[        R                  " XS9n[$        R&                  " 5       n[$        R(                  " 5       n[         R*                  " SUS9nX:  Gau  U R-                  SS9n UR-                  SS9nU" UXU R.                  R0                  U5      n[        R                  " UUS9nU" UXU R.                  R0                  UUR.                  R0                  UR.                  R0                  UUR.                  R0                  5	        [        R2                  R4                  R7                  UU5        U	" U[        R8                  XX?U R.                  R0                  UUR.                  R0                  UR.                  R0                  U5      n[        R                  " UUS9nU
" U[        R8                  XUXR.                  R0                  UUR.                  R0                  UR.                  R0                  UUR.                  R0                  UUR.                  R0                  5        [        R2                  R4                  R7                  U
U5        U" U[        R8                  [        R:                  U[        R<                  XUR>                  R.                  U R.                  R0                  XR.                  R0                  U5        USU $ U RA                  5       RB                  R-                  SS9n UnUR                  S:X  a  U4OX4n[        RD                  " UUSS9nUUSU& U" UXU R.                  R0                  U5      n[        R                  " UUS9nU" UXU R.                  R0                  UUR.                  R0                  UR.                  R0                  UUR.                  R0                  5	        [        R2                  R4                  R7                  UU5        U" U[        R8                  [        R:                  U[        R<                  XUR>                  R.                  U R.                  R0                  XR.                  R0                  U5        U	" U[        R8                  XUXR.                  R0                  UUR.                  R0                  UR.                  R0                  U5      n[        R                  " UUS9nU
" U[        R8                  XUXR.                  R0                  UUR.                  R0                  UR.                  R0                  UUR.                  R0                  UUR.                  R0                  5        [        R2                  R4                  R7                  U
U5        U$ )aw  Solves over/well/under-determined linear systems.

Computes least-square solution to equation ``ax = b` by QR factorization
using cusolverDn<t>geqrf().

Args:
    a (cupy.ndarray): The matrix with dimension ``(M, N)``.
    b (cupy.ndarray): The matrix with dimension ``(M)`` or ``(M, K)``.

Returns:
    cupy.ndarray:
        The matrix with dimension ``(N)`` or ``(N, K)``.
r   r   r	   r
   r   r   r   zdtype mismatch (a: {}, b: {}).r   r   r   r   r   r   r   zunsupported dtype (actual: {})geqrf_bufferSizegeqrftrsmsdormqr_bufferSizeormqrunmqr_bufferSizeunmqrfdr   g      ?)orderNr   rK   )#r   r   r   r   r   r   r   r    r"   r#   charr%   CUBLAS_OP_Cminr(   r)   r*   r+   r&   r'   get_cublas_handlearraycopyr,   r-   r.   r/   r0   CUBLAS_SIDE_LEFTCUBLAS_FILL_MODE_UPPERCUBLAS_DIAG_NON_UNITctypesconjTzeros)r1   r2   r3   r7   r   r4   geqrf_helperrC   rD   ormqr_helperrG   no_transr8   mr6   mn_mindev_infotaucusolver_handlecublas_handleonews_size	workspacebb	out_shapes                            r>   gelsrh   R   s    >vv{8??GHHvv{	
1wwqz=DDQVVLMMwwqzQWWQZ913 	3ww!''913 	3 GGE|	#	#	#9@@GHH9*<&<=LI7{+E7AJ'DDyy.@*@A	w;/y.@*@A	w;/""HzzT####77DAYF{{1FLL1H
++f
*C113O--/M
,,s%
(CvFFFFFF affjj!DKKu5	oqQVVZZCHHLLnn  '8==+<+<	>NN8	 W55uFFJJ388<<Q8 KKu5	ow774ffjj!SXX\\166::qnn  '8==+<+<	> 	NN8	 	]G44++X))6ZZ__affjj!VVZZ	<
 !u FFHJJOO#O&FFaKQDaY	KK	c:"1 affjj!DKKu5	oqQVVZZCHHLLnn  '8==+<+<	>NN8	 	]G44++U))1ZZ__affjj!VVZZ	< W55xDFFJJ388<<Q@ KKu5	ow77dffjj!SXX\\166::qnn  '8==+<+<	> 	NN8	 r@   c                   SSK Jn  SS KnUR                  R                  S5      (       d  [	        S5      e[
        R                  " U R                  UR                  5      n[
        R                  " US5      nUS:X  a  UR                  nUR                  nOUS:X  a  UR                  nUR                  nOdUS:X  a  UR                  nUR                  nOEUS:X  a  UR                  nUR                  nO&S	R!                  U R                  5      n[#        U5      eU R%                  US
SS9n [&        R(                  R+                  U 5      nU R,                  SS  u  p[/        [
        R0                  " U R,                  S S 5      5      n[2        R4                  " 5       n[6        R8                  n[&        R:                  " U[
        R<                  S9nU" XXR>                  R@                  XR>                  R@                  U5        [&        RB                  RD                  RG                  X^5        UR,                  nURI                  5       RK                  XS5      R%                  US
SS9n[&        R(                  R+                  U5      nUR,                  SS  u  nn[&        R:                  " S[
        R<                  S9nU" XU
UUR>                  R@                  U	UR>                  R@                  UUR>                  R@                  U5
        [&        RB                  RD                  RG                  Xn5        URI                  5       RK                  U5      $ )Nr   r   potrsBatchedpotrsBatched is not availabler   r   r   r   Ddtype must be float32, float64, complex64 or complex128 (actual: {})CTrK   rR   r   r   )&r   r   cupyx.cusolvercheck_availabilityRuntimeErrorr*   promote_typesr   spotrfBatchedspotrsBatcheddpotrfBatcheddpotrsBatchedcpotrfBatchedcpotrsBatchedzpotrfBatchedzpotrsBatchedr   r   astyper(   _core	_mat_ptrsr   intprodr&   r'   r"   CUBLAS_FILL_MODE_LOWERr)   r+   r,   r-   r.   r/   r0   rW   reshape)r1   r2   r3   cupyxr   potrfBatchedrj   msgapldar6   
batch_sizer9   uplor_   b_shapebpldbr7   s                      r>   _batched_posvr      s   =>>,,^<<:;;  !''2E  ,E| .. ..	# .. ..	# .. ..	# .. ..%vagg 	o	c-A			q	!BWWRS\FCV[["./J((*F))D{{:V\\:H q''++sMM4E4E	LLJJ  ggG	+225$2OA			q	!BIC{{1FLL1H q$S"''++s""J0	LLJJ  668G$$r@   c                   SSK Jn  [        R                  R                  nUR                  X5        UR                  U 5        UR                  U 5        U R                  S:  a  [        X5      $ [        R                  " U R                  UR                  5      n[        R                  " US5      nUS:X  a%  UR                  nUR                  nUR                  nOUS:X  a%  UR                   nUR"                  nUR$                  nO|US:X  a%  UR&                  nUR(                  nUR*                  nOQUS:X  a%  UR,                  nUR.                  nUR0                  nO&SR3                  U R                  5      n[5        U5      eU R7                  USS	S
9n U R8                  u  p[:        R<                  " 5       n[>        R@                  n[        RB                  " S[        RD                  S9nU" XXRF                  RH                  U	5      n[        RB                  " XS9nU" XXRF                  RH                  XRF                  RH                  XRF                  RH                  5        [        R                  R                  RK                  X]5        UR8                  nURM                  U
S5      R7                  USS	S
9nUR8                  u  nnU" XU
UU RF                  RH                  XRF                  RH                  UURF                  RH                  5	        [        R                  R                  RK                  X}5        [        RN                  " URM                  U5      5      $ )a$  Solve the linear equations A x = b via Cholesky factorization of A,
where A is a real symmetric or complex Hermitian positive-definite matrix.

If matrix ``A`` is not positive definite, Cholesky factorization fails
and it raises an error.

Note: For batch input, NRHS > 1 is not currently supported.

Args:
    a (cupy.ndarray): Array of real symmetric or complex hermitian
        matrices with dimension (..., N, N).
    b (cupy.ndarray): right-hand side (..., N) or (..., N, NRHS).
Returns:
    x (cupy.ndarray): The solution (shape matches b).
r   r   r	   r   r   r   r   rl   Trn   r   r   rp   )(r   r   r(   r.   r/   _assert_cupy_array_assert_stacked_2d_assert_stacked_squarer   r   r*   rt   r   spotrfspotrf_bufferSizespotrsdpotrfdpotrf_bufferSizedpotrscpotrfcpotrf_bufferSizecpotrszpotrfzpotrf_bufferSizezpotrsr   r   r}   r   r&   r'   r"   r   r)   r+   r,   r-   r0   r   ascontiguousarray)r1   r2   r3   r/   r   potrfpotrf_bufferSizepotrsr   r   r6   r9   r   r_   worksizere   r   r   r7   s                      r>   posvr     s     >LLE	Q"	Q	  #vvzQ""  !''2E  ,E|  $66  	#  $66  	#  $66  	#  $66  %vagg 	o	c-AWWFC((*F))D{{1FLL1HaSAHH2I 
&66::sNN,>,>
MM%%'	LLJJ ggG			!RSt<AIC 
&4S&&**c
--

	LLJJ ""199W#566r@   c                   SSK Jn  [        R                  R                  nUR                  X5        UR                  U 5        UR                  U 5        U R                  S:  a  [        XU5      $ U R                  S   nUR                  nUR                  S:X  a	  USS2S4   nUR                  S:X  d   S5       eUR                  S   U:X  d   S5       e[        R                  " U R                  UR                  5      n[        R                  " US	5      nU R                  US
S9UR                  US
S9pU R                  R                   (       d1  U R                  R"                  (       d  [        R$                  " U 5      n U R                  R"                  (       aB  U(       + nUR&                  U R&                  :  a  UR)                  5       nOU R)                  5       n UR                  U:w  d  UR                  R                   (       d  [        R$                  " U5      nUS	:X  a  UR*                  nO_US:X  a  UR,                  nOLUS:X  a  UR.                  nO9US:X  a  UR0                  nO&SR3                  U R                  5      n	[5        U	5      e[6        R8                  " 5       n
[        R:                  " S[        R<                  S9nU" U
U(       a  [>        R@                  O[>        RB                  U R                  S   UR                  S   U RD                  RF                  U R                  S   URD                  RF                  UR                  S   URD                  RF                  5	        [        R                  R                  RI                  X5        U R                  R"                  (       a*  UR&                  U R&                  :  a  UR)                  5       nURK                  U5      $ )a  Implements lapack XPOTRS through cusolver.potrs. Solves linear system
A x = b given the cholesky decomposition of A, namely L. Supports also
batches of linear systems and more than one right-hand side (NRHS > 1).

Args:
    L (cupy.ndarray): Array of Cholesky decomposition of real symmetric or
        complex hermitian matrices with dimension (..., N, N).
    b (cupy.ndarray): right-hand side (..., N) or (..., N, NRHS). Note that
        this array may be modified in place, as usually done in LAPACK.
    lower (bool): If True, L is lower triangular. If False, L is upper
        triangular.

Returns:
    cupy.ndarray: The solution to the linear system. Note this may point to
        the same memory as b, since b may be modified in place.

.. warning::
    This function calls one or more cuSOLVER routine(s) which may yield
    invalid results if input conditions are not met.
    To detect these invalid results, you can set the `linalg`
    configuration to a value that is not `ignore` in
    :func:`cupyx.errstate` or :func:`cupyx.seterr`.

r   r   r	   rp   r   Nzb is not a vector or a matrixz.length of arrays in b does not match size of Lr   F)rR   r   r   r   rl   r   )&r   r   r(   r.   r/   r   r   r   r   _batched_potrsr   r*   rt   r   r}   flagsf_contiguousc_contiguousasfortranarraysizerW   r   r   r   r   r   r   r&   r'   r)   r+   r"   r   rT   r,   r-   r0   r   )Lr2   lowerr3   r/   r6   r   r   r   r   r9   r_   s               r>   r   r   a  s   4 >LLE	Q"	Q	  # 	vvzaE** 	
AggGvv{agJ66Q;777;771:?LLL?   !''2E  ,E88E8&U(CqGG  177+?+?  #ww	66AFF?AA	5!''"6"6  # |  	#  	#  	#  %vagg 	o((*F{{1EKK0H	*/&&&&	
	
	

	
	

	

 
LLJJ 	wwFFH99Wr@   c                   SSK Jn  SS KnUR                  R                  S5      (       d  [	        S5      eUR
                  U R
                  S-
  :  d   S5       eUR                  nUR
                  U R
                  :  a  US   nUR                  S S U R                  S S :X  d   S	5       e[        R                  " U R                  UR                  5      n[        R                  " US
5      nU R                  USSS9n UR                  USSS9nU R                  R                  (       a  UR                  R                  (       d   S5       eU(       + nUS
:X  a  UR                  nO_US:X  a  UR                  nOLUS:X  a  UR                  nO9US:X  a  UR                   nO&SR#                  U R                  5      n[%        U5      e[&        R(                  " 5       n	[*        R,                  " S[        R.                  S9n
[        R0                  " U R                  S S 5      nU R                  S   nUR3                  5       n[*        R4                  R7                  U 5      nUR                  S   nUS:X  a  US   nO-[*        R,                  " UR                  S S UR                  SS9n[*        R4                  R7                  US   5      n[9        UR                  S   5       H  nUS:  a
  USU4   US'   U" U	U(       a  [:        R<                  O[:        R>                  USUR@                  RB                  U R                  S   UR@                  RB                  UR                  S   U
R@                  RB                  U5
        [*        RD                  RF                  RI                  Xz5        US:  a  UR3                  5       USU4'   M  UR3                  5       nM     URK                  U5      $ )Nr   r   rj   rk   r   z8Batch dimension of b is different than that         of L).Nro   z'Batch dimension of L and b do not matchr   rm   Frn   z"Unexpected non C-contiguous arraysr   r   r   rl   r   rp   ).r   rL   .)&r   r   rq   rr   rs   r   r   r*   rt   r   r}   r   r   rv   rx   rz   r|   r   r   r&   r'   r(   r)   r+   r   rW   r~   r   ranger"   r   rT   r,   r-   r.   r/   r0   r   )r   r2   r   r3   r   r   r   rj   r   r9   r_   r   r6   L_pr7   b_tmpb_tmp_pis                     r>   r   r     s8   =>>,,^<<:;; 66QVVAX    ggGvviL773B<1773B<' 212'   !''2E  ,E	c.A	c.A77AGG$8$8 -,-8IE | ..	# ..	# ..	# ..%vagg 	o ((*F{{1FLL1HQWWSb\*J	A	A
++


"C772;D qy&	AGGCRLsCkk##E)$45G 1772;!836E#J.3G****HHLLGGBKLLKKOMM	
 	NN	$ !8

Ac1fI

A1  6 99Wr@   )r   bool)
__future__r   numpyr*   cupyr(   r   r   r"   	cupy.cudar   r&   r?   rh   r   r   r   r    r@   r>   <module>r      s<    "   5 'EPBJ8%vL7^_DSr@   