1 /*
2  process_sparce.c: Speed up computations on sparse matrix for constraint calculation.
3 
4  AUTHOR: Simon Jacobs <sdjacobs@uchicago.edu>
5  LICENSE: GPLv2
6 */
7 
8 
9 #include <R.h>
10 #include <Rinternals.h>
11 
process_sparse(int * I,int * J,double * X,double * Ai,double * deg,int n,double * out)12 double * process_sparse(int *I, int *J, double *X, double *Ai,  double *deg, int n, double *out)
13 {
14 
15   for(int p = 0; p < n; p++) {
16     int j = J[p];
17     int i = I[p];
18     out[p] = X[p] * Ai[j] * Ai[i] * deg[j];
19     //out[p] = (out[p] == 0 ? 0 : 1/out[p]);
20   }
21 
22   return out;
23 }
24 
process_sparse_R(SEXP sI,SEXP sJ,SEXP sX,SEXP sAi,SEXP sdeg,SEXP sn)25 SEXP process_sparse_R(SEXP sI, SEXP sJ, SEXP sX, SEXP sAi, SEXP sdeg, SEXP sn)
26 {
27 
28   int n = INTEGER(sn)[0];
29 
30   SEXP sres = PROTECT(allocVector(REALSXP, n));
31 
32   /*Coerce inputs*/
33   int *I = INTEGER(sI),
34     *J = INTEGER(sJ);
35 
36   double *X = REAL(sX),
37     *Ai = REAL(sAi),
38     *deg = REAL(sdeg),
39     *res = REAL(sres);
40 
41   process_sparse(I, J, X, Ai, deg, n, res);
42 
43   UNPROTECT(1);
44 
45   return sres;
46 }
47 
48 
49 
50 
51