-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_generate_k_bandlimited_signal.py
76 lines (54 loc) · 2.17 KB
/
test_generate_k_bandlimited_signal.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 4 11:06:07 2018
@author: Quentin
"""
import numpy as np
import scipy as sp
import networkx as nx
from graphSamplingWithDPP import generate_graph_from_stochastic_block_model,\
generate_k_bandlimited_signal
"""
The goal of this test file is to demonstrate the computation difference between
different eigenvalue and eigenvector computation algorithms from Numpy and
Scipy.
"""
##### PARAMETERS #####
### Signal band
k = 3
### Graph creation parameters
N = 100 # Number of nodes
kgraph = 3 # Number of communities
c = 16 # Average degree
epsilonc = (c - np.sqrt(c)) / (c + np.sqrt(c) * (kgraph - 1)) # Critical epsilon
# above which one can no longer distinguish communities
epsilon = 0.5 * epsilonc # q2/q1
##### END PARAMETERS #####
# Generate graph
G = generate_graph_from_stochastic_block_model(N, kgraph, epsilon, c)
# Get laplacian and adjacency matrix
L = sp.sparse.csr_matrix(nx.laplacian_matrix(G), dtype='d')
W = sp.sparse.csr_matrix(nx.adjacency_matrix(G), dtype='d')
# Generate a k-bandlimited signal
x, alpha, Lambda_k, U_k = generate_k_bandlimited_signal(L, k)
# Using shift-inverse method
Lambda_k_ARPACK_shift_inverse, U_k_ARPACK_shift_inverse = sp.sparse.linalg.\
eigsh(L, k=k, sigma=0, which='LM')
# Directly using SM from ARPACK (not efficient)
Lambda_k_ARPACK_SM, U_k_ARPACK_SM = sp.sparse.linalg.eigsh(L, k=k, which='SM')
# Reference: numpy function
Lambda_k_numpy_eigh, U_k_numpy_eigh = np.linalg.eigh(L.toarray())
idx = Lambda_k_numpy_eigh.argsort()
Lambda_k_numpy_eigh = Lambda_k_numpy_eigh[idx[range(k)]]
U_k_numpy_eigh = U_k_numpy_eigh[:,idx[range(k)]]
print('----- Reference (Numpy) (not efficient):')
print('Lambda_k=', Lambda_k_numpy_eigh)
print('----- ARPACK shift-inverse:')
print('Lambda_k=', Lambda_k_ARPACK_shift_inverse)
print(np.dot(U_k_numpy_eigh.T, U_k_ARPACK_shift_inverse))
print('----- ARPACK SM (not efficient):')
print('Lambda_k=', Lambda_k_ARPACK_SM)
print(np.dot(U_k_numpy_eigh.T, U_k_ARPACK_SM))
print('----- Function chosen in generate_k_bandlimited_signal:')
print('Lambda_k=', Lambda_k)
print(np.dot(U_k_numpy_eigh.T, U_k))