o
    װi                     @   sb   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ dddZ	dd
dZ
dddZdS )    N)device)runtime)_util)_dtypeFc                 C   sv  ddl m} ddl m} |dvrtdtj| dd\}}|j }|j }	| j|d| d	}
| j	\}}t
||}t
d
tj}t j}|rM|j}n|j}|dkrX|j}n|j}tjs|jdvrgtdt|}t|}| }zF||||||||
jj|||jj|\}}t
|d}t|d}||||||||
jj|||jj||jj||jj||jj W | | n| | w t
j!j"|j| na|dkr|j#}|j$}n%|dkr|j%}|j&}n|dkr|j'}|j(}n|dkr|j)}|j*}ntd||||||
jj||jj}t
||}||||||
jj||jj|jj||jj
 t
j!j"|| |j|	dd|
j|ddfS )Nr   )cublas)cusolver)LUz UPLO argument must be 'L' or 'U'F)reject_float16F)ordercopy r   fdFDz>Only float32, float64, complex64, and complex128 are supportedbfdD)r   )+cupy_backends.cuda.libsr   r   
ValueErrorr   linalg_common_typecharlowerastypeshapecupyemptynumpyint32r   Devicecusolver_handleCUSOLVER_EIG_MODE_VECTORCUSOLVER_EIG_MODE_NOVECTORCUBLAS_FILL_MODE_LOWERCUBLAS_FILL_MODE_UPPERr   is_hipRuntimeErrorr   to_cuda_dtypecreateParamsxsyevd_bufferSizedataptrxsyevdctypesdestroyParamslinalg3_check_cusolver_dev_info_if_synchronization_allowedssyevd_bufferSizessyevddsyevd_bufferSizedsyevdcheevd_bufferSizecheevdzheevd_bufferSizezheevd)aUPLOwith_eigen_vectoroverwrite_ar   r   dtypev_dtype
real_dtypew_dtypevmldawdev_infohandlejobzuplotype_vtype_wparamswork_device_sizework_host_sizsework_device	work_hostbuffer_sizesyevd	work_sizeworkr   r   K/home/ubuntu/.local/lib/python3.10/site-packages/cupy/linalg/_eigenvalue.py_syevd
   s   








rU   r   c                 C   s   ddl }t|  t|  | jdkr5t| \}}|j }t	| j
dd |}t	| j
|}||fS | jdks=tjrK|j| |d\}}||fS t| |dS )a  
    Return the eigenvalues and eigenvectors of a complex Hermitian
    (conjugate symmetric) or a real symmetric matrix.

    Returns two objects, a 1-D array containing the eigenvalues of `a`, and
    a 2-D square array or matrix (depending on the input type) of the
    corresponding eigenvectors (in columns).

    Args:
        a (cupy.ndarray): A symmetric 2-D square matrix ``(M, M)`` or a batch
            of symmetric 2-D square matrices ``(..., M, M)``.
        UPLO (str): Select from ``'L'`` or ``'U'``. It specifies which
            part of ``a`` is used. ``'L'`` uses the lower triangular part of
            ``a``, and ``'U'`` uses the upper triangular part of ``a``.
    Returns:
        tuple of :class:`~cupy.ndarray`:
            Returns a tuple ``(w, v)``. ``w`` contains eigenvalues and
            ``v`` contains eigenvectors. ``v[:, i]`` is an eigenvector
            corresponding to an eigenvalue ``w[i]``. For batch input,
            ``v[k, :, i]`` is an eigenvector corresponding to an eigenvalue
            ``w[k, i]`` of ``a[k]``.

    .. 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`.

    .. seealso:: :func:`numpy.linalg.eigh`
    r   N   Tcupyx.cusolverr   _assert_stacked_2d_assert_stacked_squaresizer   r   r   r   r   r   ndimr   r%   r   syevjrU   )r9   r:   cupyx_r>   r@   rD   rA   r   r   rT   eigh^   s    



ra   c                 C   s   ddl }t|  t|  | jdkr*t| \}}|j }t	| j
dd |S | jdks2tjr:|j| |dS t| |dd S )a-  
    Compute the eigenvalues of a complex Hermitian or real symmetric matrix.

    Main difference from eigh: the eigenvectors are not computed.

    Args:
        a (cupy.ndarray): A symmetric 2-D square matrix ``(M, M)`` or a batch
            of symmetric 2-D square matrices ``(..., M, M)``.
        UPLO (str): Select from ``'L'`` or ``'U'``. It specifies which
            part of ``a`` is used. ``'L'`` uses the lower triangular part of
            ``a``, and ``'U'`` uses the upper triangular part of ``a``.
    Returns:
        cupy.ndarray:
            Returns eigenvalues as a vector ``w``. For batch input,
            ``w[k]`` is a vector of eigenvalues of matrix ``a[k]``.

    .. 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`.

    .. seealso:: :func:`numpy.linalg.eigvalsh`
    r   NrV   rW   FrX   )r9   r:   r_   r`   r>   r@   r   r   rT   eigvalsh   s   



rb   )F)r   )r   r   	cupy.cudar   r   cupy.linalgr   
cupy._corer   rU   ra   rb   r   r   r   rT   <module>   s    

T5