B
    ,[P                 @   s~   d Z ddlZddlmZ dddddgZd	d
dgZedddd	Zedddd
Z	ededdddZ
dd ZdS )zLaplacian matrix of graphs.
    N)not_implemented_for
z%Aric Hagberg <aric.hagberg@gmail.com>zPieter Swart (swart@lanl.gov)z Dan Schult (dschult@colgate.edu)z3Alejandro Weinstein <alejandro.weinstein@gmail.com>laplacian_matrixnormalized_laplacian_matrixdirected_laplacian_matrixZdirectedweightc       	      C   sd   ddl }|dkrt| }tj| ||dd}|j\}}|jdd}|jj| dg||dd}|| S )aE  Return the Laplacian matrix of G.

    The graph Laplacian is the matrix L = D - A, where
    A is the adjacency matrix and D is the diagonal matrix of node degrees.

    Parameters
    ----------
    G : graph
       A NetworkX graph

    nodelist : list, optional
       The rows and columns are ordered according to the nodes in nodelist.
       If nodelist is None, then the ordering is produced by G.nodes().

    weight : string or None, optional (default='weight')
       The edge data key used to compute each value in the matrix.
       If None, then each edge has weight 1.

    Returns
    -------
    L : SciPy sparse matrix
      The Laplacian matrix of G.

    Notes
    -----
    For MultiGraph/MultiDiGraph, the edges weights are summed.

    See Also
    --------
    to_numpy_matrix
    normalized_laplacian_matrix
    r   Ncsr)nodelistr   format   )axis)r
   )	scipy.sparselistnxto_scipy_sparse_matrixshapesumsparsespdiagsflatten)	Gr	   r   scipyAnmdiagsD r   >lib/python3.7/site-packages/networkx/linalg/laplacianmatrix.pyr      s    "

c          	   C   s   ddl }ddl}|dkr t| }tj| ||dd}|j\}}|jdd }|jj	|dg||dd}|| }	|j
dd	 d
|| }
W dQ R X d|
||
< |jj	|
dg||dd}||	|S )aR  Return the normalized Laplacian matrix of G.

    The normalized graph Laplacian is the matrix

    .. math::

        N = D^{-1/2} L D^{-1/2}

    where `L` is the graph Laplacian and `D` is the diagonal matrix of
    node degrees.

    Parameters
    ----------
    G : graph
       A NetworkX graph

    nodelist : list, optional
       The rows and columns are ordered according to the nodes in nodelist.
       If nodelist is None, then the ordering is produced by G.nodes().

    weight : string or None, optional (default='weight')
       The edge data key used to compute each value in the matrix.
       If None, then each edge has weight 1.

    Returns
    -------
    N : NumPy matrix
      The normalized Laplacian matrix of G.

    Notes
    -----
    For MultiGraph/MultiDiGraph, the edges weights are summed.
    See to_numpy_matrix for other options.

    If the Graph contains selfloops, D is defined as diag(sum(A,1)), where A is
    the adjacency matrix [2]_.

    See Also
    --------
    laplacian_matrix

    References
    ----------
    .. [1] Fan Chung-Graham, Spectral Graph Theory,
       CBMS Regional Conference Series in Mathematics, Number 92, 1997.
    .. [2] Steve Butler, Interlacing For Weighted Graphs Using The Normalized
       Laplacian, Electronic Journal of Linear Algebra, Volume 16, pp. 90-98,
       March 2007.
    r   Nr   )r	   r   r
   r   )r   )r
   ignore)Zdivideg      ?)r   r   r   r   r   r   r   r   r   r   ZerrstatesqrtZisinfdot)r   r	   r   r   r   r   r   r   r   LZ
diags_sqrtZDHr   r   r   r   A   s    3

Z
undirectedZ
multigraphffffff?c             C   s  ddl }ddlm}m}m} |dkrHt| rDt| r>d}qHd}nd}tj| ||t	d}	|	j
\}
}|dkr|d	||	jd
dj dg|
|
}|dkr||	 }n||
}|||	  d }n|dkrJd|  k rd
k sn td|	 }	||	jd
ddk}x|d D ]}d	|
 |	|< qW |	|	jd
d }	||	 d
| |
  }n
td|j|jd
d\}}| j}||  }||}||dg|
|
| |d	| dg|
|
 }|t| }|||j d  S )a  Return the directed Laplacian matrix of G.

    The graph directed Laplacian is the matrix

    .. math::

        L = I - (\Phi^{1/2} P \Phi^{-1/2} + \Phi^{-1/2} P^T \Phi^{1/2} ) / 2

    where `I` is the identity matrix, `P` is the transition matrix of the
    graph, and `\Phi` a matrix with the Perron vector of `P` in the diagonal and
    zeros elsewhere.

    Depending on the value of walk_type, `P` can be the transition matrix
    induced by a random walk, a lazy random walk, or a random walk with
    teleportation (PageRank).

    Parameters
    ----------
    G : DiGraph
       A NetworkX graph

    nodelist : list, optional
       The rows and columns are ordered according to the nodes in nodelist.
       If nodelist is None, then the ordering is produced by G.nodes().

    weight : string or None, optional (default='weight')
       The edge data key used to compute each value in the matrix.
       If None, then each edge has weight 1.

    walk_type : string or None, optional (default=None)
       If None, `P` is selected depending on the properties of the
       graph. Otherwise is one of 'random', 'lazy', or 'pagerank'

    alpha : real
       (1 - alpha) is the teleportation probability used with pagerank

    Returns
    -------
    L : NumPy array
      Normalized Laplacian of G.

    Raises
    ------
    NetworkXError
        If NumPy cannot be imported

    NetworkXNotImplemnted
        If G is not a DiGraph

    Notes
    -----
    Only implemented for DiGraphs

    See Also
    --------
    laplacian_matrix

    References
    ----------
    .. [1] Fan Chung (2005).
       Laplacians and the Cheeger inequality for directed graphs.
       Annals of Combinatorics, 9(1), 2005
    r   N)identityr   linalgrandomlazyZpagerank)r	   r   Zdtype)r&   r'   g      ?r   )r   g       @zalpha must be between 0 and 1z+walk_type must be random, lazy, or pagerank)k)r   r   r$   r   r%   r   Zis_strongly_connectedZis_aperiodicr   floatr   Zarrayr   ZflatZNetworkXErrorZtodensewhereZeigsTr   realr    len)r   r	   r   Z	walk_typeZalphaZspr$   r   r%   Mr   r   ZDIPIZdanglingdZevalsZevecsvpZsqrtpQr   r   r   r      sD    C



$





(c             C   s2   ddl m} ydd l}W n   |dY nX d S )Nr   )SkipTestzNumPy not available)Znoser5   numpy)moduler5   r6   r   r   r   setup_module   s
    r8   )Nr   )Nr   )Nr   Nr#   )__doc__Znetworkxr   Znetworkx.utilsr   join
__author____all__r   r   r   r8   r   r   r   r   <module>   s$   ,G q