SCM

SCM Repository

[matrix] Diff of /pkg/src/dgTMatrix.c
ViewVC logotype

Diff of /pkg/src/dgTMatrix.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 874, Sat Aug 27 21:27:51 2005 UTC revision 876, Sat Aug 27 22:50:57 2005 UTC
# Line 107  Line 107 
107    
108  SEXP graphNEL_as_dgTMatrix(SEXP x, SEXP symmetric)  SEXP graphNEL_as_dgTMatrix(SEXP x, SEXP symmetric)
109  {  {
110        int sym = asLogical(symmetric);
111      SEXP nodes = GET_SLOT(x, install("nodes")),      SEXP nodes = GET_SLOT(x, install("nodes")),
112          edgeL = GET_SLOT(x, install("edgeL")),          edgeL = GET_SLOT(x, install("edgeL")),
113          ans = PROTECT(NEW_OBJECT(MAKE_CLASS(LOGICAL(symmetric)[0]          ans = PROTECT(NEW_OBJECT(MAKE_CLASS(sym
114                                              ? "dsTMatrix"                                              ? "dsTMatrix"
115                                              : "dgTMatrix")));                                              : "dgTMatrix")));
116      int *ii, *jj, *dims, i, j, nnd = LENGTH(nodes), pos, totl;      int *ii, *jj, *dims, i, j, nnd = LENGTH(nodes), pos, totl;
# Line 125  Line 126 
126          SET_VECTOR_ELT(dnms, 0, duplicate(nodes));          SET_VECTOR_ELT(dnms, 0, duplicate(nodes));
127          SET_VECTOR_ELT(dnms, 1, duplicate(nodes));          SET_VECTOR_ELT(dnms, 1, duplicate(nodes));
128      }      }
129      ii = INTEGER(ALLOC_SLOT(ans, Matrix_iSym, INTSXP, totl));      ii = Calloc(totl, int);
130      jj = INTEGER(ALLOC_SLOT(ans, Matrix_jSym, INTSXP, totl));      jj = Calloc(totl, int);
131      xx = REAL(ALLOC_SLOT(ans, Matrix_xSym, REALSXP, totl));      xx = Calloc(totl, double);
132      pos = 0;      pos = 0;
133      for (i = 0; i < nnd; i++) {      for (i = 0; i < nnd; i++) {
134          SEXP edg = VECTOR_ELT(edgeL, i);          SEXP edg = VECTOR_ELT(edgeL, i);
135          SEXP edges = Matrix_getElement(edg, "edges"),          SEXP edges = Matrix_getElement(edg, "edges"),
136              weights = Matrix_getElement(edg, "weights");              weights = Matrix_getElement(edg, "weights");
137          int *edgs = INTEGER(edges), nedg = LENGTH(edges);          int *edgs = INTEGER(PROTECT(coerceVector(edges, INTSXP))),
138                nedg = LENGTH(edges);
139          double *wts = REAL(weights);          double *wts = REAL(weights);
140    
141          for (j = 0; j < nedg; j++) {          for (j = 0; j < nedg; j++) {
142                int j1 = edgs[j] - 1;
143                            /* symmetric case stores upper triangle only */
144                if ((!sym) || i <= j1) {
145              ii[pos] = i;              ii[pos] = i;
146              jj[pos] = edgs[j] - 1;                  jj[pos] = j1;
147              xx[pos] = wts[j];              xx[pos] = wts[j];
148                    pos++;
149          }          }
150      }      }
151      UNPROTECT(1);      UNPROTECT(1);
152        }
153        Memcpy(INTEGER(ALLOC_SLOT(ans, Matrix_iSym, INTSXP, pos)), ii, pos);
154        Memcpy(INTEGER(ALLOC_SLOT(ans, Matrix_jSym, INTSXP, pos)), jj, pos);
155        Memcpy(REAL(ALLOC_SLOT(ans, Matrix_xSym, REALSXP, pos)), xx, pos);
156    
157        Free(ii); Free(jj); Free(xx);
158        UNPROTECT(1);
159      return ans;      return ans;
160  }  }

Legend:
Removed from v.874  
changed lines
  Added in v.876

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge