1
from nose import SkipTest
4
from networkx.generators.degree_seq import havel_hakimi_graph
6
class TestLaplacian(object):
7
numpy=1 # nosetests attribute, use nosetests -a 'not numpy' to skip test
12
global assert_almost_equal
15
from numpy.testing import assert_equal,assert_almost_equal
17
raise SkipTest('NumPy not available.')
21
self.G=havel_hakimi_graph(deg)
22
self.WG=nx.Graph( (u,v,{'weight':0.5,'other':0.3})
23
for (u,v) in self.G.edges_iter() )
25
self.MG=nx.MultiGraph(self.G)
28
def test_laplacian(self):
30
NL=numpy.array([[ 3, -1, -1, -1, 0],
37
assert_equal(nx.laplacian(self.G),NL)
38
assert_equal(nx.laplacian(self.MG),NL)
39
assert_equal(nx.laplacian(self.G,nodelist=[0,1]),
40
numpy.array([[ 1, -1],[-1, 1]]))
41
assert_equal(nx.laplacian(self.WG),WL)
42
assert_equal(nx.laplacian(self.WG,weight=None),NL)
43
assert_equal(nx.laplacian(self.WG,weight='other'),OL)
45
def test_generalized_laplacian(self):
46
"Generalized Graph Laplacian"
47
GL=numpy.array([[ 1.00, -0.408, -0.408, -0.577, 0.00],
48
[-0.408, 1.00, -0.50, 0.00 , 0.00],
49
[-0.408, -0.50, 1.00, 0.00, 0.00],
50
[-0.577, 0.00, 0.00, 1.00, 0.00],
51
[ 0.00, 0.00, 0.00, 0.00, 0.00]])
52
assert_almost_equal(nx.generalized_laplacian(self.G),GL,decimal=3)
54
def test_normalized_laplacian(self):
55
"Generalized Graph Laplacian"
56
GL=numpy.array([[ 1.00, -0.408, -0.408, -0.577, 0.00],
57
[-0.408, 1.00, -0.50, 0.00 , 0.00],
58
[-0.408, -0.50, 1.00, 0.00, 0.00],
59
[-0.577, 0.00, 0.00, 1.00, 0.00],
60
[ 0.00, 0.00, 0.00, 0.00, 0.00]])
61
assert_almost_equal(nx.normalized_laplacian(self.G),GL,decimal=3)
62
assert_almost_equal(nx.normalized_laplacian(self.MG),GL,decimal=3)
63
assert_almost_equal(nx.normalized_laplacian(self.WG),GL,decimal=3)
64
assert_almost_equal(nx.normalized_laplacian(self.WG,weight='other'),GL,decimal=3)