ó
|£*^c           @   s®   d  Z  d d l m Z d d l Z d d l m Z d j d d d d	 g ƒ Z d
 d g Z	 e d ƒ e d ƒ d d „ ƒ ƒ Z e d ƒ e d ƒ d d „ ƒ ƒ Z d „  Z d S(   s   Modularity matrix of graphs.
iÿÿÿÿ(   t   divisionN(   t   not_implemented_fors   
s%   Aric Hagberg <aric.hagberg@gmail.com>s   Pieter Swart (swart@lanl.gov)s    Dan Schult (dschult@colgate.edu)s1   Jean-Gabriel Young (Jean.gabriel.young@gmail.com)t   modularity_matrixt   directed_modularity_matrixt   directedt
   multigraphc         C   st   | d k r |  j ƒ  } n  t j |  d | d d ƒ} | j d d ƒ } |  j ƒ  } | | j ƒ  d | } | | S(   s°  Return the modularity matrix of G.

    The modularity matrix is the matrix B = A - <A>, where A is the adjacency
    matrix and <A> is the average adjacency matrix, assuming that the graph
    is described by the configuration model.

    More specifically, the element B_ij of B is defined as
        A_ij - k_i k_j/m
    where k_i(in) is the degree of node i, and were m is the number of edges
    in the graph.

    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().

    Returns
    -------
    B : Numpy matrix
      The modularity matrix of G.

    Examples
    --------
    >>> import networkx as nx
    >>> k =[3, 2, 2, 1, 0]
    >>> G = nx.havel_hakimi_graph(k)
    >>> B = nx.modularity_matrix(G)


    See Also
    --------
    to_numpy_matrix
    adjacency_matrix
    laplacian_matrix
    directed_modularity_matrix

    References
    ----------
    .. [1] M. E. J. Newman, "Modularity and community structure in networks",
       Proc. Natl. Acad. Sci. USA, vol. 103, pp. 8577-8582, 2006.
    t   nodelistt   formatt   csrt   axisi   i   N(   t   Nonet   nodest   nxt   to_scipy_sparse_matrixt   sumt   number_of_edgest	   transpose(   t   GR   t   At   kt   mt   X(    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/linalg/modularitymatrix.pyR      s    0t
   undirectedc         C   s|   | d k r |  j ƒ  } n  t j |  d | d d ƒ} | j d d ƒ } | j d d ƒ } |  j ƒ  } | | | } | | S(   sB  Return the directed modularity matrix of G.

    The modularity matrix is the matrix B = A - <A>, where A is the adjacency
    matrix and <A> is the expected adjacency matrix, assuming that the graph
    is described by the configuration model.

    More specifically, the element B_ij of B is defined as
        B_ij = A_ij - k_i(out) k_j(in)/m
    where k_i(in) is the in degree of node i, and k_j(out) is the out degree
    of node j, with m the number of edges in the graph.

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

    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().

    Returns
    -------
    B : Numpy matrix
      The modularity matrix of G.

    Examples
    --------
    >>> import networkx as nx
    >>> G = nx.DiGraph()
    >>> G.add_edges_from(((1,2), (1,3), (3,1), (3,2), (3,5), (4,5), (4,6),
    ...                   (5,4), (5,6), (6,4)))
    >>> B = nx.directed_modularity_matrix(G)


    Notes
    -----
    NetworkX defines the element A_ij of the adjacency matrix as 1 if there
    is a link going from node i to node j. Leicht and Newman use the opposite
    definition. This explains the different expression for B_ij.

    See Also
    --------
    to_numpy_matrix
    adjacency_matrix
    laplacian_matrix
    modularity_matrix

    References
    ----------
    .. [1] E. A. Leicht, M. E. J. Newman, 
       "Community structure in directed networks",
        Phys. Rev Lett., vol. 100, no. 11, p. 118703, 2008.
    R   R   R   R	   i    i   N(   R
   R   R   R   R   R   (   R   R   R   t   k_int   k_outR   R   (    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/linalg/modularitymatrix.pyR   M   s    8c         C   sF   d d l  m } y d d  l } d d  l } Wn | d ƒ ‚ n Xd  S(   Niÿÿÿÿ(   t   SkipTests   NumPy not available(   t   noseR   t   numpyt   scipy(   t   moduleR   R   R   (    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/linalg/modularitymatrix.pyt   setup_module‘   s    (   t   __doc__t
   __future__R    t   networkxR   t   networkx.utilsR   t   joint
   __author__t   __all__R
   R   R   R   (    (    (    st   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/linalg/modularitymatrix.pyt   <module>   s   			8		B