~ubuntu-branches/ubuntu/utopic/python-networkx/utopic

« back to all changes in this revision

Viewing changes to networkx/linalg/tests/test_laplaican.py

  • Committer: Package Import Robot
  • Author(s): Sandro Tosi
  • Date: 2012-08-11 12:41:30 UTC
  • mfrom: (1.4.1) (5.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20120811124130-whr6uso7fncyg8bi
Tags: 1.7-1
* New upstream release
* debian/patches/changeset_*
  - removed, included upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from nose import SkipTest
 
2
 
 
3
import networkx as nx
 
4
from networkx.generators.degree_seq import havel_hakimi_graph
 
5
 
 
6
class TestLaplacian(object):
 
7
    numpy=1 # nosetests attribute, use nosetests -a 'not numpy' to skip test
 
8
    @classmethod
 
9
    def setupClass(cls):
 
10
        global numpy
 
11
        global assert_equal
 
12
        global assert_almost_equal
 
13
        try:
 
14
            import numpy
 
15
            from numpy.testing import assert_equal,assert_almost_equal
 
16
        except ImportError:
 
17
             raise SkipTest('NumPy not available.')
 
18
 
 
19
    def setUp(self):
 
20
        deg=[3,2,2,1,0]
 
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() )
 
24
        self.WG.add_node(4)
 
25
        self.MG=nx.MultiGraph(self.G)
 
26
 
 
27
 
 
28
    def test_laplacian(self):
 
29
        "Graph Laplacian"
 
30
        NL=numpy.array([[ 3, -1, -1, -1, 0],
 
31
                        [-1,  2, -1,  0, 0],
 
32
                        [-1, -1,  2,  0, 0],
 
33
                        [-1,  0,  0,  1, 0],
 
34
                        [ 0,  0,  0,  0, 0]])
 
35
        WL=0.5*NL
 
36
        OL=0.3*NL
 
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)
 
44
 
 
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)
 
53
                       
 
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)
 
65