ó
 ,ľ[c           @   sź   d  Z  d d l Z d d l m Z d j 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 d d    Z d   Z d S(   s   Laplacian matrix of graphs.
i˙˙˙˙N(   t   not_implemented_fors   
s%   Aric Hagberg <aric.hagberg@gmail.com>s   Pieter Swart (swart@lanl.gov)s    Dan Schult (dschult@colgate.edu)s3   Alejandro Weinstein <alejandro.weinstein@gmail.com>t   laplacian_matrixt   normalized_laplacian_matrixt   directed_laplacian_matrixt   directedt   weightc   	      C   s   d d l  } | d k r' t |   } n  t j |  d | d | d d } | j \ } } | j d d  } | j j | j	   d	 g | | d d } | | S(
   sE  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
    i˙˙˙˙Nt   nodelistR   t   formatt   csrt   axisi   i    (
   t   scipy.sparset   Nonet   listt   nxt   to_scipy_sparse_matrixt   shapet   sumt   sparset   spdiagst   flatten(	   t   GR   R   t   scipyt   At   nt   mt   diagst   D(    (    s>   lib/python2.7/site-packages/networkx/linalg/laplacianmatrix.pyR      s    "	*c         C   s"  d d l  } d d l } | d k r3 t |   } n  t j |  d | d | d d } | j \ } } | j d d  j   } | j	 j
 | d	 g | | d d } | | }	 | j d
 d   d | j |  }
 Wd QXd	 |
 | j |
  <| j	 j
 |
 d	 g | | d d } | j |	 j |   S(   sR  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.
    i˙˙˙˙NR   R   R   R   R	   i   i    t   dividet   ignoreg      đ?(   R   R
   R   R   R   R   R   R   R   R   R   t   errstatet   sqrtt   isinft   dot(   R   R   R   R   R   R   R   R   R   t   Lt
   diags_sqrtt   DH(    (    s>   lib/python2.7/site-packages/networkx/linalg/laplacianmatrix.pyR   A   s    3	$
$t
   undirectedt
   multigraphgffffffî?c         C   s  d d l  } d d l m } m } m } | d k rm t j |   rd t j |   r[ d } qj d } qm d } n  t j	 |  d | d | d	 t
 }	 |	 j \ }
 } | d k r| d
 | j |	 j d d  j  d g |
 |
  } | d k rö | |	 } qß| |
  } | | |	 d } nČ | d k rĐd | k  o:d k  n sQt j d   n  |	 j   }	 | j |	 j d d  d k  } x  | d D] } d
 |
 |	 | <qW|	 |	 j d d  }	 | |	 d | |
 } n t j d   | j | j d d \ } } | j   j } | | j   } | j |  } | | d g |
 |
  | | d
 | d g |
 |
  } | j t |    } | | | j d S(   s˝  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
    i˙˙˙˙N(   t   identityR   t   linalgt   randomt   lazyt   pagerankR   R   t   dtypeg      đ?R	   i   i    g       @s   alpha must be between 0 and 1s+   walk_type must be random, lazy, or pagerankt   k(   R(   R)   (   R   R
   R&   R   R'   R   R   t   is_strongly_connectedt   is_aperiodicR   t   floatR   t   arrayR   t   flatt   NetworkXErrort   todenset   wheret   eigst   TR   t   realR   t   len(   R   R   R   t	   walk_typet   alphat   spR&   R   R'   t   MR   R   t   DIt   Pt   It   danglingt   dt   evalst   evecst   vt   pt   sqrtpt   Q(    (    s>   lib/python2.7/site-packages/networkx/linalg/laplacianmatrix.pyR      sD    C				4!6c         C   s:   d d l  m } y d d  l } Wn | d   n Xd  S(   Ni˙˙˙˙(   t   SkipTests   NumPy not available(   t   noseRH   t   numpy(   t   moduleRH   RJ   (    (    s>   lib/python2.7/site-packages/networkx/linalg/laplacianmatrix.pyt   setup_moduleý   s
    (   t   __doc__t   networkxR   t   networkx.utilsR    t   joint
   __author__t   __all__R   R   R   R   RL   (    (    (    s>   lib/python2.7/site-packages/networkx/linalg/laplacianmatrix.pyt   <module>   s$   			,	G		q