~ubuntu-branches/ubuntu/raring/python-networkx/raring

« back to all changes in this revision

Viewing changes to networkx/linalg/spectrum.py

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2009-11-23 15:44:34 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20091123154434-ellm2ut3a4edf9wh
Tags: 1.0~rc1~svn1492-1
* New upstream snapshot, past 1.0~rc1, as requested by Yaroslav
  Halchenko (Closes: #549996).
* Refresh patch accordingly:
   + debian/patches/10_doc_relocation.
* Get rid of extra LICENSE.txt file in /usr/share/doc.
* Use dh_compress -Xexamples/ to avoid compressing examples, thanks to
  Sandro Tosi (Closes: #539942).
* Bump Standards-Version from 3.8.0 to 3.8.3 (no changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
#    Aric Hagberg <hagberg@lanl.gov>
10
10
#    Dan Schult <dschult@colgate.edu>
11
11
#    Pieter Swart <swart@lanl.gov>
12
 
#    Distributed under the terms of the GNU Lesser General Public License
13
 
#    http://www.gnu.org/copyleft/lesser.html
14
 
 
15
 
try:
16
 
    import numpy as N
17
 
except:
18
 
    raise ImportError, 'numpy not found'
 
12
#    All rights reserved.
 
13
#    BSD license.
 
14
 
 
15
 
 
16
import networkx
19
17
 
20
18
__all__ = ['adj_matrix', 'laplacian', 'generalized_laplacian',
21
19
           'laplacian_spectrum', 'adjacency_spectrum','normalized_laplacian']
22
20
 
23
 
import networkx
 
21
 
24
22
 
25
23
def adj_matrix(G,nodelist=None):
26
24
    """Return adjacency matrix of graph as a numpy matrix.
46
44
 
47
45
    """
48
46
    # this isn't the most efficient way to do this...
 
47
    try:
 
48
        import numpy as np
 
49
    except ImportError:
 
50
        raise ImportError, \
 
51
          "laplacian() requires numpy: http://scipy.org/ "
 
52
 
49
53
    n=G.order()
50
 
    I=N.identity(n)
51
 
    A=N.asarray(networkx.to_numpy_matrix(G,nodelist=nodelist))
52
 
    D=I*N.sum(A,axis=1)
 
54
    I=np.identity(n)
 
55
    A=np.asarray(networkx.to_numpy_matrix(G,nodelist=nodelist))
 
56
    D=I*np.sum(A,axis=1)
53
57
    L=D-A
54
58
    return L
55
59
 
63
67
 
64
68
    """
65
69
    # FIXME: this isn't the most efficient way to do this...
 
70
    try:
 
71
        import numpy as np
 
72
    except ImportError:
 
73
        raise ImportError, \
 
74
          "normalized_laplacian() requires numpy: http://scipy.org/ "
66
75
    n=G.order()
67
 
    I=N.identity(n)
68
 
    A=N.asarray(networkx.to_numpy_matrix(G,nodelist=nodelist))
69
 
    d=N.sum(A,axis=1)
 
76
    I=np.identity(n)
 
77
    A=np.asarray(networkx.to_numpy_matrix(G,nodelist=nodelist))
 
78
    d=np.sum(A,axis=1)
70
79
    L=I*d-A
71
 
    osd=N.zeros(len(d))
 
80
    osd=np.zeros(len(d))
72
81
    for i in range(len(d)):
73
 
        if d[i]>0: osd[i]=N.sqrt(1.0/d[i])
 
82
        if d[i]>0: osd[i]=np.sqrt(1.0/d[i])
74
83
    T=I*osd
75
 
    L=N.dot(T,N.dot(L,T))
 
84
    L=np.dot(T,np.dot(L,T))
76
85
    return L
77
86
 
78
87
def laplacian_spectrum(G):
79
88
    """Return eigenvalues of the Laplacian of G""" 
80
 
    return N.linalg.eigvals(laplacian(G))
 
89
    try:
 
90
        import numpy as np
 
91
    except ImportError:
 
92
        raise ImportError, \
 
93
          "laplacian_spectrum() requires numpy: http://scipy.org/ "
 
94
    return np.linalg.eigvals(laplacian(G))
81
95
 
82
96
def adjacency_spectrum(G):
83
97
    """Return eigenvalues of the adjacency matrix of G""" 
84
 
    return N.linalg.eigvals(adj_matrix(G))
 
98
    try:
 
99
        import numpy as np
 
100
    except ImportError:
 
101
        raise ImportError, \
 
102
          "adjacency_spectrum() requires numpy: http://scipy.org/ "
 
103
    return np.linalg.eigvals(adj_matrix(G))
85
104
 
86
105
 
87
106
combinatorial_laplacian=laplacian