o
    װi0                     @   sL   d dl Zd dlZd dlmZ d dlmZ	 dd Z
dd Zdd	 Zd
d ZdS )    N)cublas)devicec                 C   s0  ddl m} | jdkrtd| j|jdvr td|j| jd | jd kr.td| jd |jd krBtd	| j|j| j|jkrRtd
| j|j| j}|dkr\d}n|dkrcd}n|dkrjd}n|dkrqd}ntd| jt||d }t||d }t||d }|jd }|jdkr|jd nd}	| j	rt
j}
n| jrt
j}
ntd|j	stdt }tj|tjd}tjdtjd}||||| jj|}tj|| jd}||||| jj||jj|jj|jj tjj|| |||
||	| jj||jj|jj||jj
 tjj|| dS )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dwork r;   @/home/ubuntu/.local/lib/python3.10/site-packages/cupyx/lapack.pygesv   sl   


r=   c                 C   s\  ddl m} | jdkrtd| j|jdkrd}n|jdkr&|jd }ntd|j| jd |jd krBtd| j|j| j|jkrRtd| j|j| j}|d	kr\d
}n|dkrcd}n|dkrjd}n|dkrqd}ntd|t||d }t||d }tt|d }|dv rt||d }	t||d }
nt||d }	t||d }
tj	}|j
dv rtj}ntj}| j\}}t||}tjdtjd}tj||d}t }t }tjd|d}||kr| jdd} |jdd}||||| jj|}tj||d}||||| jj||jj|jj||jj	 tjj|| |	|tj||||| jj||jj|jj|}tj||d}|
|tj||||| jj||jj|jj||jj||jj tjj|
| ||tjtj|tj|||jj| jj||jj| |d| S |   j!jdd} |}|jdkr|fn||f}tj"||dd}||d|< ||||| jj|}tj||d}||||| jj||jj|jj||jj	 tjj|| ||tjtj|tj|||jj| jj||jj| |	|tj||||| jj||jj|jj|}tj||d}|
|tj||||| jj||jj|jj||jj||jj tjj|
| |S )a  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   rG   )#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,   r-   CUBLAS_SIDE_LEFTCUBLAS_FILL_MODE_UPPERCUBLAS_DIAG_NON_UNITctypesconjTzeros)r.   r/   r0   r4   r   r1   geqrf_helperr?   r@   ormqr_helperrC   no_transr5   mr3   mn_mindev_infotaucusolver_handlecublas_handleonews_size	workspacebb	out_shaper;   r;   r<   gelsP   s   






rc   c                 C   s  ddl m} dd l}|jdstdt| j|j}t|d}|dkr-|j}|j	}n+|dkr8|j
}|j}n |dkrC|j}|j}n|dkrN|j}|j}n
d	| j}t|| j|d
dd} tj| }| jdd  \}	}
tt| jd d }t }tj}tj|tjd}||||
|jj |	|jj | tj!j"#|| |j}|$ %||
dj|d
dd}tj|}|jdd  \}}tjdtjd}||||
||jj |	|jj ||jj |
 tj!j"#|| |$ %|S )Nr   r   potrsBatchedzpotrsBatched is not availabler   r   r   r   Ddtype must be float32, float64, complex64 or complex128 (actual: {})CTrG   rM   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,   r-   rR   reshape)r.   r/   r0   cupyxr   potrfBatchedrd   msgapldar3   
batch_sizer6   uplorZ   b_shapebpldbr4   r;   r;   r<   _batched_posv   s^   r   c              
   C   s  ddl m} tjj}|| | ||  ||  | jdkr$t	| |S t
| j|j}t
|d}|dkr@|j}|j}|j}n4|dkrN|j}|j}|j}n&|dkr\|j}|j}|j}n|dkrj|j}|j}|j}n
d| j}t|| j|dd	d
} | j\}	}
t }tj }tj!dt
j"d}||||
| j#j$|	}tj!||d}||||
| j#j$|	|j#j$||j#j$ tjj%|| |j}|&|
dj|dd	d
}|j\}}||||
|| j#j$|	|j#j$||j#j$	 tjj%|| t'|&|S )aP  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   re   Trg   r   r   ri   )(r   r   r%   r+   r,   _assert_cupy_array_assert_stacked_2d_assert_stacked_squarer   r   r'   rm   r   spotrfspotrf_bufferSizespotrsdpotrfdpotrf_bufferSizedpotrscpotrfcpotrf_bufferSizecpotrszpotrfzpotrf_bufferSizezpotrsr   r   rv   r   r#   r$   r   r{   r&   r(   r)   r*   r-   r|   ascontiguousarray)r.   r/   r0   r,   r   potrfpotrf_bufferSizepotrsr   r   r3   r6   r   rZ   worksizer`   r   r   r4   r;   r;   r<   posv  sh   





r   )numpyr'   cupyr%   r   r   r   	cupy.cudar   r#   r=   rc   r   r   r;   r;   r;   r<   <module>   s    H ;