-
Notifications
You must be signed in to change notification settings - Fork 0
/
makelatticeCIJ.cpp
87 lines (65 loc) · 1.95 KB
/
makelatticeCIJ.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
#include "bct.h"
/*
* Generates a random directed binary graph with a lattice organization.
*/
MATRIX_T* BCT_NAMESPACE::makelatticeCIJ(int N, int K) {
// CIJ = zeros(N);
MATRIX_T* CIJ = zeros(N);
// CIJ1 = ones(N);
MATRIX_T* CIJ1 = ones(N);
// KK = 0;
int KK = 0;
// cnt = 0;
int cnt = -1;
// seq = 1:N-1;
VECTOR_T* seq = sequence(1, N - 1);
MATRIX_T* dCIJ = NULL;
// while (KK<K)
while (KK < K) {
// cnt = cnt + 1;
cnt++;
// dCIJ = triu(CIJ1,seq(cnt))-triu(CIJ1,seq(cnt)+1);
if (dCIJ != NULL) {
MATRIX_ID(free)(dCIJ);
}
dCIJ = triu(CIJ1, (int)VECTOR_ID(get)(seq, cnt));
MATRIX_T* triu_CIJ1_seq_cnt_add_1 = triu(CIJ1, (int)VECTOR_ID(get)(seq, cnt) + 1);
MATRIX_ID(sub)(dCIJ, triu_CIJ1_seq_cnt_add_1);
MATRIX_ID(free)(triu_CIJ1_seq_cnt_add_1);
// dCIJ = dCIJ+dCIJ';
MATRIX_T* dCIJ_transpose = MATRIX_ID(alloc)(dCIJ->size2, dCIJ->size1);
MATRIX_ID(transpose_memcpy)(dCIJ_transpose, dCIJ);
MATRIX_ID(add)(dCIJ, dCIJ_transpose);
MATRIX_ID(free)(dCIJ_transpose);
// CIJ = CIJ + dCIJ;
MATRIX_ID(add)(CIJ, dCIJ);
// KK = sum(sum(CIJ));
VECTOR_T* sum_CIJ = sum(CIJ);
KK = (int)sum(sum_CIJ);
VECTOR_ID(free)(sum_CIJ);
}
MATRIX_ID(free)(CIJ1);
VECTOR_ID(free)(seq);
// overby = KK-K;
int overby = KK - K;
// if(overby>0)
if (overby > 0) {
// [i j] = find(dCIJ);
MATRIX_T* find_dCIJ = find_ij(dCIJ);
VECTOR_ID(view) i = MATRIX_ID(column)(find_dCIJ, 0);
VECTOR_ID(view) j = MATRIX_ID(column)(find_dCIJ, 1);
// rp = randperm(length(i));
gsl_permutation* rp = randperm(length(&i.vector));
// for ii=1:overby
for (int ii = 0; ii < overby; ii++) {
// CIJ(i(rp(ii)),j(rp(ii))) = 0;
int i_rp_ii = VECTOR_ID(get)(&i.vector, gsl_permutation_get(rp, ii));
int j_rp_ii = VECTOR_ID(get)(&j.vector, gsl_permutation_get(rp, ii));
MATRIX_ID(set)(CIJ, i_rp_ii, j_rp_ii, 0.0);
}
MATRIX_ID(free)(find_dCIJ);
gsl_permutation_free(rp);
}
MATRIX_ID(free)(dCIJ);
return CIJ;
}