a
    ßñhç	  ã                   @   s~   d Z ddlmZmZ ddlZddlmZ dd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zddd„ZdS )z:Various linear algebra utility methods for internal use.

é    )ÚOptionalÚTupleN)ÚTensorc                 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   úS/home/yuhw/wc/uboone-dl-vtx/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   c                 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
   ÚBr   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   8   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   c                 C   s   t t| ƒt ||ƒƒS )z7Return bilinear form of matrices: :math:`X^T A Y`.
    )r   r   )ÚXr
   ÚYr   r   r   ÚbformE   s    r"   c                 C   s   t || |ƒS )z+Return quadratic form :math:`S^T A S`.
    )r"   )r
   ÚSr   r   r   ÚqformL   s    r$   c                 C   s.   | j rtj| dd�\}}ntjt | ¡Ž }|S )z*Return orthogonal basis of A columns.
    T)Zsome)Zis_cudar   ZqrZorgqrZgeqrf)r
   ÚQÚ_r   r   r   ÚbasisS   s    r'   FTc                 C   sR   |du rd}|du rd}t  | |d¡\}}|rJt j|dd�}t j|dd�}||fS )z4Return eigenpairs of A with specified ordering.
    NFT)éÿÿÿÿ)Údims)r   ÚsymeigZflip)r
   ZlargestZeigenvectorsÚEÚZr   r   r   r*   ^   s    r*   )FT)Ú__doc__Útypingr   r   r   r   r   r   r   r   r   r   r"   r$   r'   r*   r   r   r   r   Ú<module>   s   

