ó
Ê½÷Xc           @` s{   d  Z  d d l m Z m Z m Z d d l Z d d l m Z e	 e	 e	 d „ Z
 d „  Z e	 d d „ Z e	 d d „ Z d S(	   s(   
Laplacian of a compressed-sparse graph
i    (   t   divisiont   print_functiont   absolute_importN(   t
   isspmatrixc      	   C` sÙ   |  j  d k s) |  j d |  j d k r8 t d ƒ ‚ n  | r} t j |  j t ƒ sk t j |  j t j ƒ r} |  j t	 ƒ }  n  t
 |  ƒ r t n t } | r¡ d n d } | |  d | d | ƒ\ } } | rÕ | | f S| S(   s#  
    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 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]])
    i   i    i   s(   csgraph must be a square matrix or arrayt   normedt   axis(   t   ndimt   shapet
   ValueErrort   npt
   issubdtypet   dtypet   intt   uintt   astypet   floatR   t   _laplacian_sparset   _laplacian_dense(   t   csgraphR   t   return_diagt   use_out_degreet
   create_lapt   degree_axist   lapt   d(    (    s>   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csgraph/_laplacian.pyt	   laplacian   s    3)
c         C` s$   | |  j  d  d  t | ƒ d … <d  S(   Ni   (   t   flatt   len(   t   AR   (    (    s>   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csgraph/_laplacian.pyt   _setdiag_denseT   s    c         C` s9  |  j  d	 k r$ |  j ƒ  } t } n |  } t } | j d | ƒ j ƒ  | j ƒ  } | rã | j d | ƒ } | d k } t j | d t j	 | ƒ ƒ } | j
 | | j _
 | j
 | | j _
 | j
 d 9_
 | j d | ƒ nL | j  d k r| j ƒ  } n | j d | ƒ } | j
 d 9_
 | j | ƒ | | f S(
   Nt   lilt   dokR   t   copyi    i   iÿÿÿÿt   dia(   R   R   (   t   formatt   tocoot   Falset   Truet   sumt   getA1t   diagonalR	   t   wheret   sqrtt   datat   rowt   colt   setdiagR    (   t   graphR   R   t   mt
   needs_copyt   wt   isolated_node_mask(    (    s>   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csgraph/_laplacian.pyR   X   s(    	"c         C` sÇ   t  j |  ƒ } t  j | d ƒ | j d | ƒ } | r¦ | d k } t  j | d t  j | ƒ ƒ } | | } | | d  d  … t  j f } | d 9} t | d | ƒ n | d 9} t | | ƒ | | f S(   Ni    R   i   iÿÿÿÿ(   R	   t   arrayt   fill_diagonalR&   R)   R*   t   newaxisR   (   R/   R   R   R0   R2   R3   (    (    s>   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csgraph/_laplacian.pyR   r   s    


(   t   __doc__t
   __future__R    R   R   t   numpyR	   t   scipy.sparseR   R$   R   R   R   R   (    (    (    s>   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csgraph/_laplacian.pyt   <module>   s   B	