-
Notifications
You must be signed in to change notification settings - Fork 0
/
efficiency.cpp
91 lines (72 loc) · 1.92 KB
/
efficiency.cpp
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
#include <gsl/gsl_math.h>
#include "bct.h"
MATRIX_T* distance_inv(const MATRIX_T*, const MATRIX_T*);
/*
* Computes global efficiency. Takes an optional distance matrix that is
* computed if not given.
*/
FP_T BCT_NAMESPACE::efficiency_global(const MATRIX_T* G, const MATRIX_T* D) {
if (safe_mode) check_status(G, SQUARE, "efficiency_global");
// N=length(G);
int N = length(G);
// e=distance_inv(G);
MATRIX_T* e = distance_inv(G, D);
// E=sum(e(:))./(N^2-N);
VECTOR_T* e_v = to_vector(e);
MATRIX_ID(free)(e);
FP_T sum_e = sum(e_v);
VECTOR_ID(free)(e_v);
return sum_e / (FP_T)(N * (N - 1));
}
/*
* Computes local efficiency.
*/
VECTOR_T* BCT_NAMESPACE::efficiency_local(const MATRIX_T* G) {
if (safe_mode) check_status(G, SQUARE, "efficiency_local");
// N=length(G);
int N = length(G);
// E=zeros(N,1);
VECTOR_T* E = zeros_vector(N);
// for u=1:N
#ifdef _OPENMP
#pragma omp parallel for shared(E)
#endif
for (int u = 0; u < N; u++) {
// V=find(G(u,:));
VECTOR_ID(const_view) G_row_u = MATRIX_ID(const_row)(G, u);
VECTOR_T* V = find(&G_row_u.vector);
if (V != NULL) {
// k=length(V);
int k = length(V);
// if k>=2;
if (k >= 2) {
// e=distance_inv(G(V,V));
MATRIX_T* G_idx = ordinal_index(G, V, V);
MATRIX_T* e = distance_inv(G_idx, NULL);
MATRIX_ID(free)(G_idx);
// E(u)=sum(e(:))./(k^2-k);
VECTOR_T* e_v = to_vector(e);
MATRIX_ID(free)(e);
FP_T sum_e = sum(e_v);
VECTOR_ID(free)(e_v);
VECTOR_ID(set)(E, u, sum_e / (FP_T)(k * (k - 1)));
}
VECTOR_ID(free)(V);
}
}
return E;
}
MATRIX_T* distance_inv(const MATRIX_T* G, const MATRIX_T* D) {
using namespace BCT_NAMESPACE;
MATRIX_T* D_inv;
if (D == NULL) {
MATRIX_T* G_inv = invert_elements(G);
MATRIX_T* temp = distance_wei(G_inv);
MATRIX_ID(free)(G_inv);
D_inv = invert_elements(temp);
MATRIX_ID(free)(temp);
} else {
D_inv = invert_elements(D);
}
return D_inv;
}