# SCM Repository

[matrix] Diff of /pkg/Matrix/src/Csparse.c
 [matrix] / pkg / Matrix / src / Csparse.c

# Diff of /pkg/Matrix/src/Csparse.c

revision 1867, Mon Jun 4 17:13:02 2007 UTC revision 2175, Wed Apr 23 11:23:50 2008 UTC
# Line 1  Line 1
1                          /* Sparse matrices in compressed column-oriented form */                          /* Sparse matrices in compressed column-oriented form */
2  #include "Csparse.h"  #include "Csparse.h"
3    #include "Tsparse.h"
4  #include "chm_common.h"  #include "chm_common.h"
5
6  SEXP Csparse_validate(SEXP x)  SEXP Csparse_validate(SEXP x)
# Line 7  Line 8
8      /* NB: we do *NOT* check a potential 'x' slot here, at all */      /* NB: we do *NOT* check a potential 'x' slot here, at all */
9      SEXP pslot = GET_SLOT(x, Matrix_pSym),      SEXP pslot = GET_SLOT(x, Matrix_pSym),
10          islot = GET_SLOT(x, Matrix_iSym);          islot = GET_SLOT(x, Matrix_iSym);
11      int j, k, sorted,      Rboolean sorted, strictly;
12        int j, k,
13          *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),          *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),
14          nrow = dims[0],          nrow = dims[0],
15          ncol = dims[1],          ncol = dims[1],
# Line 18  Line 20
20          return mkString(_("slot p must have length = ncol(.) + 1"));          return mkString(_("slot p must have length = ncol(.) + 1"));
21      if (xp[0] != 0)      if (xp[0] != 0)
22          return mkString(_("first element of slot p must be zero"));          return mkString(_("first element of slot p must be zero"));
23      if (length(islot) != xp[ncol])      if (length(islot) < xp[ncol]) /* allow larger slots from over-allocation!*/
24          return          return
25              mkString(_("last element of slot p must match length of slots i and x"));              mkString(_("last element of slot p must match length of slots i and x"));
26      for (j = 0; j < length(islot); j++) {      for (j = 0; j < length(islot); j++) {
27          if (xi[j] < 0 || xi[j] >= nrow)          if (xi[j] < 0 || xi[j] >= nrow)
28              return mkString(_("all row indices must be between 0 and nrow-1"));              return mkString(_("all row indices must be between 0 and nrow-1"));
29      }      }
30      sorted = TRUE;      sorted = TRUE; strictly = TRUE;
31      for (j = 0; j < ncol; j++) {      for (j = 0; j < ncol; j++) {
32          if (xp[j] > xp[j+1])          if (xp[j] > xp[j+1])
33              return mkString(_("slot p must be non-decreasing"));              return mkString(_("slot p must be non-decreasing"));
34          for (k = xp[j] + 1; k < xp[j + 1]; k++)          if(sorted)
35              if (xi[k] < xi[k - 1]) sorted = FALSE;              for (k = xp[j] + 1; k < xp[j + 1]; k++) {
36                    if (xi[k] < xi[k - 1])
37                        sorted = FALSE;
38                    else if (xi[k] == xi[k - 1])
39                        strictly = FALSE;
40                }
41      }      }
42      if (!sorted) {      if (!sorted) {
43          cholmod_sparse *chx = as_cholmod_sparse(x);          CHM_SP chx = AS_CHM_SP(x);
44            R_CheckStack();
45
46          cholmod_sort(chx, &c);          cholmod_sort(chx, &c);
47          Free(chx);          /* Now re-check that row indices are *strictly* increasing
48             * (and not just increasing) within each column : */
49            for (j = 0; j < ncol; j++) {
50                for (k = xp[j] + 1; k < xp[j + 1]; k++)
51                    if (xi[k] == xi[k - 1])
52                        return mkString(_("slot i is not *strictly* increasing inside a column (even after cholmod_sort)"));
53            }
54
55        } else if(!strictly) {  /* sorted, but not strictly */
56            return mkString(_("slot i is not *strictly* increasing inside a column"));
57      }      }
58      return ScalarLogical(1);      return ScalarLogical(1);
59  }  }
60
61    SEXP Rsparse_validate(SEXP x)
62    {
63        /* NB: we do *NOT* check a potential 'x' slot here, at all */
64        SEXP pslot = GET_SLOT(x, Matrix_pSym),
65            jslot = GET_SLOT(x, Matrix_jSym);
66        Rboolean sorted, strictly;
67        int i, k,
68            *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),
69            nrow = dims[0],
70            ncol = dims[1],
71            *xp = INTEGER(pslot),
72            *xj = INTEGER(jslot);
73
74        if (length(pslot) != dims[0] + 1)
75            return mkString(_("slot p must have length = nrow(.) + 1"));
76        if (xp[0] != 0)
77            return mkString(_("first element of slot p must be zero"));
78        if (length(jslot) < xp[nrow]) /* allow larger slots from over-allocation!*/
79            return
80                mkString(_("last element of slot p must match length of slots j and x"));
81        for (i = 0; i < length(jslot); i++) {
82            if (xj[i] < 0 || xj[i] >= ncol)
83                return mkString(_("all column indices must be between 0 and ncol-1"));
84        }
85        sorted = TRUE; strictly = TRUE;
86        for (i = 0; i < nrow; i++) {
87            if (xp[i] > xp[i+1])
88                return mkString(_("slot p must be non-decreasing"));
89            if(sorted)
90                for (k = xp[i] + 1; k < xp[i + 1]; k++) {
91                    if (xj[k] < xj[k - 1])
92                        sorted = FALSE;
93                    else if (xj[k] == xj[k - 1])
94                        strictly = FALSE;
95                }
96        }
97        if (!sorted)
98            /* cannot easily use cholmod_sort(.) ... -> "error out" :*/
99            return mkString(_("slot j is not increasing inside a column"));
100        else if(!strictly) /* sorted, but not strictly */
101            return mkString(_("slot j is not *strictly* increasing inside a column"));
102
103        return ScalarLogical(1);
104    }
105
106
107  /* Called from ../R/Csparse.R : */  /* Called from ../R/Csparse.R : */
108  /* Can only return [dln]geMatrix (no symm/triang);  /* Can only return [dln]geMatrix (no symm/triang);
109   * FIXME: replace by non-CHOLMOD code ! */   * FIXME: replace by non-CHOLMOD code ! */
110  SEXP Csparse_to_dense(SEXP x)  SEXP Csparse_to_dense(SEXP x)
111  {  {
112      cholmod_sparse *chxs = as_cholmod_sparse(x);      CHM_SP chxs = AS_CHM_SP(x);
113      /* This loses the symmetry property, since cholmod_dense has none,      /* This loses the symmetry property, since cholmod_dense has none,
114       * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices       * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices
115       * to numeric (CHOLMOD_REAL) ones : */       * to numeric (CHOLMOD_REAL) ones : */
116      cholmod_dense *chxd = cholmod_sparse_to_dense(chxs, &c);      CHM_DN chxd = cholmod_sparse_to_dense(chxs, &c);
117      int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);      int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);
118        R_CheckStack();
119
Free(chxs);
120      return chm_dense_to_SEXP(chxd, 1, Rkind, GET_SLOT(x, Matrix_DimNamesSym));      return chm_dense_to_SEXP(chxd, 1, Rkind, GET_SLOT(x, Matrix_DimNamesSym));
121  }  }
122
123  SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)  SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)
124  {  {
125      cholmod_sparse *chxs = as_cholmod_sparse(x);      CHM_SP chxs = AS_CHM_SP(x);
126      cholmod_sparse      CHM_SP chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);
*chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);
127      int tr = asLogical(tri);      int tr = asLogical(tri);
128        R_CheckStack();
129
130      Free(chxs);      return chm_sparse_to_SEXP(chxcp, 1/*do_free*/,
return chm_sparse_to_SEXP(chxcp, 1,
131                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
132                                0, tr ? diag_P(x) : "",                                0, tr ? diag_P(x) : "",
133                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
# Line 72  Line 135
135
136  SEXP Csparse_to_matrix(SEXP x)  SEXP Csparse_to_matrix(SEXP x)
137  {  {
138      cholmod_sparse *chxs = as_cholmod_sparse(x);      return chm_dense_to_matrix(cholmod_sparse_to_dense(AS_CHM_SP(x), &c),
139      cholmod_dense *chxd = cholmod_sparse_to_dense(chxs, &c);                                 1 /*do_free*/, GET_SLOT(x, Matrix_DimNamesSym));

Free(chxs);
return chm_dense_to_matrix(chxd, 1,
GET_SLOT(x, Matrix_DimNamesSym));
140  }  }
141
142  SEXP Csparse_to_Tsparse(SEXP x, SEXP tri)  SEXP Csparse_to_Tsparse(SEXP x, SEXP tri)
143  {  {
144      cholmod_sparse *chxs = as_cholmod_sparse(x);      CHM_SP chxs = AS_CHM_SP(x);
145      cholmod_triplet *chxt = cholmod_sparse_to_triplet(chxs, &c);      CHM_TR chxt = cholmod_sparse_to_triplet(chxs, &c);
146      int tr = asLogical(tri);      int tr = asLogical(tri);
147      int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
148        R_CheckStack();
149
Free(chxs);
150      return chm_triplet_to_SEXP(chxt, 1,      return chm_triplet_to_SEXP(chxt, 1,
151                                 tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,                                 tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
152                                 Rkind, tr ? diag_P(x) : "",                                 Rkind, tr ? diag_P(x) : "",
# Line 97  Line 156
156  /* this used to be called  sCMatrix_to_gCMatrix(..)   [in ./dsCMatrix.c ]: */  /* this used to be called  sCMatrix_to_gCMatrix(..)   [in ./dsCMatrix.c ]: */
157  SEXP Csparse_symmetric_to_general(SEXP x)  SEXP Csparse_symmetric_to_general(SEXP x)
158  {  {
159      cholmod_sparse *chx = as_cholmod_sparse(x), *chgx;      CHM_SP chx = AS_CHM_SP(x), chgx;
160      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
161        R_CheckStack();
162
163      if (!(chx->stype))      if (!(chx->stype))
164          error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));          error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));
165      chgx = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);      chgx = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);
166      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
Free(chx);
167      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
168                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
169  }  }
170
171  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
172  {  {
173      cholmod_sparse *chx = as_cholmod_sparse(x), *chgx;      CHM_SP chx = AS_CHM_SP(x), chgx;
174      int uploT = (*CHAR(asChar(uplo)) == 'U') ? 1 : -1;      int uploT = (*CHAR(STRING_ELT(uplo,0)) == 'U') ? 1 : -1;
175      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
176        R_CheckStack();
177
178      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
179      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
Free(chx);
180      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
181                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
182  }  }
183
184  SEXP Csparse_transpose(SEXP x, SEXP tri)  SEXP Csparse_transpose(SEXP x, SEXP tri)
185  {  {
186      cholmod_sparse *chx = as_cholmod_sparse(x);      /* TODO: lgCMatrix & igC* currently go via double prec. cholmod -
187         *       since cholmod (& cs) lacks sparse 'int' matrices */
188        CHM_SP chx = AS_CHM_SP(x);
189      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
190      cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c);      CHM_SP chxt = cholmod_transpose(chx, chx->xtype, &c);
191      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;
192      int tr = asLogical(tri);      int tr = asLogical(tri);
193        R_CheckStack();
194
Free(chx);
195      tmp = VECTOR_ELT(dn, 0);    /* swap the dimnames */      tmp = VECTOR_ELT(dn, 0);    /* swap the dimnames */
196      SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1));      SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1));
197      SET_VECTOR_ELT(dn, 1, tmp);      SET_VECTOR_ELT(dn, 1, tmp);
# Line 142  Line 203
203
204  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)
205  {  {
206      cholmod_sparse      CHM_SP
207          *cha = as_cholmod_sparse(a),          cha = AS_CHM_SP(Csparse_diagU2N(a)),
208          *chb = as_cholmod_sparse(b);          chb = AS_CHM_SP(Csparse_diagU2N(b)),
209      cholmod_sparse *chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);          chc = cholmod_ssmult(cha, chb, /*out_stype:*/ 0,
210                                 cha->xtype, /*out sorted:*/ 1, &c);
211        const char *cl_a = class_P(a), *cl_b = class_P(b);
212        char diag[] = {'\0', '\0'};
213        int uploT = 0;
214      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = allocVector(VECSXP, 2);
215        R_CheckStack();
216
217      Free(cha); Free(chb);      /* Preserve triangularity and even unit-triangularity if appropriate.
218         * Note that in that case, the multiplication itself should happen
219         * faster.  But there's no support for that in CHOLMOD */
220
221        /* UGLY hack -- rather should have (fast!) C-level version of
222         *       is(a, "triangularMatrix") etc */
223        if (cl_a[1] == 't' && cl_b[1] == 't')
224            /* FIXME: fails for "Cholesky","BunchKaufmann"..*/
225            if(*uplo_P(a) == *uplo_P(b)) { /* both upper, or both lower tri. */
226                uploT = (*uplo_P(a) == 'U') ? 1 : -1;
227                if(*diag_P(a) == 'U' && *diag_P(b) == 'U') { /* return UNIT-triag. */
228                    /* "remove the diagonal entries": */
229                    chm_diagN2U(chc, uploT, /* do_realloc */ FALSE);
230                    diag[0]= 'U';
231                }
232                else diag[0]= 'N';
233            }
234      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
235                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
236      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
237                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), 1)));                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), 1)));
238      return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);      return chm_sparse_to_SEXP(chc, 1, uploT, /*Rkind*/0, diag, dn);
239  }  }
240
241  SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)  SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)
242  {  {
243      int tr = asLogical(trans);      int tr = asLogical(trans);
244      cholmod_sparse      CHM_SP
245          *cha = as_cholmod_sparse(a),          cha = AS_CHM_SP(Csparse_diagU2N(a)),
246          *chb = as_cholmod_sparse(b);          chb = AS_CHM_SP(Csparse_diagU2N(b)),
247      cholmod_sparse *chTr, *chc;          chTr, chc;
248        const char *cl_a = class_P(a), *cl_b = class_P(b);
249        char diag[] = {'\0', '\0'};
250        int uploT = 0;
251      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = allocVector(VECSXP, 2);
252        R_CheckStack();
253
254  /*     cholmod_sparse *chTr = cholmod_transpose(cha, 1, &c); */      chTr = cholmod_transpose((tr) ? chb : cha, chb->xtype, &c);
/*     cholmod_sparse *chc = cholmod_ssmult(chTr, chb, 0, cha->xtype, 1, &c); */

if (tr)
chTr = cholmod_transpose(chb, chb->xtype, &c);
else
chTr = cholmod_transpose(cha, cha->xtype, &c);
255      chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,      chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,
256                           0, cha->xtype, 1, &c);                           /*out_stype:*/ 0, cha->xtype, /*out sorted:*/ 1, &c);
257        cholmod_free_sparse(&chTr, &c);
258
259      Free(cha); Free(chb); cholmod_free_sparse(&chTr, &c);      /* Preserve triangularity and unit-triangularity if appropriate;
260         * see Csparse_Csparse_prod() for comments */
261        if (cl_a[1] == 't' && cl_b[1] == 't')
262            if(*uplo_P(a) != *uplo_P(b)) { /* one 'U', the other 'L' */
263                uploT = (*uplo_P(b) == 'U') ? 1 : -1;
264                if(*diag_P(a) == 'U' && *diag_P(b) == 'U') { /* return UNIT-triag. */
265                    chm_diagN2U(chc, uploT, /* do_realloc */ FALSE);
266                    diag[0]= 'U';
267                }
268                else diag[0]= 'N';
269            }
270
271      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
272                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), (tr) ? 0 : 1)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), (tr) ? 0 : 1)));
273      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
274                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), (tr) ? 0 : 1)));                     duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), (tr) ? 0 : 1)));
275      return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);      return chm_sparse_to_SEXP(chc, 1, uploT, /*Rkind*/0, diag, dn);
276  }  }
277
278  SEXP Csparse_dense_prod(SEXP a, SEXP b)  SEXP Csparse_dense_prod(SEXP a, SEXP b)
279  {  {
280      cholmod_sparse *cha = as_cholmod_sparse(a);      CHM_SP cha = AS_CHM_SP(Csparse_diagU2N(a));
281      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
282      cholmod_dense *chb = as_cholmod_dense(b_M);      CHM_DN chb = AS_CHM_DN(b_M);
283      cholmod_dense *chc =      CHM_DN chc = cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow,
284          cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow, chb->xtype, &c);                                          chb->xtype, &c);
285      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = PROTECT(allocVector(VECSXP, 2));
286      double alpha[] = {1,0}, beta[] = {0,0};      double one[] = {1,0}, zero[] = {0,0};
287        R_CheckStack();
288
289      cholmod_sdmult(cha, 0, alpha, beta, chb, chc, &c);      cholmod_sdmult(cha, 0, one, zero, chb, chc, &c);
Free(cha); Free(chb);
UNPROTECT(1);
290      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
291                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
292      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
293                     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));                     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));
294        UNPROTECT(2);
295      return chm_dense_to_SEXP(chc, 1, 0, dn);      return chm_dense_to_SEXP(chc, 1, 0, dn);
296  }  }
297
298  SEXP Csparse_dense_crossprod(SEXP a, SEXP b)  SEXP Csparse_dense_crossprod(SEXP a, SEXP b)
299  {  {
300      cholmod_sparse *cha = as_cholmod_sparse(a);      CHM_SP cha = AS_CHM_SP(Csparse_diagU2N(a));
301      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
302      cholmod_dense *chb = as_cholmod_dense(b_M);      CHM_DN chb = AS_CHM_DN(b_M);
303      cholmod_dense *chc =      CHM_DN chc = cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol,
304          cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol, chb->xtype, &c);                                          chb->xtype, &c);
305      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = PROTECT(allocVector(VECSXP, 2));
306      double alpha[] = {1,0}, beta[] = {0,0};      double one[] = {1,0}, zero[] = {0,0};
307        R_CheckStack();
308
309      cholmod_sdmult(cha, 1, alpha, beta, chb, chc, &c);      cholmod_sdmult(cha, 1, one, zero, chb, chc, &c);
Free(cha); Free(chb);
UNPROTECT(1);
310      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
311                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));
312      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
313                     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));                     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));
314        UNPROTECT(2);
315      return chm_dense_to_SEXP(chc, 1, 0, dn);      return chm_dense_to_SEXP(chc, 1, 0, dn);
316  }  }
317
318  /* Computes   x'x  or  x x'  -- see Csparse_Csparse_crossprod above for  x'y and x y' */  /* Computes   x'x  or  x x' -- *also* for Tsparse (triplet = TRUE)
319       see Csparse_Csparse_crossprod above for  x'y and x y' */
320  SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet)  SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet)
321  {  {
322      int trip = asLogical(triplet),      int trip = asLogical(triplet),
323          tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */          tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */
324      cholmod_triplet      CHM_TR cht = trip ? AS_CHM_TR(Tsparse_diagU2N(x)) : (CHM_TR) NULL;
325          *cht = trip ? as_cholmod_triplet(x) : (cholmod_triplet*) NULL;      CHM_SP chcp, chxt,
326      cholmod_sparse *chcp, *chxt,          chx = (trip ?
327          *chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c)                 cholmod_triplet_to_sparse(cht, cht->nnz, &c) :
328          : as_cholmod_sparse(x);                 AS_CHM_SP(Csparse_diagU2N(x)));
329      SEXP dn = PROTECT(allocVector(VECSXP, 2));      SEXP dn = PROTECT(allocVector(VECSXP, 2));
330        R_CheckStack();
331
332      if (!tr)      if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c);
chxt = cholmod_transpose(chx, chx->xtype, &c);
333      chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);      chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);
334      if(!chcp)      if(!chcp) {
335            UNPROTECT(1);
336          error(_("Csparse_crossprod(): error return from cholmod_aat()"));          error(_("Csparse_crossprod(): error return from cholmod_aat()"));
337        }
338      cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);      cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);
339      chcp->stype = 1;      chcp->stype = 1;
340      if (trip) {      if (trip) cholmod_free_sparse(&chx, &c);
cholmod_free_sparse(&chx, &c);
Free(cht);
} else {
Free(chx);
}
341      if (!tr) cholmod_free_sparse(&chxt, &c);      if (!tr) cholmod_free_sparse(&chxt, &c);
342                                  /* create dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
SET_VECTOR_ELT(dn, 0,
343                     duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),                     duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),
344                                          (tr) ? 0 : 1)));                                          (tr) ? 0 : 1)));
345      SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));      SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));
# Line 261  Line 349
349
350  SEXP Csparse_drop(SEXP x, SEXP tol)  SEXP Csparse_drop(SEXP x, SEXP tol)
351  {  {
352      cholmod_sparse *chx = as_cholmod_sparse(x),      const char *cl = class_P(x);
353          *ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);      /* dtCMatrix, etc; [1] = the second character =?= 't' for triangular */
354        int tr = (cl[1] == 't');
355        CHM_SP chx = AS_CHM_SP(x);
356        CHM_SP ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);
357      double dtol = asReal(tol);      double dtol = asReal(tol);
358      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
359        R_CheckStack();
360
361      if(!cholmod_drop(dtol, ans, &c))      if(!cholmod_drop(dtol, ans, &c))
362          error(_("cholmod_drop() failed"));          error(_("cholmod_drop() failed"));
363      Free(chx);      return chm_sparse_to_SEXP(ans, 1,
364      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
365                                  Rkind, tr ? diag_P(x) : "",
366                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
367  }  }
368

