Matrix  $Rev: 3071 $ at $LastChangedDate: 2015-03-26 15:35:47 +0100 (Thu, 26 Mar 2015) $
lgCMatrix.c
Go to the documentation of this file.
1 #include "lgCMatrix.h"
2 
3 #include "dgCMatrix.h"
4 /* validate: -> xCMatrix_validate() in ./dgCMatrix.c */
5 
6 SEXP lgC_to_matrix(SEXP x)
7 {
8  SEXP ans, pslot = GET_SLOT(x, Matrix_pSym),
9  dn = GET_SLOT(x, Matrix_DimNamesSym);
10  int j, ncol = length(pslot) - 1,
11  nrow = INTEGER(GET_SLOT(x, Matrix_DimSym))[0],
12  *xp = INTEGER(pslot),
13  *xi = INTEGER(GET_SLOT(x, Matrix_iSym));
14  int *xx = LOGICAL(GET_SLOT(x, Matrix_xSym)), *ax;
15 
16  ax = LOGICAL(ans = PROTECT(allocMatrix(LGLSXP, nrow, ncol)));
17  for (j = 0; j < (nrow * ncol); j++) ax[j] = 0;
18  for (j = 0; j < ncol; j++) {
19  int ind;
20  for (ind = xp[j]; ind < xp[j+1]; ind++)
21  ax[j * nrow + xi[ind]] = xx[ind];
22  }
23  if (!(isNull(VECTOR_ELT(dn,0)) && isNull(VECTOR_ELT(dn,1))))
24  setAttrib(ans, R_DimNamesSymbol, duplicate(dn));
25  UNPROTECT(1);
26  return ans;
27 }
28 
29 /* as above, '1' instead of 'x' slot: */
30 SEXP ngC_to_matrix(SEXP x)
31 {
32  SEXP ans, pslot = GET_SLOT(x, Matrix_pSym),
33  dn = GET_SLOT(x, Matrix_DimNamesSym);
34  int j, ncol = length(pslot) - 1,
35  nrow = INTEGER(GET_SLOT(x, Matrix_DimSym))[0],
36  *xp = INTEGER(pslot),
37  *xi = INTEGER(GET_SLOT(x, Matrix_iSym));
38  int *ax;
39 
40  ax = LOGICAL(ans = PROTECT(allocMatrix(LGLSXP, nrow, ncol)));
41  for (j = 0; j < (nrow * ncol); j++) ax[j] = 0;
42  for (j = 0; j < ncol; j++) {
43  int ind;
44  for (ind = xp[j]; ind < xp[j+1]; ind++)
45  ax[j * nrow + xi[ind]] = 1;
46  }
47  if (!(isNull(VECTOR_ELT(dn,0)) && isNull(VECTOR_ELT(dn,1))))
48  setAttrib(ans, R_DimNamesSymbol, duplicate(dn));
49  UNPROTECT(1);
50  return ans;
51 }
52 
53 #ifdef _NEED_logical_to_csc_FIRST_
54 /* very parallel to matrix_to_csc() in ./dgCMatrix.c */
55 SEXP matrix_to_lcsc(SEXP A)
56 {
57  if (!(isMatrix(A) && isLogical(A)))
58  error(_("A must be a logical matrix"));
59  return logical_to_csc(LOGICAL(A),
60  INTEGER(getAttrib(A, R_DimSymbol)));
61 }
62 #endif
SEXP Matrix_DimSym
Definition: Syms.h:2
SEXP lgC_to_matrix(SEXP x)
Definition: lgCMatrix.c:6
SEXP Matrix_xSym
Definition: Syms.h:2
SEXP Matrix_DimNamesSym
Definition: Syms.h:2
#define _(String)
Definition: Mutils.h:32
SEXP Matrix_iSym
Definition: Syms.h:2
SEXP ngC_to_matrix(SEXP x)
Definition: lgCMatrix.c:30
SEXP Matrix_pSym
Definition: Syms.h:2