1""" 2Eigenvalue spectrum of graphs. 3""" 4# Copyright (C) 2004-2011 by 5# Aric Hagberg <hagberg@lanl.gov> 6# Dan Schult <dschult@colgate.edu> 7# Pieter Swart <swart@lanl.gov> 8# All rights reserved. 9# BSD license. 10import networkx as nx 11__author__ = "\n".join(['Aric Hagberg (hagberg@lanl.gov)', 12 'Pieter Swart (swart@lanl.gov)', 13 'Dan Schult(dschult@colgate.edu)']) 14 15__all__ = ['laplacian_spectrum', 'adjacency_spectrum'] 16 17 18def laplacian_spectrum(G, weight='weight'): 19 """Return eigenvalues of the Laplacian of G 20 21 Parameters 22 ---------- 23 G : graph 24 A NetworkX graph 25 26 weight : string or None, optional (default='weight') 27 The edge data key used to compute each value in the matrix. 28 If None, then each edge has weight 1. 29 30 Returns 31 ------- 32 evals : NumPy array 33 Eigenvalues 34 35 Notes 36 ----- 37 For MultiGraph/MultiDiGraph, the edges weights are summed. 38 See to_numpy_matrix for other options. 39 40 See Also 41 -------- 42 laplacian_matrix 43 """ 44 try: 45 import numpy as np 46 except ImportError: 47 raise ImportError( 48 "laplacian_spectrum() requires NumPy: http://scipy.org/ ") 49 return np.linalg.eigvals(nx.laplacian_matrix(G,weight=weight)) 50 51def adjacency_spectrum(G, weight='weight'): 52 """Return eigenvalues of the adjacency matrix of G. 53 54 Parameters 55 ---------- 56 G : graph 57 A NetworkX graph 58 59 weight : string or None, optional (default='weight') 60 The edge data key used to compute each value in the matrix. 61 If None, then each edge has weight 1. 62 63 Returns 64 ------- 65 evals : NumPy array 66 Eigenvalues 67 68 Notes 69 ----- 70 For MultiGraph/MultiDiGraph, the edges weights are summed. 71 See to_numpy_matrix for other options. 72 73 See Also 74 -------- 75 adjacency_matrix 76 """ 77 try: 78 import numpy as np 79 except ImportError: 80 raise ImportError( 81 "adjacency_spectrum() requires NumPy: http://scipy.org/ ") 82 return np.linalg.eigvals(nx.adjacency_matrix(G,weight=weight)) 83 84# fixture for nose tests 85def setup_module(module): 86 from nose import SkipTest 87 try: 88 import numpy 89 except: 90 raise SkipTest("NumPy not available") 91