schochastics / signnet
1
#include <RcppArmadillo.h>
2

3
// [[Rcpp::depends(RcppArmadillo)]]
4
using namespace Rcpp;
5

6
// [[Rcpp::export]]
7 1
double blockCriterion(arma::sp_mat A,IntegerVector clu,double alpha){
8 1
  double P = 0;
9 1
  double N = 0;
10

11 1
  for (arma::sp_mat::const_iterator i = A.begin(); i != A.end(); ++i) {
12 1
    if((clu[i.row()]==clu[i.col()]) & (*i==-1)){
13 1
      N+=1;
14
    }
15 1
    if((clu[i.row()]!=clu[i.col()]) & (*i==1)){
16 1
      P+=1;
17
    }
18
  }
19 1
  return alpha*N+(1-alpha)*P;
20
}
21

22
// [[Rcpp::export]]
23 1
double critUpdate(arma::sp_mat A, int v, int from, int to,IntegerVector clu,double alpha){
24 1
  double P = 0;
25 1
  double N = 0;
26 1
  arma::sp_mat::const_col_iterator start = A.begin_col(v);
27 1
  arma::sp_mat::const_col_iterator end = A.end_col(v);
28 1
  for(arma::sp_mat::const_col_iterator j = start; j != end; ++j){
29

30 1
    if((clu[j.row()]==from) & (A(j.row(),v)==-1)){
31 1
      N-=1;
32
    }
33 1
    if((clu[j.row()]==to) & (A(j.row(),v)==-1)){
34 1
      N+=1;
35
    }
36 1
    if((clu[j.row()]==to) & (A(j.row(),v)==1)){
37 1
      P-=1;
38
    }
39 1
    if((clu[j.row()]==from) & (A(j.row(),v)==1)){
40 1
      P+=1;
41
    }
42
  }
43 1
  A=A.t();
44 1
  start = A.begin_col(v);
45 1
  end = A.end_col(v);
46 1
  for(arma::sp_mat::const_col_iterator j = start; j != end; ++j){
47

48 1
    if((clu[j.row()]==from) & (A(j.row(),v)==-1)){
49 1
      N-=1;
50
    }
51 1
    if((clu[j.row()]==to) & (A(j.row(),v)==-1)){
52 1
      N+=1;
53
    }
54 1
    if((clu[j.row()]==to) & (A(j.row(),v)==1)){
55 1
      P-=1;
56
    }
57 1
    if((clu[j.row()]==from) & (A(j.row(),v)==1)){
58 1
      P+=1;
59
    }
60
  }
61 1
  return alpha*N+(1-alpha)*P;
62

63
}
64

65
// [[Rcpp::export]]
66 1
List optimBlocks1(arma::sp_mat A,IntegerVector clu, int k, double alpha){
67

68 1
  double crit = blockCriterion(A,clu,alpha);
69 1
  int n = A.n_cols;
70 1
  int maxiter = n*n;
71 1
  int iter=0;
72

73 1
  IntegerVector cluSizes(k);
74 1
  for(int i=0; i<n;++i){
75 1
    cluSizes[clu[i]]+=1;
76
  }
77

78 1
  NumericMatrix updateMat(n,k);
79 1
  IntegerVector mover(2);
80 1
  while(iter<maxiter){
81 1
    double minDelta = 1000;
82 1
    double minOptim = 1000;
83 1
    for(int i=0;i<n;++i){
84 1
      for(int j=0;j<k;++j){
85 1
        updateMat(i,j) = critUpdate(A, i, clu[i], j,clu,alpha);
86 1
        if(updateMat(i,j)+cluSizes[j]<minOptim){
87 1
          minDelta = updateMat(i,j);
88 1
          minOptim = minDelta + cluSizes[j];
89 1
          mover[0] = i;
90 1
          mover[1] = j;
91
        }
92
      }
93
    }
94 1
    if(minDelta<0){
95 1
      cluSizes[clu[mover[0]]]-=1;
96 1
      cluSizes[mover[1]]+=1;
97 1
      clu[mover[0]]= mover[1];
98 1
      crit+=minDelta;
99

100
    } else{
101 1
      break;
102
    }
103 1
    iter+=1;
104
  }
105 1
  return Rcpp::List::create(Rcpp::Named("membership") = clu,
106 1
                            Rcpp::Named("criterion")=crit);
107
}
108

109
// [[Rcpp::export]]
110 1
double blockCriterion1(IntegerVector clu,arma::sp_mat A,double alpha,int k){
111 1
  double P = 0;
112 1
  double N = 0;
113

114 1
  for (arma::sp_mat::const_iterator i = A.begin(); i != A.end(); ++i) {
115 1
    if((clu[i.row()]==clu[i.col()]) && (*i==-1)){
116 1
      N+=1;
117
    }
118 1
    if((clu[i.row()]!=clu[i.col()]) && (*i==1)){
119 1
      P+=1;
120
    }
121
  }
122 1
  return alpha*N+(1-alpha)*P;
123
}

Read our documentation on viewing source code .

Loading