-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
115 lines (102 loc) · 2.66 KB
/
utils.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import numpy as np
import math
class Operators:
def L(self, w):
'''Usage: print(L([1,0,1]))
'''
k = len(w)
n = int(0.5*(1+ math.sqrt(1+8*k)))
Lw = np.zeros((n,n))
k=0
for i in range(0, n):
for j in range(i+1,n):
Lw[i][j] = -w[k]
k = k + 1
Lw = Lw + Lw.T
row,col = np.diag_indices_from(Lw)
Lw[row,col] = -Lw.sum(axis=1)
return Lw
def vecLmat(self, n):
ncols = int(0.5*n*(n-1))
nrows = int(n*n)
R = np.zeros((nrows, ncols))
e = np.zeros(ncols)
e[0] = 1
R[:, 0] = self.L(e).flatten()
for j in range(1,ncols):
e[j-1] = 0
e[j] = 1
R[:, j] = self.L(e).flatten()
return R
def Linv(self, M):
'''Computes the inverse of the L operator
Parameters
----------
M : Laplacian matrix
Returns
-------
w : the weight vector of the graph
Usage
-------
a = [[1, -1, 0], [-1, 2, -1], [0, -1, 1]]
Linv(np.array(a))
'''
N = M.shape[0]
k = int( 0.5*N*(N-1))
l = 0
w = np.zeros(k)
for i in range(0,N):
for j in range(i+1, N):
w[l] = -M[i][j]
l = l+1
return w
def A(self, w):
'''Computes the Adjacency linear operator which maps a vector of weights into a valid Adjacency matrix.
Parameters
----------
w : weight vector of the graph
Returns
-------
Aw : the Adjacency matrix
Usage
-------
A(np.array([1,0,1]))
'''
k = w.shape[0]
n = int(0.5 * (1 + math.sqrt(1 + 8 * k)))
Aw = np.zeros((n,n))
k=0
for i in range(0, n):
for j in range(i+1,n):
Aw[i][j] = w[k]
k = k + 1
Aw = Aw + Aw.T
return Aw
def Lstar(self, M):
N = M.shape[1]
k = int( 0.5*N*(N-1))
w = np.zeros(k)
j=0
l=1
for i in range(0,k):
w[i] = M[j][j] + M[l][l] -(M[l][j] + M[j][l])
if l==(N-1):
j = j+1
l = j+1
else:
l = l+1
return w
def Astar(self, M):
N = M.shape[1]
k = int( 0.5*N*(N-1))
w = np.zeros(k)
j=0
l=1
for i in range(0,k):
w[i] = M[l][j] + M[j][l]
if l==(N-1):
j = j+1
l = j+1
else:
l = l+1
return w