-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHyperEigen.py
91 lines (80 loc) · 3.02 KB
/
HyperEigen.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
import scipy.sparse as ss
import scipy.sparse.linalg as ssalg
from scipy.sparse import csr_matrix
import numpy as np
#Function to return the hypergraph degree matrix from its incidence matrix - sparse implementation
def get_degree_matrix_py(inc_mat):
'''
Parameters
----------
inc_mat : csr_matrix
Incidence matrix of hypergraph
Returns
-------
deg_mat : csr_matrix
Degree matrix of hypergraph
'''
if ss.isspmatrix(inc_mat) == False:
inc_mat = csr_matrix(inc_mat)
sqr_mat = inc_mat.power(2)
sqr_sum = ss.csr_matrix.sum(sqr_mat, 1)
deg_mat = ss.csr_matrix.multiply(ss.identity(sqr_sum.shape[0]), sqr_sum)
deg_mat = csr_matrix(deg_mat)
return deg_mat
#Function to return the hypergraph hyperedge normalise laplacian matrix from its incidence matrix - sparse implementation
def get_hyperedge_normalised_laplacian_py(inc_mat, deg_mat = None):
'''
Parameters
----------
inc_mat : csr_matrix
Incidence matrix of hypergraph
deg_mat : csr_matrix, optional
Degree matrix of hypergraph. If None then it is calculated from the incidence matrix. The default is None.
Returns
-------
lap_mat : csr_matrix
The hyperedge normalised laplacian matrix of the hypergraph
'''
if not ss.isspmatrix(inc_mat):
inc_mat = csr_matrix(inc_mat)
if deg_mat is None:
deg_mat = get_degree_matrix_py(inc_mat)
elif not ss.isspmatrix_csr(deg_mat):
deg_mat = csr_matrix(deg_mat)
else:
deg_mat = deg_mat
for i in range(deg_mat.shape[0]):
if deg_mat[i,i] != 0:
deg_mat[i,i] = deg_mat[i,i]**-1
inc_T = inc_mat.T
part_lap = inc_T.dot(deg_mat)
lap_mat = part_lap.dot(inc_mat)
return lap_mat
#Function to return the largest eigenvalue of the hyperedge normalise laplacian matrix from its incidence matrix - sparse implementation
def get_largest_eigenvalue_py(inc_mat, lap_mat = None, deg_mat = None):
'''
Parameters
----------
inc_mat : csr_matrix
Incidence matrix of hypergraph
lap_mat : csr_matrix optional
The hyperedge normalised laplacian of the hypergraph. If None then it is calculated from incidence matrix. The default is None.
deg_mat : csr_matrix, optional
The degree matrix of the hypergraph. If needed, is used to speed up finding the hyperedge normalised laplacian matrix. The default is None.
Returns
-------
eigen_val : complex
The largest eigenvalue of the hyperedge normalised laplacian matrix.
'''
if not ss.isspmatrix(inc_mat):
inc_mat = csr_matrix(inc_mat)
if lap_mat is None:
lap_mat = get_hyperedge_normalised_laplacian_py(inc_mat, deg_mat)
elif ss.isspmatrix_csr(lap_mat):
lap_mat = csr_matrix(lap_mat)
lap_mat = get_hyperedge_normalised_laplacian_py(inc_mat)
eigen_val = ssalg.eigs(lap_mat, k = 1)
eigen_val = eigen_val[0] / inc_mat.shape[0]
return eigen_val
count_mat = r.CountsMatrix
large_val = get_largest_eigenvalue_py(inc_mat = count_mat)