-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdegree_power.py
34 lines (25 loc) · 909 Bytes
/
degree_power.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 warnings
from typing import Any
import numpy as np
from scipy import sparse as sp
def calculate_degree_power(A, k) -> Any:
"""
Calculates the value of `A` in power of `k` from the given adjacency matrix.
It can be used to calculate the normalized Laplacian.
Args:
A: rank 2 array or sparse matrix
k: exponent to which the degree matrix is raised
Returns:
If the input parameter `A` is a dense array, it will return a dense array;
If `A` is sparse, it will return a sparse array in DIA format.
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
degrees = np.power(np.array(A.sum(1)), k).ravel()
degrees[np.isinf(degrees)] = 0.0
issparse_predicate = sp.issparse(A)
if issparse_predicate:
diags = sp.diags(degrees)
else:
diags = np.diag(degrees)
return diags