forked from aweinstein/dw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_T_grid.py
35 lines (26 loc) · 869 Bytes
/
make_T_grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import networkx as nx
import numpy as np
from scipy.io import savemat
from diffusion import get_T
def make_T(N, add_selfloops=False):
'''Compute the diffusion operator of an N-by-N grid graph.
The difussion operator is defined as
T = D^0.5 * P * D^-0.5, where D is the degree matrix, P=D^-1W is the random
walk matrix, and W is the adjancency matrix.
'''
G = nx.grid_2d_graph(N,N)
if add_selfloops:
for n in G:
G.add_edge(n, n)
T, _ = get_T(G)
L = nx.laplacian(G, nodelist=sorted(G.nodes()))
return T, L
for N in [4, 8, 16]:
T, L = make_T(N)
fn = 'T_grid_%d' % N
savemat(fn, {'T':T, 'L':L}, oned_as='column')
print 'Saved as %s' % fn
T, L = make_T(N, True)
fn = 'T_grid_sl_%d' % N
savemat(fn, {'T':T, 'L':L}, oned_as='column')
print 'Saved as %s' % fn