a
    –¥»aE	  ã                   @   s¾   d Z ddlmZ ddlZddlmZmZ dd„ Zdd„ Zee eed	œd
d„Zdd„ Z	dd„ Z
dd„ Zeee eedœdd„Zee edœdd„Zdd„ Zdeee eeef dœdd„ZdS )z:Various linear algebra utility methods for internal use.

é    )ÚTensorN)ÚOptionalÚTuplec                 C   sD   t | tjƒr| jtjkS d}tj ¡ s8|d t| ƒ¡7 }t	|ƒ‚dS )z$Check if tensor A is a sparse tensorzexpected Tensorz but got {}N)
Ú
isinstanceÚtorchr   ZlayoutZ
sparse_cooZjitZis_scriptingÚformatÚtypeÚ	TypeError)ÚAZ	error_str© r   úM/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/_linalg_utils.pyÚ	is_sparse   s    
r   c                 C   s$   | j }|tjtjtjfv r|S tjS )zTReturn the floating point dtype of tensor A.

    Integer types map to float32.
    )Údtyper   Úfloat16Úfloat32Úfloat64)r
   r   r   r   r   Úget_floating_dtype   s    r   )r
   ÚBÚreturnc                 C   s.   | du r|S t | ƒr"tj | |¡S t | |¡S )ziMultiply two matrices.

    If A is None, return B. A can be sparse or dense. B is always
    dense.
    N)r   r   ÚsparseÚmmÚmatmul)r
   r   r   r   r   r       s
    r   c                 C   s   |   ¡ r|  ¡ S | S )z]Return conjugate of tensor A.

    .. note:: If A's dtype is not complex, A is returned.
    )Z
is_complexZconj©r
   r   r   r   Ú	conjugate-   s    r   c                 C   s   t | jƒ}|  |d |d ¡S )z9Return transpose of a matrix or batches of matrices.
    é   é   )ÚlenÚshapeÚ	transpose)r
   Úndimr   r   r   r   7   s    
r   c                 C   s   t t| ƒƒS )zCReturn transpose conjugate of a matrix or batches of matrices.
    )r   r   r   r   r   r   Útransjugate>   s    r    )ÚXr
   ÚYr   c                 C   s   t t| ƒt ||ƒƒS )z7Return bilinear form of matrices: :math:`X^T A Y`.
    )r   r   )r!   r
   r"   r   r   r   ÚbformD   s    r#   ©r
   ÚSc                 C   s   t || |ƒS )z+Return quadratic form :math:`S^T A S`.
    )r#   r$   r   r   r   ÚqformJ   s    r&   c                 C   s*   | j rtj | ¡j}ntjt | ¡Ž }|S )z*Return orthogonal basis of A columns.
    )Zis_cudar   ÚlinalgZqrÚQZorgqrZgeqrf)r
   r(   r   r   r   ÚbasisP   s    r)   F)r
   Úlargestr   c                 C   sH   |du rd}t jj| dd�\}}|r@t j|dd�}t j|dd�}||fS )z4Return eigenpairs of A with specified ordering.
    NFÚU)ZUPLO)éÿÿÿÿ)Údims)r   r'   ZeighZflip)r
   r*   ÚEÚZr   r   r   Úsymeig[   s    r0   )F)Ú__doc__r   r   Útypingr   r   r   r   r   r   r   r    r#   r&   r)   Úboolr0   r   r   r   r   Ú<module>   s   

