a
    ™¥»a¸  ã                   @   sB   d Z ddlZddlmZ ddd„Zdd„ Zdd	d
„Zddd„ZdS )z(
Laplacian of a compressed-sparse graph
é    N)Ú
isspmatrixFc                 C   s’   | j dks| jd | jd kr&tdƒ‚|rTt | jtj¡sJt | jtj¡rT|  t	¡} t
| ƒr`tnt}|rldnd}|| ||d�\}}|rŽ||fS |S )a,  
    Return the Laplacian matrix of a directed graph.

    Parameters
    ----------
    csgraph : array_like or sparse matrix, 2 dimensions
        compressed-sparse graph, with shape (N, N).
    normed : bool, optional
        If True, then compute symmetric normalized Laplacian.
    return_diag : bool, optional
        If True, then also return an array related to vertex degrees.
    use_out_degree : bool, optional
        If True, then use out-degree instead of in-degree.
        This distinction matters only if the graph is asymmetric.
        Default: False.

    Returns
    -------
    lap : ndarray or sparse matrix
        The N x N laplacian matrix of csgraph. It will be a NumPy array (dense)
        if the input was dense, or a sparse matrix otherwise.
    diag : ndarray, optional
        The length-N diagonal of the Laplacian matrix.
        For the normalized Laplacian, this is the array of square roots
        of vertex degrees or 1 if the degree is zero.

    Notes
    -----
    The Laplacian matrix of a graph is sometimes referred to as the
    "Kirchoff matrix" or the "admittance matrix", and is useful in many
    parts of spectral graph theory. In particular, the eigen-decomposition
    of the laplacian matrix can give insight into many properties of the graph.

    Examples
    --------
    >>> from scipy.sparse import csgraph
    >>> G = np.arange(5) * np.arange(5)[:, np.newaxis]
    >>> G
    array([[ 0,  0,  0,  0,  0],
           [ 0,  1,  2,  3,  4],
           [ 0,  2,  4,  6,  8],
           [ 0,  3,  6,  9, 12],
           [ 0,  4,  8, 12, 16]])
    >>> csgraph.laplacian(G, normed=False)
    array([[  0,   0,   0,   0,   0],
           [  0,   9,  -2,  -3,  -4],
           [  0,  -2,  16,  -6,  -8],
           [  0,  -3,  -6,  21, -12],
           [  0,  -4,  -8, -12,  24]])
    é   r   é   z(csgraph must be a square matrix or array)ÚnormedÚaxis)ÚndimÚshapeÚ
ValueErrorÚnpZ
issubdtypeZdtypeZsignedintegerZuintZastypeÚfloatr   Ú_laplacian_sparseÚ_laplacian_dense)Zcsgraphr   Zreturn_diagZuse_out_degreeZ
create_lapZdegree_axisZlapÚd© r   úY/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/scipy/sparse/csgraph/_laplacian.pyÚ	laplacian   s    3ÿ
r   c                 C   s   || j d d t|ƒd …< d S )Nr   )ZflatÚlen)ÚAr   r   r   r   Ú_setdiag_denseR   s    r   c                 C   sê   | j dv r|  ¡ }d}n| }d}|j|d� ¡ | ¡  }|rª|j|d�}|dk}t |dt |¡¡}| j||j	   _| j||j
   _| jd9  _| d| ¡ n8|j d	kr¾| ¡ }n|j|d�}| jd9  _| |¡ ||fS )
N)ZlilZdokFT©r   )Úcopyr   r   éÿÿÿÿZdia)ÚformatZtocooÚsumZgetA1Zdiagonalr
   ÚwhereÚsqrtÚdataÚrowÚcolZsetdiagr   )Úgraphr   r   ÚmZ
needs_copyÚwÚisolated_node_maskr   r   r   r   V   s(    



r   c                 C   s’   t  | ¡}t  |d¡ |j|d�}|rx|dk}t  |dt  |¡¡}|| }||d d …t jf  }|d9 }t|d| ƒ n|d9 }t||ƒ ||fS )Nr   r   r   r   )r
   ÚarrayZfill_diagonalr   r   r   Znewaxisr   )r   r   r   r    r!   r"   r   r   r   r   p   s    

r   )FFF)Fr   )Fr   )	Ú__doc__Únumpyr
   Zscipy.sparser   r   r   r   r   r   r   r   r   Ú<module>   s   	
B