369  SEXP Csparse_horzcat(SEXP x, SEXP y)  SEXP Csparse_horzcat(SEXP x, SEXP y)
370  {  {
371      cholmod_sparse *chx = as_cholmod_sparse(x),      CHM_SP chx = AS_CHM_SP(x), chy = AS_CHM_SP(y);
*chy = as_cholmod_sparse(y), *ans;
372      int Rkind = 0; /* only for "d" - FIXME */      int Rkind = 0; /* only for "d" - FIXME */
373        R_CheckStack();
374
ans = cholmod_horzcat(chx, chy, 1, &c);
Free(chx); Free(chy);
375      /* FIXME: currently drops dimnames */      /* FIXME: currently drops dimnames */
376      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", R_NilValue);      return chm_sparse_to_SEXP(cholmod_horzcat(chx, chy, 1, &c),
377                                  1, 0, Rkind, "", R_NilValue);
378  }  }
379
380  SEXP Csparse_vertcat(SEXP x, SEXP y)  SEXP Csparse_vertcat(SEXP x, SEXP y)
381  {  {
382      cholmod_sparse *chx = as_cholmod_sparse(x),      CHM_SP chx = AS_CHM_SP(x), chy = AS_CHM_SP(y);
*chy = as_cholmod_sparse(y), *ans;
383      int Rkind = 0; /* only for "d" - FIXME */      int Rkind = 0; /* only for "d" - FIXME */
384        R_CheckStack();
385
ans = cholmod_vertcat(chx, chy, 1, &c);
Free(chx); Free(chy);
386      /* FIXME: currently drops dimnames */      /* FIXME: currently drops dimnames */
387      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", R_NilValue);      return chm_sparse_to_SEXP(cholmod_vertcat(chx, chy, 1, &c),
388                                  1, 0, Rkind, "", R_NilValue);
389  }  }
390
391  SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2)  SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2)
392  {  {
393      cholmod_sparse *chx = as_cholmod_sparse(x), *ans;      CHM_SP chx = AS_CHM_SP(x);
394      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
395        CHM_SP ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);
396        R_CheckStack();
397
ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);
Free(chx);
398      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
399                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
400  }  }
401
402  SEXP Csparse_diagU2N(SEXP x)  SEXP Csparse_diagU2N(SEXP x)
403  {  {
404      if (*diag_P(x) != 'U') {/* "trivially fast" when there's no 'diag' slot at all */      const char *cl = class_P(x);
405        /* dtCMatrix, etc; [1] = the second character =?= 't' for triangular */
406        if (cl[1] != 't' || *diag_P(x) != 'U') {
407            /* "trivially fast" when not triangular (<==> no 'diag' slot),
408               or not *unit* triangular */
409          return (x);          return (x);
410      }      }
411      else {      else { /* unit triangular (diag='U'): "fill the diagonal" & diag:= "N" */
412          cholmod_sparse *chx = as_cholmod_sparse(x);          CHM_SP chx = AS_CHM_SP(x);
413          cholmod_sparse *eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c);          CHM_SP eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c);
414          double one[] = {1, 0};          double one[] = {1, 0};
415          cholmod_sparse *ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c);          CHM_SP ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c);
416          int uploT = (*uplo_P(x) == 'U') ? 1 : -1;          int uploT = (*uplo_P(x) == 'U') ? 1 : -1;
417          int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;          int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
418
419          Free(chx); cholmod_free_sparse(&eye, &c);          R_CheckStack();
420            cholmod_free_sparse(&eye, &c);
421          return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",          return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",
422                                    GET_SLOT(x, Matrix_DimNamesSym));                                    GET_SLOT(x, Matrix_DimNamesSym));
423      }      }
424  }  }
425
426    SEXP Csparse_diagN2U(SEXP x)
427    {
428        const char *cl = class_P(x);
429        /* dtCMatrix, etc; [1] = the second character =?= 't' for triangular */
430        if (cl[1] != 't' || *diag_P(x) != 'N') {
431            /* "trivially fast" when not triangular (<==> no 'diag' slot),
432               or already *unit* triangular */
433            return (x);
434        }
435        else { /* triangular with diag='N'): now drop the diagonal */
436            /* duplicate, since chx will be modified: */
437            CHM_SP chx = AS_CHM_SP(duplicate(x));
438            int uploT = (*uplo_P(x) == 'U') ? 1 : -1,
439                Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
440            R_CheckStack();
441
442            chm_diagN2U(chx, uploT, /* do_realloc */ FALSE);
443
444            return chm_sparse_to_SEXP(chx, /*dofree*/ 0/* or 1 ?? */,
445                                      uploT, Rkind, "U",
446                                      GET_SLOT(x, Matrix_DimNamesSym));
447        }
448    }
449
450  SEXP Csparse_submatrix(SEXP x, SEXP i, SEXP j)  SEXP Csparse_submatrix(SEXP x, SEXP i, SEXP j)
451  {  {
452      cholmod_sparse *chx = as_cholmod_sparse(x);      CHM_SP chx = AS_CHM_SP(x);
453      int rsize = (isNull(i)) ? -1 : LENGTH(i),      int rsize = (isNull(i)) ? -1 : LENGTH(i),
454          csize = (isNull(j)) ? -1 : LENGTH(j);          csize = (isNull(j)) ? -1 : LENGTH(j);
455      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
456        R_CheckStack();
457
458      if (rsize >= 0 && !isInteger(i))      if (rsize >= 0 && !isInteger(i))
459          error(_("Index i must be NULL or integer"));          error(_("Index i must be NULL or integer"));
# Line 346  Line 466
466                                1, 0, Rkind, "",                                1, 0, Rkind, "",
467                                /* FIXME: drops dimnames */ R_NilValue);                                /* FIXME: drops dimnames */ R_NilValue);
468  }  }
469
470    SEXP Csparse_MatrixMarket(SEXP x, SEXP fname)
471    {
472        FILE *f = fopen(CHAR(asChar(fname)), "w");
473
474        if (!f)
475            error(_("failure to open file \"%s\" for writing"),
476                  CHAR(asChar(fname)));
477        if (!cholmod_write_sparse(f, AS_CHM_SP(Csparse_diagU2N(x)),
478                                  (CHM_SP)NULL, (char*) NULL, &c))
479            error(_("cholmod_write_sparse returned error code"));
480        fclose(f);
481        return R_NilValue;
482    }
483
484
485    /**
486     * Extract the diagonal entries from *triangular* Csparse matrix  __or__ a
487     * cholmod_sparse factor (LDL = TRUE).
488     *
489     * @param n  dimension of the matrix.
490     * @param x_p  'p' (column pointer) slot contents
491     * @param x_x  'x' (non-zero entries) slot contents
492     * @param perm 'perm' (= permutation vector) slot contents; only used for "diagBack"
493     * @param resultKind a (SEXP) string indicating which kind of result is desired.
494     *
495     * @return  a SEXP, either a (double) number or a length n-vector of diagonal entries
496     */
497    SEXP diag_tC_ptr(int n, int *x_p, double *x_x, int *perm, SEXP resultKind)
498    /*                                ^^^^^^ FIXME[Generalize] to int / ... */
499    {
500        const char* res_ch = CHAR(STRING_ELT(resultKind,0));
501        enum diag_kind { diag, diag_backpermuted, trace, prod, sum_log
502        } res_kind = ((!strcmp(res_ch, "trace")) ? trace :
503                      ((!strcmp(res_ch, "sumLog")) ? sum_log :
504                       ((!strcmp(res_ch, "prod")) ? prod :
505                        ((!strcmp(res_ch, "diag")) ? diag :
506                         ((!strcmp(res_ch, "diagBack")) ? diag_backpermuted :
507                          -1)))));
508        int i, n_x, i_from = 0;
509        SEXP ans = PROTECT(allocVector(REALSXP,
510    /*                                 ^^^^  FIXME[Generalize] */
511                                       (res_kind == diag ||
512                                        res_kind == diag_backpermuted) ? n : 1));
513        double *v = REAL(ans);
514    /*  ^^^^^^      ^^^^  FIXME[Generalize] */
515
516    #define for_DIAG(v_ASSIGN)                                              \
517        for(i = 0; i < n; i++, i_from += n_x) {                             \
518            /* looking at i-th column */                                    \
519            n_x = x_p[i+1] - x_p[i];/* #{entries} in this column */ \
520            v_ASSIGN;                                                       \
521        }
522
523        /* NOTA BENE: we assume  -- uplo = "L" i.e. lower triangular matrix
524         *            for uplo = "U" (makes sense with a "dtCMatrix" !),
525         *            should use  x_x[i_from + (nx - 1)] instead of x_x[i_from],
526         *            where nx = (x_p[i+1] - x_p[i])
527         */
528
529        switch(res_kind) {
530        case trace:
531            v[0] = 0.;
532            for_DIAG(v[0] += x_x[i_from]);
533            break;
534
535        case sum_log:
536            v[0] = 0.;
537            for_DIAG(v[0] += log(x_x[i_from]));
538            break;
539
540        case prod:
541            v[0] = 1.;
542            for_DIAG(v[0] *= x_x[i_from]);
543            break;
544
545        case diag:
546            for_DIAG(v[i] = x_x[i_from]);
547            break;
548
549        case diag_backpermuted:
550            for_DIAG(v[i] = x_x[i_from]);
551
552            warning(_("resultKind = 'diagBack' (back-permuted) is experimental"));
553            /* now back_permute : */
554            for(i = 0; i < n; i++) {
555                double tmp = v[i]; v[i] = v[perm[i]]; v[perm[i]] = tmp;
556                /*^^^^ FIXME[Generalize] */
557            }
558            break;
559
560        default: /* -1 from above */
561            error("diag_tC(): invalid 'resultKind'");
562            /* Wall: */ ans = R_NilValue; v = REAL(ans);
563        }
564
565        UNPROTECT(1);
566        return ans;
567    }
568
569    /**
570     * Extract the diagonal entries from *triangular* Csparse matrix  __or__ a
571     * cholmod_sparse factor (LDL = TRUE).
572     *
573     * @param pslot  'p' (column pointer)   slot of Csparse matrix/factor
574     * @param xslot  'x' (non-zero entries) slot of Csparse matrix/factor
575     * @param perm_slot  'perm' (= permutation vector) slot of corresponding CHMfactor;
576     *                   only used for "diagBack"
577     * @param resultKind a (SEXP) string indicating which kind of result is desired.
578     *
579     * @return  a SEXP, either a (double) number or a length n-vector of diagonal entries
580     */
581    SEXP diag_tC(SEXP pslot, SEXP xslot, SEXP perm_slot, SEXP resultKind)
582    {
583        int n = length(pslot) - 1, /* n = ncol(.) = nrow(.) */
584            *x_p  = INTEGER(pslot),
585            *perm = INTEGER(perm_slot);
586        double *x_x = REAL(xslot);
587    /*  ^^^^^^        ^^^^ FIXME[Generalize] to INTEGER(.) / LOGICAL(.) / ... xslot !*/
588
589        return diag_tC_ptr(n, x_p, x_x, perm, resultKind);
590    }

Legend:
 Removed from v.1867 changed lines Added in v.2175